Documentation Center |
Taylor series expansion
taylor(f)
taylor(f,Name,Value)
taylor(f,v)
taylor(f,v,Name,Value)
taylor(f,v,a)
taylor(f,v,a,Name,Value)
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.
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 |
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.
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