23. The Taylor se (-12n (2n) cos(x) 1 = 2! 4 6! where x is in radians. Write a M
ID: 3167896 • Letter: 2
Question
23. The Taylor se (-12n (2n) cos(x) 1 = 2! 4 6! where x is in radians. Write a MATLAB program that determines cos(x) using the Taylor series expansion. The program asks the user to type a value for an angle in degrees. Then the program uses a loop for adding the terms of the Taylor series. If a, is the nth term in the series, then the sum S, of the n terms is S" S"-1 +a". In each pass calculate the estimated error E given by E-Pa-sell . Stop adding terms when E S-S .S 0.000001. The program displays the value of cos(x). Use the program for calculating: (a) cos (35°) Compare the values with those obtained by using a calculator. (b) cos(125)Explanation / Answer
%%% Matlab code %%%%
x1=input('Enter the angle in degree :');
x=x1*pi/180;
tol=10^(-7);
f=0;
for n=0:100
f=f+(-1)^(n)*x^(2*n)/factorial(2*n);
s(n+1)=f;
if (n>1)
err=abs((s(n+1)-s(n))/s(n));
if err < tol
break;
end
end
end
f1=cos(x);
fprintf(' cos( %d ) = %f ',x1,s(end));
fprintf(' Actual value of cos( %d ) = %1.8f ',x1,f1);
OUTPUT:
Enter the angle in degree :35
cos( 35 ) = 0.819152
Actual value of cos( 35 ) = 0.81915204
Enter the angle in degree :125
cos( 125 ) = -0.573576
Actual value of cos( 125 ) = -0.57357644
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.