Differentiate symbolic expression or function
Df = diff(
f with respect to the symbolic variable
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 double.
Df2 = Df(2)
ans = -2.6146
Differentiation 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)
Differentiation 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 differential with respect to matrix, you can use symbolic matrix variables. For example, find the differential 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 differential 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
f — Expression or function to differentiate
symbolic expression | symbolic function | symbolic vector | symbolic matrix | symbolic matrix variable
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)
f is a symbolic vector or matrix,
diff differentiates each element of
f and returns a vector or a matrix of the same size
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 function created using the
mvar — Differentiation parameter
symbolic matrix variable
Differentiation parameter, specified as a symbolic matrix variable.
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.
n — Differentiation order
Differentiation order, specified as a nonnegative integer.
When computing mixed higher-order derivatives with more than one variable, do not use
nto specify the differentiation order. 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. 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
sign, ensure that the arguments are 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.