Main Content

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

example

Df = diff(f,mvar) differentiates f with respect to the symbolic matrix variable mvar of type symmatrix. (since R2021a)

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)

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

Df2 = Df(2)
Df2 = 4cos(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)

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 = x

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

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

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

syms t
D4 = diff(t^6,4)
D4 = 360t2
D5 = diff(t^6,5)
D5 = 720t
D6 = diff(t^6,6)
D6 = 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)

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

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)

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)

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)

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)

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

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)

Evaluate the second term L/x.

D2 = diff(L,x)
D2(t) = -kx(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)=0

Since R2021a

To evaluate derivatives with respect to vectors, you can use symbolic matrix variables. For example, find the derivatives α/x and α/y for the expression α=yTAx, where y is a 3-by-1 vector, A is a 3-by-4 matrix, and x is a 4-by-1 vector.

Create three symbolic matrix variables x, y, and A, of the appropriate sizes, and use them to define alpha.

syms x [4 1] matrix
syms y [3 1] matrix
syms A [3 4] matrix
alpha = y.'*A*x
alpha = yTAx

Find the derivative of alpha with respect to the vectors x and y.

Dx = diff(alpha,x)
Dx = yTA
Dy = diff(alpha,y)
Dy = xTAT

Since R2021a

To evaluate differential with respect to matrix, you can use symbolic matrix variables. For example, find the differential Y/A for the expression Y=XTAX, where X is a 3-by-1 vector, and A is a 3-by-3 matrix. Here, Y is a scalar that is a function of the vector X and the matrix A.

Create two symbolic matrix variables to represent X and A. Define Y.

syms X [3 1] matrix
syms A [3 3] matrix
Y = X.'*A*X
Y = XTAX

Find the differential of Y with respect to the matrix A.

D = diff(Y,A)
D = XTX

The result is a Kronecker tensor product between XT and X, which is a 3-by-3 matrix.

size(D)
ans = 1×2

     3     3

Input Arguments

collapse all

Expression or function to differentiate, specified as

  • a symbolic expression

  • a symbolic function

  • a vector or a matrix of symbolic expressions or functions (a symbolic vector or a symbolic matrix)

  • a symbolic matrix variable (since R2021a)

If f is a symbolic vector or matrix, diff differentiates each element of f and returns a vector or a matrix of the same size as f.

Data Types: single | double | sym | symfun | symmatrix

Differentiation parameter, specified as a symbolic scalar variable, symbolic function, or a derivative function created using the diff function.

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

  • 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 a specific point, such as f(3) or g(0)

Data Types: single | double | sym | symfun

Differentiation parameters, specified as symbolic scalar variables, symbolic functions, or derivative function created using the diff function.

Data Types: single | double | sym | symfun

Since R2021a

Differentiation parameter, specified as a symbolic matrix variable.

The diff function currently does not support tensor derivatives. If the derivative is a tensor, or the derivative is a matrix in terms of tensors, then the diff function will error. If f is a differentiable scalar function, mvar can be a scalar, vector or matrix. For further examples, see Differentiate With Respect to Vectors and Differentiate With Respect to Matrix.

Data Types: symmatrix

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