Taylor series in Matlab
4 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Walter Roberson
on 10 May 2018
p5 = taylor(f, 'Order', 6)
1 Comment
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.
More Answers (3)
John Smith
on 11 May 2018
1 Comment
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.
John Smith
on 11 May 2018
5 Comments
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.)
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)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!