The cosine function can be evaluated by the following infinite series: cos x = 1
ID: 3108954 • Letter: T
Question
The cosine function can be evaluated by the following infinite series: cos x = 1- x^2/2! + x^4/4! - x^6/6! +... Write an M-file to implement this formula so that it computes and prints out the values of cos x as each term in the series is added. Employ the library function for the cosine in your Matlab to determine the true value. In other words, compute and print in sequence the values for cos x = 1; cos x = 1- x^2/2!; up to the order term n of your choosing. For each of the preceding, cos x = 1 x^2/2! + x^4/4! compute and display percent relative error as % error = (true-series approximation)/true times 100%. Have the program print out the series approximation and the error at each step.Explanation / Answer
clc;
clear all;
close all;
n=input(' Enter the number of terms u want in expantion ');
% % let x= pi/6
x=pi/3;
sum=0;
for k=0:n
sum=sum+(-1)^k*x^(2*k)/factorial(2*k);
cosx=sum;
cosx
p_err=abs(cos(x)-cosx)/cosx*100
end
output:
Enter the number of terms u want in expantion 3
cosx =
1
p_err =
50.0000
cosx =
0.4517
p_err =
10.6957
cosx =
0.5018
p_err =
0.3580
cosx =
0.5000
p_err =
0.0071
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.