Write a MATLAB program that determines cosx using taylor ser
Write a MATLAB program that determines cos(x) using taylor series expansion. The program asks the user to type a value for an angle in degrees. Then the program uses a loop fo adding the terms of the taylor series. If An is the nth term in the series, then the sum Sn of the n terms is Sn=Sn-1+An. In each pass calculate the estimated error E given by E=abs(Sn-Sn-1/Sn-1). Stop adding terms when E is less then or equal to .000001. The program displays the value of cos(x). Use the program for calculating: a)cos(35) and b)sin(125).
Solution
clc
clear
x = input(\'type value of angle in degrees:\ \');
x = x*pi/180; %converting from degree to radian
cos_x = 1; %as first term of Taylor series is x
E = 1; %just giving a value of error greater than desired error
n = 0;
while E > 0.000001
previous = cos_x;
n = n+1;
cos_x = cos_x + ((-1)^n)*(x^(2*n))/factorial(2*n);
E = abs(cos_x - previous); %calculating error
end
a = sprintf(\'cos(x) = %1.6f\',cos_x);
disp(a)
