Documentation

### This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

# diff

Differentiate symbolic expression or function

## Syntax

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

## Description

example

````diff(F)` differentiates `F` with respect to the variable determined by `symvar(F,1)`.```

example

````diff(F,var)` differentiates `F` with respect to the variable `var`.```

example

````diff(F,n)` computes the `n`th derivative of `F` with respect to the variable determined by `symvar`.```

example

````diff(F,var,n)` computes the `n`th derivative of `F` with respect to the variable `var`.```

example

````diff(F,var1,...,varN)` differentiates `F` with respect to the variables `var1,...,varN`.```

## Examples

### Differentiate Function

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```

### Differentiation with Respect to Particular Variable

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)```

### Higher-Order Derivatives of Univariate Expression

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```

### Higher-Order Derivatives of Multivariate Expression with Respect to Particular Variable

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)```

### 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, `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)`.

### Mixed Derivatives

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)```

## Input Arguments

collapse all

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.

Differentiation variables, specified as symbolic variables.

Differentiation order, specified as a nonnegative integer.

## Tips

• 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.