taylor

Syntax

T = taylor(f,var)
T = taylor(f,var,a)
T = taylor(___,Name,Value)

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)
T6 =
x^4/120 - x^2/6 + 1

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)
T8 =
- x^6/5040 + x^4/120 - x^2/6 + 1
 
T10 =
x^8/362880 - x^6/5040 + x^4/120 - x^2/6 + 1

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

More About

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

Introduced before R2006a