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 Kommentar

Ruben Montoya
Ruben Montoya am 1 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Nicolas Vayas
Nicolas Vayas am 14 Feb. 2018

2 Stimmen

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 am 13 Feb. 2012

0 Stimmen

Why are you looping over "i" and then in the loop setting "i = n+1" ?
Matias Campos
Matias Campos am 14 Feb. 2018
Bearbeitet: Matias Campos am 14 Feb. 2018

0 Stimmen

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 Kommentar

Matias Campos
Matias Campos am 14 Feb. 2018
Bearbeitet: Matias Campos am 14 Feb. 2018
This should work, you had a slight error on the last for loop.

Melden Sie sich an, um zu kommentieren.

Fawaz Hjouj
Fawaz Hjouj am 15 Sep. 2019

0 Stimmen

What happen if we try
mycos1(100,100)

1 Kommentar

Walter Roberson
Walter Roberson am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Elementary Math finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 13 Feb. 2012

Kommentiert:

am 15 Sep. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by