Using the Taylor series expansion of a suitable function sho
Using the Taylor series expansion of a suitable function, show that a cosine function can be evaluated by the following infinite series as (where the angle x is given in radians): cos x = 1 -x^2/2! + x^4/4! - X^6/6! + ............ Create a function M-file my COS that takes the angle x (in radians), and the desired accuracy SF as the input, and returns COS(x). Test your function to find cosine of pi/2 and 2pi with an accuracy of 14 significant digits.
Solution
% matlab code to approximate cosine value using taylor series
function approx = cosineApprox(x,loop_tolerance)
approx=1;
i = 1;
new = 2;
old = 1;
while abs(new - old) > loop_tolerance
old = approx;
addterm = (-1)^i*(x^(2*i))/factorial(2*i);
approx = approx + addterm;
new = approx;
i = i + 1;
end
end
x = 2*pi;
actual = cos(x);
disp(\"Original value: \");
disp(actual);
loop_tolerance = 0.00000000000001;
approx = cosineApprox(x,loop_tolerance);
disp(\"Approximate Value: \");
disp(approx);
%{
output:
Original value:
1
Enter number of terms: 5
Approximate Value:
1.00000
%}
