Taylor series in Matlab
Show older comments
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
More Answers (3)
John Smith
on 11 May 2018
0 votes
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
0 votes
5 Comments
Walter Roberson
on 11 May 2018
Which MATLAB release are you using? Do you have access to Maple ?
John Smith
on 12 May 2018
Walter Roberson
on 12 May 2018
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?
John Smith
on 12 May 2018
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)
Categories
Find more on Numeric Solvers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!