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

4. The cosine function (in radians) can be approximated by the following infinit

ID: 3111526 • Letter: 4

Question

4. The cosine function (in radians) can be approximated by the following infinite series: cos x = 1 + + a) Approximate cos(0.75) using the first 4 terms of the series. Calculated the true percent relative error of your approximation. Write a MATLAB function that implements this formula. The inputs to the function should be the value x and the maximum true percent relative error desired. The function should continue to include terms in the infinite series until the true percent relative error of the approximation falls below the input maximum true percent relative error. For example, if the inputs arex 0.75 and maxtpre = 0.01, the function will include more and more terms of the infinite until the true percent relative error of the approximation falls below 0.01. Once this is achieved, the function will terminate and return the approximate value and corresponding true percent relative error. Use your function to approximate cos(0.75) with max tpre 0.01 b) a. Hint 1 you can use the built in function·factorial'. In MATLAB, factorial(0)-1. Hint 2 you will need to use a while loop since you don't know how many iterations are required. Hint 3 you can use a "counting" variable to help calculate each new term in the series (see pseudocode snippet below). Notice the psuedocode below does not account for the sign flipping for each new term. b. C. approx-0 count 1 DO new termx(2"(count-1))/factorial(count-1)*2) approx = approx. + new term count count+1 err = tpre(cos(x)., cos(x)) %calls tpre function from previous problem IF err 5 EXIT ENDIF ENDDO

Explanation / Answer

a)

clc;
clear all;
close all;

x=0.75;
f_ap=1-x^2/factorial(2)+x^4/factorial(4)-x^6/factorial(6);
f_act=cos(x);
rel_err=abs(f_act-f_ap);
per_rel_err=abs(rel_err/f_act*100)

output:

per_rel_err =

3.3723e-004

b)

%%% function %%%%

function [val,tpre]=cosapprox (x,err)
val=0;
act=cos(x);
for n=0:100
val=val+(-1)^(n)*x^(2*n)/factorial(2*n);
tpre=(abs(val-act)/act*100)
if (tpre < err)
break;
end
end

%%% Main %%%

clc;
clear all;
close all;

x=0.75;
[v,k]=cosapprox(0.75,0.01)

output:

v =

0.7317


k =

3.3723e-004

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