Differentiate symbolic expression or function

`diff(F)`

`diff(F,var)`

`diff(F,n)`

`diff(F,var,n)`

`diff(F,var1,...,varN)`

`diff(`

differentiates `F`

,`var1,...,varN`

)`F`

with
respect to the variables `var1,...,varN`

.

Find the derivative of the function `sin(x^2)`

.

syms f(x) f(x) = sin(x^2); df = diff(f,x)

df(x) = 2*x*cos(x^2)

Find the value of the derivative at `x = 2`

. Convert the value to
double.

df2 = df(2)

df2 = 4*cos(4)

double(df2)

ans = -2.6146

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)

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,$$\frac{\partial}{\partial x}\frac{\partial}{\partial y}f\left(x,y\right)=\frac{\partial}{\partial y}\frac{\partial}{\partial x}f\left(x,y\right)$$

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.If you differentiate an expression or function containing

`abs`

or`sign`

, ensure that the arguments are real values. For complex arguments of`abs`

and`sign`

, the`diff`

function formally computes the derivative, but this result is not generally valid because`abs`

and`sign`

are not differentiable over complex numbers.

`curl`

| `divergence`

| `functionalDerivative`

| `gradient`

| `hessian`

| `int`

| `jacobian`

| `laplacian`

| `symvar`