MATLAB cos(x) Taylor Series

79 views (last 30 days)
Alexandre Marruco
Alexandre Marruco on 13 Feb 2012
Commented: Walter Roberson on 15 Sep 2019
I'm working on a Taylor Series expansion for the cox(x) function. A value is returned for all non-negative integer values of n, but no matter how many terms I have the program provide the sum always approaches ans=1, reaching it by around n=5. The code I have right now is this:
function cosx = mycos1(x,n)
%Evaluate and sum the first n terms of the cosx Taylor Series.
if n~=round(n)
disp('Error: n must be an integer value.')
cosx = 0;
return
end
if n<0
disp('Error: n must be a non-negative value.')
cosx = 0;
return
end
cosx=1;
i=0;
for i=0:n+1
i = n+1;
addterm = ((-1)^i)*(x^(2*i))/factorial(2*i);
cosx = cosx + addterm;
end
end
Can someone find my error? Everything works other than the sum always approaching a value of 1. Thanks!
  1 Comment
Ruben Montoya
Ruben Montoya on 1 Mar 2019
El script es erroneo, no se puede poner cosx=1 porque suma los calculos a uno y si el resultado del desarrollo en 0, el resultado que muestra el programa es 0+1=1. Por ejemplo para el coseno de pi/2.

Sign in to comment.

Answers (4)

Nicolas Vayas
Nicolas Vayas on 14 Feb 2018
The i=0 and i=n+1 in lines 14 and 16, respectively, are unnecessary (such as Matias suggests, but add another unnecessary if statement): the for loop runs each iteration assigning integer values between your boundaries. Furthermore, the Taylor series expansion of cos(x), has the leading term of '1'. This means that the approximation of the cos(x) for any x using 1 term is equal to 1. When you ran mycos1(x, 1), the return should always be 1. To fix this error, add n = n-1 before your first if statement. This should end up like:
function cosx = mycos1(x,n)
%Evaluate and sum the first n terms of the cosx Taylor Series.
n = n-1;
if n~=round(n)
disp('Error: n must be an integer value.')
cosx = 0;
return
end
if n<0
disp('Error: n must be a non-negative value.')
cosx = 0;
return
end
cosx=1;
% i=0;
for i=1:n
% i = n+1;
addterm = ((-1)^i)*(x^(2*i))/factorial(2*i);
cosx = cosx + addterm;
end
end

Walter Roberson
Walter Roberson on 13 Feb 2012
Why are you looping over "i" and then in the loop setting "i = n+1" ?

Matias Campos
Matias Campos on 14 Feb 2018
Edited: Matias Campos on 14 Feb 2018
function cosx = mycos1(x,n)
%Evaluate and sum the first n terms of the cosx Taylor Series.
if n~=round(n)
disp('Error: n must be an integer value.')
cosx = 0;
return
end
if n<0
disp('Error: n must be a non-negative value.')
cosx = 0;
return
end
if n == 1
cosx = 1;
return
end
cosx=1;
for i =1:n
addterm = ((-1)^i)*(x^(2*i))/factorial(2*(i));
cosx = cosx + addterm;
end
end
  1 Comment
Matias Campos
Matias Campos on 14 Feb 2018
Edited: Matias Campos on 14 Feb 2018
This should work, you had a slight error on the last for loop.

Sign in to comment.


Fawaz Hjouj
Fawaz Hjouj on 15 Sep 2019
What happen if we try
mycos1(100,100)
  1 Comment
Walter Roberson
Walter Roberson on 15 Sep 2019
You can get a big number.
taylor() gets increasingly inaccurate as you get away from the point that the taylor series was approximated around. The order 100 term is going to be proportional to x^100/100! and for x near 100 that is going to be about 1E+42

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!