Taylor series in Matlab

4 views (last 30 days)
John Smith
John Smith on 10 May 2018
Edited: Pasindu Ranasinghe on 1 Dec 2020
I am taking a MATLAB class and the instructions given to run a taylor series example test run is not working:
To get the Taylor Polynomial of Degree 5:
>> syms f x p5
>> f=log(1+x)
>> p5=taylor(f,6)
p5 = x - 1/2*x^2 + 1/3*x^3 - 1/4*x^4 + 1/5*x^5 % p5 is shorthand for P5(x)
This is the example code is gives me to test, but when I try to use it, it returns this error code:
Error using sym/taylor (line 99)
The value of 'x' is invalid. It must satisfy the function:
@(x)isvector(x)&&isAllVars(x).
What is the correct instruction to get this example to work?

Accepted Answer

Walter Roberson
Walter Roberson on 10 May 2018
p5 = taylor(f, 'Order', 6)
  1 Comment
Walter Roberson
Walter Roberson on 10 May 2018
Your example does not appear to be for the MuPAD Symbolic Toolbox. When I check in sufficiently old versions of MATLAB, I do find that taylor(f,6) was accepted, but the coefficients were ordered in reducing degree, as is the case in newer versions of MATLAB. I do not happen to have a version before R2011a handy to test against though.

Sign in to comment.

More Answers (3)

John Smith
John Smith on 11 May 2018
p5= x - 1/2 x^2 + 1/3 x^3 - 1/4 x^4 +1/5x^5 Is what the example said was supposed to be returned.
So I just used your correction, and this is what I got back. I'm using 2017a matlab i believe:
>> p5 = taylor(f, 'Order', 6) p5 = x^5/5 - x^4/4 + x^3/3 - x^2/2 + x
Why is it reversed?
  1 Comment
Walter Roberson
Walter Roberson on 11 May 2018
The order is highest degree to lowest in every version of the MuPAD based Symbolic Toolbox that I checked. The order shown in the example would be for the Maple based Symbolic Toolbox, which has not been available from Mathworks since R2007b or so.

Sign in to comment.


John Smith
John Smith on 11 May 2018
Ohh I see, that really sucks for me then. Is there anyway to get around that or not really?
  5 Comments
John Smith
John Smith on 12 May 2018
Yes that showed up, and yes I believe the textbook I'm looking at is at least a decade old lol. I will let my professor know that the example doesn't work with the newer version of Matlab. Thank you.
Walter Roberson
Walter Roberson on 12 May 2018
The result is the same, just the order is different. When you are using symbolic toolboxes, you should seldom count on the order of the parts of commutative expressions, as symbolic toolboxes often reorder for internal efficiency reasons (or sometimes because they just have strange ideas about what 'looks' better.)

Sign in to comment.


Pasindu Ranasinghe
Pasindu Ranasinghe on 1 Dec 2020
Edited: Pasindu Ranasinghe on 1 Dec 2020
Function
function [TS_Approx] = taylorSeries(Fun,a,N)
syms x;
TS_Approx = Fun(a);
for n = 1:N
derivative = diff(Fun(x),n);
TS_Approx = TS_Approx + (subs(derivative,a)*(x-a)^n/factorial(n));
end
end
Caling the function
taylorSeries(@(x)(1/x),1,3)
But Simpliy you can use the inbuilt taylor function to make it even easier
syms x
TS=taylor(1/x,x,1,"order",4)

Community Treasure Hunt

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

Start Hunting!