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

How to program the following on MATLAB The value of cos(x) can be approximated u

ID: 1766992 • Letter: H

Question

How to program the following on MATLAB

The value of cos(x) can be approximated using a Maclaurin series cos(x) = 1--+ 2! 4 6! Which can be expressed compactly as k-I) cos(x)-2(-1) (2(k-1)): ! Using a midpoint break loop to determine how many terms must be included in the summation in order to find the correct value of cos(2) within an error of 0.001. Hint: You can try maximum iteration of 10 or 20, and use a break command to exit from the for loop once it is within an error of 0.001 compared with cos (2) calculated using the MATLAB's built-in function cos () Here, we assume that cos (2) is an exact value, and summation you calculated from the for loop is your approximation. You can use the built-in function factorial) for Maclaurin series calculation. The following is the pseudo codes for your programming: Calculate an exact value of cos(2) using MATLAB's built-in function cos Initialize sum 0 Start the for loop with an index of k-1:20 Keep adding Maclaurin series terms inside of for loop for sum Break from the for loop when abs (sum-exact)

Explanation / Answer

Code

clc
clear all
X = input("Enter value of X=");
sum = 0; % intialization of approximate value
exact=cos(X)

for k = 1:1:10 % this for loop for iteration
sum = sum +( ((-1)^(k-1))*(X^(2*(k-1)) ) )/ factorial( 2*(k-1) );

if ( abs((sum-exact)) < 0.001) % this condition for accuracy
    break;
end

end

printf("It took %d iterms to converge ", k)
printf("Approximate value of cos(%d) is %f ", X,sum)
printf("Exact (build in) value of cos(%d) is %f ", X, exact)

Output:-

Enter value of X=2
exact = -0.41615
It took 5 iterms to converge
Approximate value of cos(2) is -0.415873
Exact (build in) value of cos(2) is -0.416147

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