diff

Differentiate symbolic expression or function

Description

example

Df = diff(f) differentiates f with respect to the symbolic variable determined by symvar(f,1).

example

Df = diff(f,n) computes the nth derivative of f with respect to the symbolic variable determined by symvar.

example

Df = diff(f,var) differentiates f with respect to the differentiation parameter var. var can be a symbolic variable, such as x, a symbolic function, such as f(x), or a derivative function, such as diff(f(t),t).

example

Df = diff(f,var,n) computes the nth derivative of f with respect to var.

example

Df = diff(f,var1,...,varN) differentiates f with respect to the parameters var1,...,varN.

Examples

collapse all

Find the derivative of the function sin(x^2).

syms f(x)
f(x) = sin(x^2);
Df = diff(f,x)
Df(x) = 2xcos(x2)2*x*cos(x^2)

Find the value of the derivative at x = 2. Convert the value to double.

Df2 = Df(2)
Df2 = 4cos(4)sym(4)*cos(sym(4))
double(Df2)
ans = -2.6146

Find the first derivative of this expression.

syms x t
Df = diff(sin(x*t^2))
Df = t2cos(t2x)t^2*cos(t^2*x)

Because you did not specify the differentiation variable, diff uses the default variable defined by symvar. For this expression, the default variable is x.

var = symvar(sin(x*t^2),1)
var = xx

Now, find the derivative of this expression with respect to the variable t.

Df = diff(sin(x*t^2),t)
Df = 2txcos(t2x)2*t*x*cos(t^2*x)

Find the 4th, 5th, and 6th derivatives of t6.

syms t
D4 = diff(t^6,4)
D4 = 360t2360*t^2
D5 = diff(t^6,5)
D5 = 720t720*t
D6 = diff(t^6,6)
D6 = 720sym(720)

Find the second derivative of this expression with respect to the variable y.

syms x y
Df = diff(x*cos(x*y), y, 2)
Df = -x3cos(xy)-x^3*cos(x*y)

Compute the second derivative of the expression x*y. If you do not specify the differentiation variable, diff uses the variable determined by symvar. For this expression, symvar(x*y,1) returns x. Therefore, diff computes the second derivative of x*y with respect to x.

syms x y
Df = diff(x*y,2)
Df = 0sym(0)

If you use nested diff calls and do not specify the differentiation variable, diff determines the differentiation variable for each call. For example, differentiate the expression x*y by calling the diff function twice.

Df = diff(diff(x*y))
Df = 1sym(1)

In the first call, diff differentiates x*y with respect to x, and returns y. In the second call, diff differentiates y with respect to y, and returns 1.

Thus, diff(x*y,2) is equivalent to diff(x*y,x,x), and diff(diff(x*y)) is equivalent to diff(x*y,x,y).

Differentiate this expression with respect to the variables x and y.

syms x y
Df = diff(x*sin(x*y),x,y)
Df = 2xcos(xy)-x2ysin(xy)2*x*cos(x*y) - x^2*y*sin(x*y)

You also can compute mixed higher-order derivatives by providing all differentiation variables.

syms x y
Df = diff(x*sin(x*y),x,x,x,y)
Df = x2y3sin(xy)-6xy2cos(xy)-6ysin(xy)x^2*y^3*sin(x*y) - 6*x*y^2*cos(x*y) - 6*y*sin(x*y)

Find the derivative of the function y=f(x)2dfdx with respect to f(x)

syms f(x) y
y = f(x)^2*diff(f(x),x);
Dy = diff(y,f(x))
Dy = 

2f(x)x f(x)2*f(x)*diff(f(x), x)

Find the 2nd derivative of the function y=f(x)2dfdx with respect to f(x)

Dy2 = diff(y,f(x),2)
Dy2 = 

2x f(x)2*diff(f(x), x)

Find the mixed derivative of the function y=f(x)2dfdx with respect to f(x) and dfdx.

Dy3 = diff(y,f(x),diff(f(x)))
Dy3 = 2f(x)2*f(x)

Find the Euler–Lagrange equation that describes the motion of a mass-spring system. Define the kinetic and potential energy of the system.

syms x(t) m k
T = m/2*diff(x(t),t)^2;
V = k/2*x(t)^2;

Define the Lagrangian.

L = T - V
L = 

mt x(t)22-kx(t)22(m*(diff(x(t), t))^2)/2 - (k*x(t)^2)/2

The Euler–Lagrange equation is given by

0=ddtL(t,x,x˙)x˙-L(t,x,x˙)x

Evaluate the term L/x˙.

D1 = diff(L,diff(x(t),t))
D1 = 

mt x(t)m*diff(x(t), t)

Evaluate the second term L/x.

D2 = diff(L,x)
D2(t) = -kx(t)-k*x(t)

Find the Euler–Lagrange equation of motion of the mass-spring system.

diff(D1,t) - D2 == 0
ans(t) = 

m2t2 x(t)+kx(t)=0m*diff(x(t), t, 2) + k*x(t) == 0

Input Arguments

collapse all

Expression or function to differentiate, specified as a symbolic expression or function or as a vector or matrix of symbolic expressions or functions. If f is a vector or a matrix, diff differentiates each element of f and returns a vector or a matrix of the same size as f.

Differentiation parameter, specified as a symbolic variable, symbolic function, or a derivative diff function.

If you specify differentiation with respect to a symbolic function var = f(x) or the derivative function var = diff(f(x),x), then the first argument f must not contain:

  • integral transforms, such as fourier, ifourier, laplace, ilaplace, htrans, ihtrans, ztrans, and iztrans

  • unevaluated symbolic expressions that include limit or int

  • symbolic functions evaluated at certain points, such as f(2) or g(0)

Differentiation parameters, specified as symbolic variables, symbolic functions, or symbolic diff functions.

Differentiation order, specified as a nonnegative integer.

Tips

  • When computing mixed higher-order derivatives with more than one variable, do not use n to specify the differentiation order. Instead, specify all differentiation variables explicitly.

  • To improve performance, diff assumes that all mixed derivatives commute. For example,

    xyf(x,y)=yxf(x,y)

    This assumption suffices for most engineering and scientific problems.

  • If you differentiate a multivariate expression or function f without specifying the differentiation variable, then a nested call to diff and diff(f,n) can return different results. This is because in a nested call, each differentiation step determines and uses its own differentiation variable. In calls like diff(f,n), the differentiation variable is determined once by symvar(f,1) and used for all differentiation steps.

  • If you differentiate an expression or function containing abs or sign, ensure that the arguments are real values. For complex arguments of abs and sign, the diff function formally computes the derivative, but this result is not generally valid because abs and sign are not differentiable over complex numbers.

Introduced before R2006a