How can I ensure that the first order term of my Taylor series expansion about some expansion point is actually displayed as such?

4 views (last 30 days)
As a random exercise I have played around with MATLAB to create a script that would take in as inputs some function, a specified order, and an expansion point and output a plot with the original specified function and the taylor series approximations of order 1, 2, ..., n = specified order.
The issue that I have now is that MATLAB doesn't seem to display the Taylor series expansion quite how I specified it. Below is a snippet of what I mean.
Code
syms x
x_p=7.1;
n=4;
f=log(x)+(x/9)-2;
tf=taylor(f,x,'ExpansionPoint',x_p,'Order',n);
sympref('PolynomialDisplayStyle','ascend');
tf
Desired output
tf =
log(71/10) - 109/90 + (161*(-71/10 + x)/639 + (50*(- 71/10 + x)^2)/5041 + (1000*(- 71/10 + x)^3)/1073733
Actual output
tf =
(161*x)/639 + log(71/10) - (50*(- 71/10 + x)^2)/5041 + (1000*(- 71/10 + x)^3)/1073733 - 3
Note that the actual output does two things that I do not want it to;
  1. The first order term (x^1 term) is not displayed about the expansion point
  2. The zeroth order term is displayed separately (appearing 2nd from left and 1st from right as opposed to combined at the very beginning). Additionaly note that due to point 1. the zeroth order term is different to compensate for the lack of -71/10 in the first order term.
Help on this issue would be much appreciated!

Accepted Answer

Walter Roberson
Walter Roberson on 1 Oct 2020
You will need to use children() to extract the parts of the taylor series, and re-order the parts the way you want, and then display them, such as by using char() on the parts and joining the text together with ' + ' or ' - ' as appropriate.
Which is to say that you have no control over the order that the Symbolic Toolbox uses. The order it uses can be ... strange. The order it uses is not determined entirely by the powers (e.g. ascending or descending polynomial orders): there are cases involving division where the leading term of the denominator is chosen as the one with the first non-negative coefficient, no matter what power it is.
  2 Comments
Haziq Yazid
Haziq Yazid on 1 Oct 2020
Edited: Haziq Yazid on 1 Oct 2020
I was going to comment that I tried that out and found that coeffs() was a better option, looks like you beat me to it! Thank you for the answer and stay safe!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!