Let the future tell the truth and evaluate each one according to his work and accomplishments. The present is theirs; the future, for which I really worked, is mine. (Nikola Tesla)

12/06/2011

● Calculating cos(x) Using Taylor Series Expansion on MATLAB

This program evaluates cosine function using "Taylor Series Expansion".
x=input('x= '); %Number that we will calculate its cosine
N=input('N= '); %Number of iteraritons
cosx=1; %Initial value cos(0)
truecos=cos(x) %True value of cos(x) calculated by Matlab command

for k=2:N
fact=1; %Initial value to calculate factorial
for i=1:(2*(k-1)) %for loop to calculate required factorial
fact=fact*i;
end
cosx=cosx+(-1)^(k+1)*x^(2*(k-1))/fact %calculation of cos(x)
error=(truecos-cosx)/truecos*100 %calculation of relative error
end


ShareThis