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
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)
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)
Find the second derivative of this expression with respect to the variable
syms x y Df = diff(x*cos(x*y), y, 2)
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)
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
f— Expression or function to differentiate
Expression or function to differentiate, specified as a symbolic
expression or function or as a vector or matrix of symbolic expressions or
f is a vector or a matrix,
diff differentiates each element of
f and returns a vector or a matrix of the same size
var— Differentiation parameter
Differentiation parameter, specified as a symbolic variable, symbolic
function, or a derivative
If you specify differentiation with respect to a symbolic function
var = f(x) or the derivative function
diff(f(x),x), then the first argument
must not contain:
integral transforms, such as
unevaluated symbolic expressions that include
symbolic functions evaluated at certain points, such as
var1,...,varN— Differentiation parameters
Differentiation parameters, specified as symbolic variables, symbolic
functions, or symbolic
n— Differentiation order
Differentiation order, specified as a nonnegative integer.
When computing mixed higher-order derivatives with more than one variable, do
n to specify the differentiation order. Instead,
specify all differentiation variables explicitly.
To improve performance,
that all mixed derivatives commute. For example,
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
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
symvar(f,1) and used for all differentiation
If you differentiate an expression or function containing
ensure that the arguments are real values. For complex arguments of
diff function formally computes the derivative,
but this result is not generally valid because
not differentiable over complex numbers.