diff

Differentiate symbolic expression or function

Syntax

Description

example

diff(F) differentiates F with respect to the variable determined by symvar.

example

diff(F,var) differentiates F with respect to the variable var.

example

diff(F,n) computes the nth derivative of F with respect to the variable determined by symvar.

example

diff(F,var,n) computes the nth derivative of F with respect to the variable var. This syntax is equivalent to diff(F,n,var).

example

diff(F,var1,...,varN) differentiates F with respect to the variables var1,...,varN.

Examples

Differentiation of a Univariate Function

Find the first derivative of this univariate function:

syms x
f(x) = sin(x^2);
df = diff(f)
df(x) =
2*x*cos(x^2)

Differentiation with Respect to a Particular Variable

Find the first derivative of this expression:

syms x t
diff(sin(x*t^2))
ans =
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:

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

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

diff(sin(x*t^2),t)
ans =
2*t*x*cos(t^2*x)

Higher-Order Derivatives of a Univariate Expression

Find the 4th, 5th, and 6th derivatives of this expression:

syms t
d4 = diff(t^6,4)
d5 = diff(t^6,5)
d6 = diff(t^6,6)
d4 =
360*t^2
 
d5 =
720*t
 
d6 =
720

Higher-Order Derivatives of a Multivariate Expression with Respect to a Particular Variable

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

syms x y
diff(x*cos(x*y), y, 2)
ans =
-x^3*cos(x*y)

Higher-Order Derivatives of a Multivariate Expression with Respect to the Default Variable

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
diff(x*y, 2)
ans =
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:

diff(diff(x*y))
ans =
1

In the first call, diff differentiate 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).

Mixed Derivatives

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

syms x y
diff(x*sin(x*y), x, y)
ans =
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
diff(x*sin(x*y), x, x, x, y)
ans =
x^2*y^3*sin(x*y) - 6*x*y^2*cos(x*y) - 6*y*sin(x*y)

Input Arguments

expand all

F — Expression or function to differentiatesymbolic expression | symbolic function | symbolic vector | symbolic matrix

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.

var — Differentiation variablesymbolic variable | string

Differentiation variable, specified as a symbolic variable or a string.

var1,...,varN — Differentiation variablessymbolic variables | strings

Differentiation variables, specified as symbolic variables or strings.

n — Differentiation ordernonnegative integer

Differentiation order, specified as a nonnegative integer.

More About

expand all

Tips

  • When computing mixed higher-order derivatives, 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.

Was this topic helpful?