Taylor series in Matlab

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

p5 = taylor(f, 'Order', 6)

1 Comment

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

0 votes

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

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

0 votes

Ohh I see, that really sucks for me then. Is there anyway to get around that or not really?

5 Comments

Which MATLAB release are you using? Do you have access to Maple ?
R2016a is the version I am using. I'm not sure what Maple is to be honest. Where can I find if I have access to it?
What shows up for
which -all taylor
I suspect you will see just toolbox/symbolic/symbolic/@sym/taylor.m which is what would be expected if you have the MuPAD based Symbolic Toolbox. The result you are getting in descending order is the correct answer for the MuPAD based symbolic toolbox that has been the only symbolic toolbox Mathworks has sold in a decade. Is it possible that you are working with textbooks that are pretty much a decade or more old?
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.
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!