General Taylor Polynomial code

10 vues (au cours des 30 derniers jours)
cee878
cee878 le 11 Fév 2016
Commenté : Walter Roberson le 14 Fév 2016
I'm trying to create a general code for evaluating Taylor polynomials for f(x)=x-sin(x)/x^3 and plot their relative error vs n iterations. I ran the code but I kept getting a N must be non-negative integers. Now, I'm confused. Also, I'm pretty new at Matlab. Some help would be great.
function[] = Taylor4(n,x)
n = input('Degree: ');
x = input('Value of x: ');
f=(1/x^2)-(sin(x)/x^3);
terms = zeros(1, n+1);
for j = 0:1:n
terms(j+1)=(-1).^(j+1)*(x.^(2j-2))/factorial(2j+1);
end;
%display(terms)
termsSum = cumsum(terms); %holds the estimate
termsSum1 = sum(terms);
display('The iteration: ')
display(terms)
display('The approximate estimate at x = -20: ')
display(termsSum1)
%true value at x=-20
display('TRUE VALUE')
display(f)
%absolute error between the approximated and true value
t = exp(x);
y = termsSum.^sign(x);
h = abs(t-y);
display('THE RELATIVE ERROR: ')
display(abs(f-termsSum))
%plot the Taylor approximation and exact value
semilogy(0:n,h); hold on %the Taylor polyonmial estimate when n = 10
semilogy(x,exp(x),'*') %exact value at x = -20
  2 commentaires
John D'Errico
John D'Errico le 11 Fév 2016
Modifié(e) : John D'Errico le 11 Fév 2016
Why would you create a function that has input arguments of n and x, and THEN use input on those same variables inside the function???????????
As far as your error goes, N never appears in that code. So we cannot guess what you did.
Show how you called the function, else how can we help you?
Star Strider
Star Strider le 12 Fév 2016

General Zachary Taylor, Hero of the Mexican War?

Connectez-vous pour commenter.

Réponses (2)

Walter Roberson
Walter Roberson le 11 Fév 2016
You have
terms(j+1)=(-1).^(j+1)*(x.^(2j-2))/factorial(2j+1);
In MATLAB, the syntax 2j+1 means complex(1,2) -- that is, i or j immediately following a number indicates the number is to be multiplied by the imaginary constant to form a complex number.
MATLAB has no implicit multiplication of variables. If you want to multiply a variable by a value, you need to use either * or .* to indicate the multiplication.

John D'Errico
John D'Errico le 11 Fév 2016
Modifié(e) : John D'Errico le 11 Fév 2016
Other questions abound. Do you really want to approximate the function:
f(x)=x-sin(x)/x^3
That is what you wrote after all. Or do you want
f(x) = (1/x^2)-(sin(x)/x^3)
Which is what your code shows?
I'll assume the latter, since it is more interesting. Perhaps you just don't appreciate the need for parens in the first case.
In that event, then a quick check of your series approximation suggests you got that wrong.
syms x
f=(1/x^2)-(sin(x)/x^3);
taylor(f,'order',8)
ans =
- x^6/362880 + x^4/5040 - x^2/120 + 1/6
If we look at the j==0 term in your series approximation...
(-1).^(j+1)*(x.^(2*j-2))/factorial(2*j+1)
(after fixing the 2j issue) when j == 0, this reduces to
-1/factorial(1)
Clearly not 1/6. So you want to carefully re-derive that series approximation. Yes, I could do it for you, but that would take all the fun out of it for you. :)
  6 commentaires
cee878
cee878 le 13 Fév 2016
Okay, I fixed it. However, conceptually I still don't really get what's the difference between "cumsum" and "sum". Could you explain that a little in my code?
Walter Roberson
Walter Roberson le 14 Fév 2016
v = [2 5 8];
sum(v)
would be 2+5+8 which would be the single value 15.
cumsum(v)
would be [(2), (2+5), (2+5+8)] = [2 7 15], a vector, in which each position is the cumulative total of the original vector, [sum(v(1)), sum(v(1:2)), sum(v(1:3)), ...]

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by