Differentiate symbolic expression or 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)
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:
ans = x
Now, find the derivative of this expression with respect to the variable t:
ans = 2*t*x*cos(t^2*x)
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
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)
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:
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).
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)
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.
Differentiation variable, specified as a symbolic variable or a string.
Differentiation variables, specified as symbolic variables or strings.
Differentiation order, specified as a nonnegative integer.
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,
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.