Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The cosine function can be evaluated by the following infinite series: cos x = 1

ID: 3862807 • Letter: T

Question

The cosine function can be evaluated by the following infinite series: cos x = 1 - x^2/2! + x^4/4! - x^5/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; cosx = 1 - x^2/2!; up to the order term n of your choosing. For each of the preceding, compute and cos x =1 - x^2/2! + x^4/4! 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

Here is the matlab code for you:

%Utilizing a for-loop, compute and display in sequence the values for all 8 terms as shown below.

x = input('Enter the value of x: ');

cosX = 1.0;

for i = 1 : 7

n = 2*i;

fact = 1;

j = 1;

while(1)

fact = fact * j;

j = j + 1;

if(j == n+1)

break;

end

end

cosX = cosX + (-1)^i * x^n/fact;

pError = (cos(x) - cosX) / cos(x) * 100;

fprintf('%d %.4f %.4f ', i+1, cosX, pError);

end

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote