# 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) specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. For example, you can specify the expansion point, truncation order, or order mode of the Taylor series expansion.

## 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))
T1 =

$\frac{{x}^{5}}{120}+\frac{{x}^{4}}{24}+\frac{{x}^{3}}{6}+\frac{{x}^{2}}{2}+x+1$

T2 = taylor(sin(x))
T2 =

$\frac{{x}^{5}}{120}-\frac{{x}^{3}}{6}+x$

T3 = taylor(cos(x))
T3 =

$\frac{{x}^{4}}{24}-\frac{{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
T1 =

$1+x+\frac{{x}^{2}}{2}+\frac{{x}^{3}}{6}+\frac{{x}^{4}}{24}+\frac{{x}^{5}}{120}$

T2
T2 =

$x-\frac{{x}^{3}}{6}+\frac{{x}^{5}}{120}$

T3
T3 =

$1-\frac{{x}^{2}}{2}+\frac{{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 $\mathit{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-\frac{{\left(x-1\right)}^{2}}{2}+\frac{{\left(x-1\right)}^{3}}{3}-\frac{{\left(x-1\right)}^{4}}{4}+\frac{{\left(x-1\right)}^{5}}{5}-1$

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

T = taylor(acot(x),x,1)
T =

$\frac{\pi }{4}-\frac{x}{2}+\frac{{\left(x-1\right)}^{2}}{4}-\frac{{\left(x-1\right)}^{3}}{12}+\frac{{\left(x-1\right)}^{5}}{40}+\frac{1}{2}$

Find the Maclaurin series expansion for f = sin(x)/x. The default truncation order is 6. The 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 7 and 9.

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 with error O(x^6)', ...
'approximation of sin(x)/x with error O(x^8)', ...
'approximation of sin(x)/x with error 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.

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

$-\frac{{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 =

$-\frac{{x}^{7}}{2520}-\frac{{x}^{5}}{60}-\frac{{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 =

$\frac{{x}^{5}}{120}-\frac{{x}^{3}}{6}+x+\mathrm{cos}\left(y\right)+{\mathrm{e}}^{z}$

Find the multivariate Maclaurin series 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 =

$\frac{{x}^{5}}{120}-\frac{{x}^{3}}{6}+x+\frac{{y}^{4}}{24}-\frac{{y}^{2}}{2}+\frac{{z}^{5}}{120}+\frac{{z}^{4}}{24}+\frac{{z}^{3}}{6}+\frac{{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+\frac{{z}^{2}}{2}+\frac{{z}^{3}}{6}+\frac{{z}^{4}}{24}+\frac{{z}^{5}}{120}-\frac{{y}^{2}}{2}+\frac{{y}^{4}}{24}+x-\frac{{x}^{3}}{6}+\frac{{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 series 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+\frac{{\left(x-1\right)}^{2}}{2}+\frac{{\left(y-1\right)}^{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+\frac{{\left(x-1\right)}^{2}}{2}+\frac{{\left(y-1\right)}^{2}}{2}$

Find the error estimate when approximating a function $f\left(x\right)=\mathrm{log}\left(x+1\right)$ using the Taylor series expansion. Here, consider the Taylor approximation up to the 7th order (with the truncation order $\mathit{n}=8$) at the expansion point $\mathit{a}=0$.

The error or remainder in the Taylor approximation is given by the Lagrange form:

${\mathit{R}}_{\mathit{n}-1}\left(\mathit{x}\right)=\frac{{\mathit{f}}^{\mathit{n}}\left(\mathit{c}\right)}{\mathit{n}!}{\left(\mathit{x}-\mathit{a}\right)}^{\mathit{n}}.$

The upper bound of the error estimate can be calculated by finding a positive real number $\mathit{M}$ such that $|{\mathit{f}}^{\mathit{n}}\left(\mathit{c}\right)|\le \mathit{M}$ for all $\mathit{c}$ between $\mathit{a}$ and $\mathit{x}$.

Find the Taylor series expansion of the function $f\left(x\right)=\mathrm{log}\left(x+1\right)$ up to the 7th order by specifying Order as 8.

syms x
f = log(x+1)
f = $\mathrm{log}\left(x+1\right)$
T = taylor(f,'Order',8)
T =

$\frac{{x}^{7}}{7}-\frac{{x}^{6}}{6}+\frac{{x}^{5}}{5}-\frac{{x}^{4}}{4}+\frac{{x}^{3}}{3}-\frac{{x}^{2}}{2}+x$

To estimate the error in the Taylor approximation, first compute the term ${\mathit{f}}^{8}\left(\mathit{c}\right)$.

syms c
fn(c) = subs(diff(f,8),x,c)
fn(c) =

$-\frac{5040}{{\left(c+1\right)}^{8}}$

For positive values of $\mathit{x}$, the upper bound of the error estimate can be calculated by using the relation $|{\mathit{f}}^{8}\left(\mathit{c}\right)|\le 5040$ (because $\mathit{c}$ must be a positive value between $0$ and a positive $\mathit{x}$). Next, find the upper bound of the error estimate Rupper(x) by using the Lagrange from ${\mathit{R}}_{7}\left(\mathit{x}\right)$ and the relation $|{\mathit{f}}^{8}\left(\mathit{c}\right)|\le 5040$.

Rupper(x) = 5040*x^8/factorial(8)
Rupper(x) =

$\frac{{x}^{8}}{8}$

Evaluate the Taylor series expansion at the point $\mathit{x}=0.5$. Find the upper bound of the error estimate in the Taylor approximation.

Teval = subs(T,x,0.5)
Teval =

$\frac{909}{2240}$

Rmax = double(Rupper(0.5))
Rmax = 4.8828e-04

For comparison, evaluate the exact function at $\mathit{x}=0.5$ and find the remainder in the Taylor approximation.

feval = subs(f,x,0.5)
feval =

$\mathrm{log}\left(\frac{3}{2}\right)$

R = double(abs(feval-Teval))
R = 3.3846e-04

## 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 argument. If you specify the expansion point both ways, then the name-value argument takes precedence.

### Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

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 argument takes precedence.

Truncation order of the 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 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

A 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}$

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

### Maclaurin Series Expansion

The Taylor series expansion around x = 0 is called a 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, then the value specified by 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.

• If taylor cannot find the Taylor series expansion, then use series to find the more general Puiseux series expansion.

## Version History

Introduced before R2006a