Documentation Center

  • Trial Software
  • Product Updates

taylor

Taylor series expansion

Syntax

taylor(f)
taylor(f,Name,Value)
taylor(f,v)
taylor(f,v,Name,Value)
taylor(f,v,a)
taylor(f,v,a,Name,Value)

Description

taylor(f) computes the Taylor series expansion of f up to the fifth order. The expansion point is 0.

taylor(f,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

taylor(f,v) computes the Taylor series expansion of f with respect to v.

taylor(f,v,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

taylor(f,v,a) computes the Taylor series expansion of f with respect to v around the expansion point a.

taylor(f,v,a,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

Input Arguments

f

Symbolic expression.

v

Symbolic variable or vector of symbolic variables with respect to which you want to compute the Taylor series expansion.

Default: Symbolic variable or vector of symbolic variables of f determined by symvar.

a

Real number (including infinities and symbolic numbers) specifying the expansion point. For multivariate Taylor series expansions, use a vector of numbers.

Default: 0

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 single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'ExpansionPoint'

Specify the expansion point a. The value a is a scalar or a vector.

Default: If you specify the expansion point as a third argument a of taylor, then the value of that argument. Otherwise, 0.

'Order'

Specify the truncation order n, where n is a positive integer. taylor computes the Taylor polynomial approximation with the order n-1. The truncation order n is the exponent in the O-term: O(vn).

Default: 6

'OrderMode'

Specify whether you want to use absolute or relative order when computing the Taylor polynomial approximation. The value must be one of these strings: Absolute or Relative. Absolute order is the truncation order of the computed series. Relative order n means that the exponents of v in the computed series range from the leading order m to the highest exponent m + n - 1. Here m + n is the exponent of v in the O-term: O(vm + n).

Default: Absolute

Examples

Compute the Maclaurin series expansions of these functions:

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

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

syms x
taylor(log(x), x, 'ExpansionPoint', 1)
ans =
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:

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

Compute the Maclaurin series expansion for this function. The default truncation order is 6. Taylor series approximation of this function does not have a fifth-degree term, so taylor approximates this function with the fourth-degree polynomial:

syms x
f = sin(x)/x;
t6 = taylor(f)
t6 =
x^4/120 - x^2/6 + 1

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

t8 = taylor(f, 'Order', 8)
t10 = taylor(f, '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 function f and its approximations t6, t8, and t10. Note how the accuracy of the approximation depends on the truncation order.

plotT6 = ezplot(t6, [-4, 4]);
hold on
set(plotT6,'Color','red')

plotT8 = ezplot(t8, [-4, 4]);
set(plotT8,'Color','magenta')

plotT10 = ezplot(t10, [-4, 4]);
set(plotT10,'Color','cyan')

plotF = ezplot(f, [-4, 4]);
set(plotF,'Color','blue','LineWidth', 2)

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^1^0)',...
'sin(x)/x',...
'Location', 'South')

title('Taylor Series Expansion')
hold off

 

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

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

To compute the Taylor series expansion with a relative truncation order, use OrderMode. For some expressions, a relative truncation order provides more accurate approximations.

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

Compute the Maclaurin series expansion of this multivariate function. 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);
taylor(f)
ans =
x^5/120 - x^3/6 + x + cos(y) + exp(z)

Compute the multivariate Maclaurin expansion by specifying the vector of variables:

syms x y z
f = sin(x) + cos(y) + exp(z);
taylor(f, [x, y, z])
ans =
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
 

Compute 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);
taylor(f, [x, y], [1, 1], 'Order', 3)
ans =
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:

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

More About

expand 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:

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:

Tips

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

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

See Also

|

Was this topic helpful?