The cosine function can be evaluated by the following infini
The cosine function can be evaluated by the following infinite series: cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + Write a MATLAB code to implement this formula so that it computes and prints the value of cos(pi/3) using 5 terms of approximation function above. where cos(x) =1 (1 term) cos(x) = 1 - x^2/2! cos(x) = 1 - x^2/2! + x^4/4! and so on. Also, compute and display the percent relative error % error = true - series approximation/true times 100% What is the type of error resulted from this approximation (Name of the error). Plot your solution for cos(x) in MATLAB and attach the plot.
Solution
Code:
%assigning the values of x and n as pi/3 and 5 respectively
x=pi/3;
n=5;
% initize resultant variable
sum=0;
%iterate the loop n times
for k=1:n
init = (-1)^(k-1);
num = x^(2*(k-1));
denom = factorial(2*(k-1));
total=(init*num)/denom;
sum=sum+total;
end
fprintf(\'cos(%f)=%6.6f \ \',x,sum);
Output:
cos(1.047198)=0.500000
