Taylor expansion of the function
basic knowledge
If we want to get sin(x)
the Taylor expansion, enter the following command:
>> syms x;
>> s = taylor(sin(x))
result:
s =
x^5/120 - x^3/6 + x
Although it seems that MATLAB
only 3 items were returned, it actually returned 5 items because MATLAB
only non-zero items were returned.
Now we draw the image:
Expansion order
To make the image of the polynomial expansion closer sin(x)
, we must MATLAB
return more terms. Suppose we want to get the m
term expansion, use the order
command, followed by the order we want to expand
s = taylor(sin(x),'order',m);
Let's try the first 20 items:
syms x;
s = taylor(sin(x),'order',10)
s =
- x^19/121645100408832000 + x^17/355687428096000 - x^15/1307674368000 + x^13/6227020800 - x^11/39916800 + x^9/362880 - x^7/5040 + x^5/120 - x^3/6 + x
image:
This time the image is smoother than last time.
Another problem is that the order of our expansion is from high to low, which is not what we want, we want to make it from low to high, you can use the sympref
command:
sympref('PolynomialDisplayStyle','ascend');
s =
x - x^3/6 + x^5/120 - x^7/5040 + x^9/362880 - x^11/39916800 + x^13/6227020800 - x^15/1307674368000 + x^17/355687428096000 - x^19/121645100408832000
Where to unfold
As we wish, the order is from low to high.
We know that the Taylor formula can be expanded at any point, and at the zero point we call the McLaurin formula.
So, we can also use the command 'ExpansionPoint'
to expand at the specified point
s = taylor(sin(x),'order',20,'ExpansionPoint',1)
s =
sin(1) - (sin(1)*(- 1 + x)^2)/2 + (sin(1)*(- 1 + x)^4)/24 - (sin(1)*(- 1 + x)^6)/720 + (sin(1)*(- 1 + x)^8)/40320 - (sin(1)*(- 1 + x)^10)/3628800 + (sin(1)*(- 1 + x)^12)/479001600 - (sin(1)*(- 1 + x)^14)/87178291200 + (sin(1)*(- 1 + x)^16)/20922789888000 - (sin(1)*(- 1 + x)^18)/6402373705728000 + cos(1)*(- 1 + x) - (cos(1)*(- 1 + x)^3)/6 + (cos(1)*(- 1 + x)^5)/120 - (cos(1)*(- 1 + x)^7)/5040 + (cos(1)*(- 1 + x)^9)/362880 - (cos(1)*(- 1 + x)^11)/39916800 + (cos(1)*(- 1 + x)^13)/6227020800 - (cos(1)*(- 1 + x)^15)/1307674368000 + (cos(1)*(- 1 + x)^17)/355687428096000 - (cos(1)*(- 1 + x)^19)/121645100408832000