Differentiate symbolic expression or function
Df = diff(
f with respect to the symbolic scalar
variable determined by
Find the derivative of the function
syms f(x) f(x) = sin(x^2); Df = diff(f,x)
Find the value of the derivative at
x = 2. Convert the value to
Df2 = Df(2)
ans = -2.6146
Differentiate with Respect to Particular Variable
Find the first derivative of this expression.
syms x t Df = diff(sin(x*t^2))
Because you did not specify the differentiation variable,
diff uses the default variable defined by
symvar. For this expression, the default variable is
var = symvar(sin(x*t^2),1)
Now, find the derivative of this expression with respect to the variable
Df = diff(sin(x*t^2),t)
Higher-Order Derivatives of Univariate Expression
Find the 4th, 5th, and 6th derivatives of .
syms t D4 = diff(t^6,4)
D5 = diff(t^6,5)
D6 = diff(t^6,6)
Higher-Order Derivatives of Multivariate Expression with Respect to Particular Variable
Find the second derivative of this expression with respect to the variable
syms x y Df = diff(x*cos(x*y), y, 2)
Higher-Order Derivatives of Multivariate Expression with Respect to 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,
diff computes the second derivative of
x*y with respect to
syms x y Df = diff(x*y,2)
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))
In the first call,
x*y with respect to
x, and returns
y. In the second call,
y with respect to
y, and returns
diff(x*y,2) is equivalent to
diff(diff(x*y)) is equivalent to
Differentiate this expression with respect to the variables
syms x y Df = diff(x*sin(x*y),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)
Differentiate with Respect to Function and Derivative
Find the derivative of the function with respect to .
syms f(x) y y = f(x)^2*diff(f(x),x); Dy = diff(y,f(x))
Find the 2nd derivative of the function with respect to .
Dy2 = diff(y,f(x),2)
Find the mixed derivative of the function with respect to and .
Dy3 = diff(y,f(x),diff(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
The Euler–Lagrange equation is given by
Evaluate the term .
D1 = diff(L,diff(x(t),t))
Evaluate the second term .
D2 = diff(L,x)
Find the Euler–Lagrange equation of motion of the mass-spring system.
diff(D1,t) - D2 == 0
Differentiate with Respect to Vectors
To evaluate derivatives with respect to vectors, you can use symbolic matrix variables. For example, find the derivatives and for the expression , where is a 3-by-1 vector, is a 3-by-4 matrix, and is a 4-by-1 vector.
Create three symbolic matrix variables
A, of the appropriate sizes, and use them to define
syms x [4 1] matrix syms y [3 1] matrix syms A [3 4] matrix alpha = y.'*A*x
Find the derivative of
alpha with respect to the vectors and .
Dx = diff(alpha,x)
Dy = diff(alpha,y)
Differentiate with Respect to Matrix
To evaluate a derivative with respect to a matrix, you can use symbolic matrix variables. For example, find the derivative for the expression , where is a 3-by-1 vector, and is a 3-by-3 matrix. Here, is a scalar that is a function of the vector and the matrix .
Create two symbolic matrix variables to represent and . Define .
syms X [3 1] matrix syms A [3 3] matrix Y = X.'*A*X
Find the derivative of with respect to the matrix .
D = diff(Y,A)
The result is a Kronecker tensor product between and , which is a 3-by-3 matrix.
ans = 1×2 3 3
Differentiate Symbolic Matrix Function
Differentiate a symbolic matrix function with respect to its matrix argument.
Find the derivative of the function , where is a 1-by-3 matrix, is a 3-by-2 matrix, and is a 2-by-1 matrix. Create , , and as symbolic matrix variables and as a symbolic matrix function.
syms A [1 3] matrix syms B [3 2] matrix syms X [2 1] matrix syms t(X) [1 1] matrix keepargs t(X) = A*sin(B*X)
Differentiate the function with respect to using
Dt = diff(t,X)
f — Expression or function to differentiate
symbolic expression | symbolic function | symbolic vector | symbolic matrix | symbolic matrix variable | symbolic matrix function
Expression or function to differentiate, specified as one of these values:
a symbolic expression
a symbolic function
a symbolic vector or a symbolic matrix (a vector or a matrix of symbolic expressions or functions)
a symbolic matrix variable
a symbolic matrix function
f is a symbolic vector or matrix,
diff differentiates each element of
f and returns a vector or a matrix of the same size
n — Order of derivative
Order of derivative, specified as a nonnegative integer.
var — Differentiation parameter
symbolic scalar variable | symbolic function | derivative function
Differentiation parameter, specified as a symbolic scalar variable,
symbolic function, or a derivative function created using the
If you specify differentiation with respect to the symbolic function
var = f(x) or the derivative function
diff(f(x),x), then the first argument
must not contain any of these:
Integral transforms, such as
Unevaluated symbolic expressions that include
Symbolic functions evaluated at a specific point, such as
var1,...,varN — Differentiation parameters
symbolic scalar variables | symbolic functions | derivative functions
Differentiation parameters, specified as symbolic scalar variables,
symbolic functions, or derivative functions created using the
mvar — Differentiation parameter
symbolic matrix variable
Differentiation parameter, specified as a symbolic matrix variable.
When using a symbolic matrix variable as the differentiation parameter,
f must be a differentiable scalar function, where
mvar can represent a scalar, vector, or matrix. The
f cannot be a tensor or a matrix in terms
of tensors. For example, see Differentiate with Respect to Vectors and
Differentiate with Respect to Matrix.
difffunction does not support tensor derivatives when using a symbolic matrix variable as the differentiation parameter. If the derivative is a tensor, or the derivative is a matrix in terms of tensors, then the
difffunction will error.
When computing mixed higher-order derivatives with more than one variable, do not use
nto specify the order of derivative. Instead, specify all differentiation variables explicitly.
To improve performance,
diffassumes that all mixed derivatives commute. For example,
This assumption suffices for most engineering and scientific problems.
If you differentiate a multivariate expression or function
fwithout specifying the differentiation variable, then a nested call to
diff(f,n)can return different results. The reason is that 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
sign, the arguments must be real values. For complex arguments of
difffunction formally computes the derivative, but this result is not generally valid because
signare not differentiable over complex numbers.
Version HistoryIntroduced before R2006a
R2022a: Differentiate symbolic matrix functions
diff function accepts an input argument of type
symfunmatrix. For an example, see Differentiate Symbolic Matrix Function.