Documentation |
Differentiate symbolic expression or function
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).
diff(F,var1,...,varN) differentiates F with respect to the variables var1,...,varN.
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:
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)
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:
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).
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)