Main Content

taylor

Description

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.

example

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 = 

x5120+x424+x36+x22+x+1

T2 = taylor(sin(x))
T2 = 

x5120-x36+x

T3 = taylor(cos(x))
T3 = 

x424-x22+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+x22+x36+x424+x5120

T2
T2 = 

x-x36+x5120

T3
T3 = 

1-x22+x424

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-122+x-133-x-144+x-155-1

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

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

π4-x2+x-124-x-1312+x-1540+12

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")

Figure contains an axes object. The axes object with title Taylor Series Expansion contains 4 objects of type functionline. These objects represent 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.

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 = 

-x33

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 = 

-x72520-x560-x33

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 = 

x5120-x36+x+cos(y)+ez

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 = 

x5120-x36+x+y424-y22+z5120+z424+z36+z22+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+z22+z36+z424+z5120-y22+y424+x-x36+x5120

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+x-122+y-122

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-122+y-122

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

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

Rn-1(x)=fn(c)n!(x-a)n.

The upper bound of the error estimate can be calculated by finding a positive real number M such that |fn(c)|M for all c between a and x.

Find the Taylor series expansion of the function f(x)=log(x+1) up to the 7th order by specifying Order as 8.

syms x
f = log(x+1)
f = log(x+1)
T = taylor(f,Order=8)
T = 

x77-x66+x55-x44+x33-x22+x

To estimate the error in the Taylor approximation, first compute the term f8(c).

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

-5040c+18

For positive values of x, the upper bound of the error estimate can be calculated by using the relation |f8(c)|5040 (because c must be a positive value between 0 and a positive x). Next, find the upper bound of the error estimate Rupper(x) by using the Lagrange from R7(x) and the relation |f8(c)|5040.

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

x88

Evaluate the Taylor series expansion at the point x=0.5. Find the upper bound of the error estimate in the Taylor approximation.

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

9092240

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

For comparison, evaluate the exact function at x=0.5 and find the remainder in the Taylor approximation.

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

log(32)

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.

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

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

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).

More About

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(x)=f(a)+f(a)1!(xa)+f(a)2!(xa)2+=m=0f(m)(a)m!(xa)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(x)=f(0)+f(0)1!x+f(0)2!x2+=m=0f(m)(0)m!xm

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