# taylor

## Description

example

T = taylor(f,var) approximates f with the Taylor series expansion of f up to the fifth order at the point var = 0. If you do not specify var, then taylor uses the default variable determined by symvar(f,1).

example

T = taylor(f,var,a) approximates f with the Taylor series expansion of f at the point var = a.

example

T = taylor(___,Name,Value) uses additional options specified by one or more Name,Value pair arguments. You can specify Name,Value after the input arguments in any of the previous syntaxes.

## Examples

collapse all

Find the Maclaurin series expansions of the exponential, sine, and cosine functions up to the fifth order.

syms x
T1 = taylor(exp(x))
T2 = taylor(sin(x))
T3 = taylor(cos(x))
T1 =
x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1

T2 =
x^5/120 - x^3/6 + x

T3 =
x^4/24 - x^2/2 + 1

You can use the sympref function to modify the output order of symbolic polynomials. Redisplay the polynomials in ascending order.

sympref('PolynomialDisplayStyle','ascend');
T1
T2
T3
T1 =
1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120

T2 =
x - x^3/6 + x^5/120

T3 =
1 - x^2/2 + x^4/24

The display format you set using sympref persists through your current and future MATLAB® sessions. Restore the default value by specifying the 'default' option.

sympref('default');

Find the Taylor series expansions at x = 1 for these functions. The default expansion point is 0. To specify a different expansion point, use ExpansionPoint:

syms x
T = taylor(log(x), x, 'ExpansionPoint', 1)
T =
x - (x - 1)^2/2 + (x - 1)^3/3 - (x - 1)^4/4 + (x - 1)^5/5 - 1

Alternatively, specify the expansion point as the third argument of taylor:

T = taylor(acot(x), x, 1)
T =
pi/4 - x/2 + (x - 1)^2/4 - (x - 1)^3/12 + (x - 1)^5/40 + 1/2

Find the Maclaurin series expansion for f = sin(x)/x. The default truncation order is 6. Taylor series approximation of this expression does not have a fifth-degree term, so taylor approximates this expression with the fourth-degree polynomial:

syms x
f = sin(x)/x;
T6 = taylor(f, x);

Use Order to control the truncation order. For example, approximate the same expression up to the orders 8 and 10:

T8 = taylor(f, x, 'Order', 8);
T10 = taylor(f, x, 'Order', 10);

Plot the original expression f and its approximations T6, T8, and T10. Note how the accuracy of the approximation depends on the truncation order.

fplot([T6 T8 T10 f])
xlim([-4 4])
grid on

legend('approximation of sin(x)/x up to O(x^6)',...
'approximation of sin(x)/x up to O(x^8)',...
'approximation of sin(x)/x up to O(x^{10})',...
'sin(x)/x','Location','Best')
title('Taylor Series Expansion')

Find the Taylor series expansion of this expression. By default, taylor uses an absolute order, which is the truncation order of the computed series.

T = taylor(1/(exp(x)) - exp(x) + 2*x, x, 'Order', 5)
T =
-x^3/3

Find the Taylor series expansion with a relative truncation order by using OrderMode. For some expressions, a relative truncation order provides more accurate approximations.

T = taylor(1/(exp(x)) - exp(x) + 2*x, x, 'Order', 5, 'OrderMode', 'relative')
T =
- x^7/2520 - x^5/60 - x^3/3

Find the Maclaurin series expansion of this multivariate expression. If you do not specify the vector of variables, taylor treats f as a function of one independent variable.

syms x y z
f = sin(x) + cos(y) + exp(z);
T = taylor(f)
T =
x^5/120 - x^3/6 + x + cos(y) + exp(z)

Find the multivariate Maclaurin expansion by specifying the vector of variables.

syms x y z
f = sin(x) + cos(y) + exp(z);
T = taylor(f, [x, y, z])
T =
x^5/120 - x^3/6 + x + y^4/24 - y^2/2 + z^5/120 + z^4/24 + z^3/6 + z^2/2 + z + 2

