a) Write a Matlab script file to calculate sin(x) and cos(x), which are defined
ID: 2081483 • Letter: A
Question
a) Write a Matlab script file to calculate sin(x) and cos(x), which are defined as follows:
sin(x)=x-x^3/3!+x^5/5!-x^7/7!+.... cos(x) 1-x^2/2!+x^4/4!-x^6/6!+...
Here n! is 'n factorial':n!=n(n-1)(n-2)...3*2*1.
Note that the argument x is in radians, NOT degrees! Your program should have the following features:
Prompt user for x and the number of terms to include in the summation (input the values of x and n from command window)
Do NOT calculate n! for each term. Recognize that for each successive term, n! for that term is n! of the previous term times (n+1)·(n+2).
Similarly do NOT calculate x^n for each term. Recognize that each successive term for n is x from the previous term multiplied by x^2 , i.e., x^(n+2)=x^n*x^2
Tips:
Manually calculate the first several terms to verify your program is working correctly o Learn to use the debugger in Matlab. It is an invaluable tool.
Use the ‘format long’ command to express your results with many digits
b) use your program to compute the following: sin(0.01) and cos(0.01) using 80 terms, sin(0.5) and sin(0.5) using 80 terms, sin(10) and cos(10) using 80 terms
c) how many terms are required to computer each of the following to 10 digits of accuracy (tip: use the sin() and cos() functions in MATLAB to get the true values) sin(0.5), sin(5), sin(20)
d) can you calculate sin(100)? can you calculate sin(0.01) using 200 terms? If not, what do you think is happening?
Explanation / Answer
a.Script to calculate sine and cosine taylor expansion :
Matlab code for sine series
% SINESERIES: computes sin(x) from series expansion.
% A script to evaluate the series expansion of sine with the formula:
% sin(x) = x-(x^3/3!)+(x^5/5!)-...
x = input('Enter the argument x: ');
n = input('Enter the interval n: ');
N = 1:1:n; % Creates row vector with n elements, 1 at a time.
k = 2*N-1; % For convenience with prod() function
j = N-1; % For cleanup.
sinseries = ((-1).^(j)).*((x.^(k))./(prod(k)))
sum(sinseries)
Type the code and execute in matlab after that you just need to enter value of argument x and value of n and you will get the sine series
Matlab code for cosine series
% COSINESERIES: computes cos(x) from series expansion.
% A script to evaluate the series expansion of cosine with the formula:
% sin(x) = 1-(x^2/2!)+(x^4/4!)-...
x = input('Enter the argument x: ');
n = input('Enter the interval n: ');
N = 1:1:n; % Creates row vector with n elements, 1 at a time.
k = 2*N-1; % For convenience with prod() function
j = N-1; % For cleanup.
cosseries = ((-1).^(j)).*((x.^(k))./(prod(k)))
sum(cosseries)
Type the code and execute in matlab after that you just need to enter value of argument x and value of n and you will get the cosine series.
b. Put value of x=0.01 and value of n=80 you will get the answer.
Put valuue of x=0.5 and value of n=80
Put value of x=10 and value of n=80
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.