General Taylor Polynomial code
10 views (last 30 days)
Show older comments
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 Comments
John D'Errico
on 11 Feb 2016
Edited: John D'Errico
on 11 Feb 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?
Answers (2)
Walter Roberson
on 11 Feb 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.
0 Comments
John D'Errico
on 11 Feb 2016
Edited: John D'Errico
on 11 Feb 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 Comments
Walter Roberson
on 14 Feb 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)), ...]
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!