You can use the sympref function to modify the output order of a symbolic polynomial. Redisplay the polynomial in ascending order.

sympref('PolynomialDisplayStyle','ascend');
T
T =
2 + z + z^2/2 + z^3/6 + z^4/24 + z^5/120 - y^2/2 + y^4/24 + x - x^3/6 + x^5/120

The display format you set using sympref persists through your current and future MATLAB sessions. Restore the default value by specifying the 'default' option.

sympref('default');

Find the multivariate Taylor expansion by specifying both the vector of variables and the vector of values defining the expansion point:

syms x y
f = y*exp(x - 1) - x*log(y);
T = taylor(f, [x, y], [1, 1], 'Order', 3)
T =
x + (x - 1)^2/2 + (y - 1)^2/2

If you specify the expansion point as a scalar a, taylor transforms that scalar into a vector of the same length as the vector of variables. All elements of the expansion vector equal a:

T = taylor(f, [x, y], 1, 'Order', 3)
T =
x + (x - 1)^2/2 + (y - 1)^2/2

## Input Arguments

collapse all

Input to approximate, specified as a symbolic expression or function. It also can be a vector, matrix, or multidimensional array of symbolic expressions or functions.

Expansion variable, specified as a symbolic variable. If you do not specify var, then taylor uses the default variable determined by symvar(f,1).

Expansion point, specified as a number, or a symbolic number, variable, function, or expression. The expansion point cannot depend on the expansion variable. You also can specify the expansion point as a Name,Value pair argument. If you specify the expansion point both ways, then the Name,Value pair argument takes precedence.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: taylor(log(x),x,'ExpansionPoint',1,'Order',9)

Expansion point, specified as a number, or a symbolic number, variable, function, or expression. The expansion point cannot depend on the expansion variable. You can also specify the expansion point using the input argument a. If you specify the expansion point both ways, then the Name,Value pair argument takes precedence.

Truncation order of Taylor series expansion, specified as a positive integer or a symbolic positive integer. taylor computes the Taylor series approximation with the order n - 1. The truncation order n is the exponent in the O-term: O(varn).

Order mode indicator, specified as 'absolute' or 'relative'. This indicator specifies whether you want to use absolute or relative order when computing the Taylor polynomial approximation.

Absolute order is the truncation order of the computed series. Relative order n means that the exponents of var in the computed series range from the leading order m to the highest exponent m + n - 1. Here m + n is the exponent of var in the O-term: O(varm + n).

collapse all

### Taylor Series Expansion

Taylor series expansion represents an analytic function f(x) as an infinite sum of terms around the expansion point x = a:

$f\left(x\right)=f\left(a\right)+\frac{{f}^{\prime }\left(a\right)}{1!}\left(x-a\right)+\frac{{f}^{″}\left(a\right)}{2!}{\left(x-a\right)}^{2}+\dots =\sum _{m=0}^{\infty }\frac{{f}^{\left(m\right)}\left(a\right)}{m!}\cdot {\left(x-a\right)}^{m}$

Taylor series expansion requires a function to have derivatives up to an infinite order around the expansion point.

### Maclaurin Series Expansion

Taylor series expansion around x = 0 is called Maclaurin series expansion:

$f\left(x\right)=f\left(0\right)+\frac{{f}^{\prime }\left(0\right)}{1!}x+\frac{{f}^{″}\left(0\right)}{2!}{x}^{2}+\dots =\sum _{m=0}^{\infty }\frac{{f}^{\left(m\right)}\left(0\right)}{m!}{x}^{m}$

## Tips

• If you use both the third argument a and ExpansionPoint to specify the expansion point, the value specified via ExpansionPoint prevails.

• If var is a vector, then the expansion point a must be a scalar or a vector of the same length as var. If var is a vector and a is a scalar, then a is expanded into a vector of the same length as var with all elements equal to a.

• If the expansion point is infinity or negative infinity, then taylor computes the Laurent series expansion, which is a power series in 1/var.

• You can use the sympref function to modify the output order of symbolic polynomials.