# diff

Differentiate symbolic expression or function

## Syntax

``Df = diff(f)``
``Df = diff(f,n)``
``Df = diff(f,var)``
``Df = diff(f,var,n)``
``Df = diff(f,var1,...,varN)``
``Df = diff(f,mvar)``

## Description

example

````Df = diff(f)` differentiates `f` with respect to the symbolic scalar variable determined by `symvar(f,1)`.```

example

````Df = diff(f,n)` computes the `n`th derivative of `f` with respect to the symbolic scalar variable determined by `symvar`.```

example

````Df = diff(f,var)` differentiates `f` with respect to the differentiation parameter `var`. `var` can be a symbolic scalar variable, such as `x`, a symbolic function, such as `f(x)`, or a derivative function, such as `diff(f(t),t)`. ```

example

````Df = diff(f,var,n)` computes the `n`th derivative of `f` with respect to `var`.```

example

````Df = diff(f,var1,...,varN)` differentiates `f` with respect to the parameters `var1,...,varN`.```

example

````Df = diff(f,mvar)` differentiates `f` with respect to the symbolic matrix variable `mvar` of type `symmatrix`.```

## Examples

collapse all

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 \mathrm{cos}\left({x}^{2}\right)$`

Find the value of the derivative at `x = 2`. Convert the value to `double`.

`Df2 = Df(2)`
`Df2 = $4 \mathrm{cos}\left(4\right)$`
`double(Df2)`
```ans = -2.6146 ```

Find the first derivative of this expression.

```syms x t Df = diff(sin(x*t^2))```
`Df = ${t}^{2} \mathrm{cos}\left({t}^{2} x\right)$`

Because you did not specify the differentiation variable, `diff` uses the default variable defined by `symvar`. For this expression, the default variable is `x`.

`var = symvar(sin(x*t^2),1)`
`var = $x$`

Now, find the derivative of this expression with respect to the variable `t`.

`Df = diff(sin(x*t^2),t)`
`Df = $2 t x \mathrm{cos}\left({t}^{2} x\right)$`

Find the 4th, 5th, and 6th derivatives of ${t}^{6}$.

```syms t D4 = diff(t^6,4)```
`D4 = $360 {t}^{2}$`
`D5 = diff(t^6,5)`
`D5 = $720 t$`
`D6 = diff(t^6,6)`
`D6 = $720$`

Find the second derivative of this expression with respect to the variable `y`.

```syms x y Df = diff(x*cos(x*y), y, 2)```
`Df = $-{x}^{3} \mathrm{cos}\left(x y\right)$`

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 Df = diff(x*y,2)```
`Df = $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.

`Df = diff(diff(x*y))`
`Df = $1$`

In the first call, `diff` differentiates `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 Df = diff(x*sin(x*y),x,y)```
`Df = $2 x \mathrm{cos}\left(x y\right)-{x}^{2} y \mathrm{sin}\left(x y\right)$`

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)```
`Df = ${x}^{2} {y}^{3} \mathrm{sin}\left(x y\right)-6 x {y}^{2} \mathrm{cos}\left(x y\right)-6 y \mathrm{sin}\left(x y\right)$`

Find the derivative of the function $y=f\left(x{\right)}^{2}\frac{df}{dx}$ with respect to $f\left(x\right)$.

```syms f(x) y y = f(x)^2*diff(f(x),x); Dy = diff(y,f(x))```
```Dy =  ```

Find the 2nd derivative of the function $y=f\left(x{\right)}^{2}\frac{df}{dx}$ with respect to $f\left(x\right)$.

`Dy2 = diff(y,f(x),2)`
```Dy2 =  ```

Find the mixed derivative of the function $y=f\left(x{\right)}^{2}\frac{df}{dx}$ with respect to $f\left(x\right)$ and $\frac{df}{dx}$.

`Dy3 = diff(y,f(x),diff(f(x)))`
`Dy3 = $2 f\left(x\right)$`

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

The Euler–Lagrange equation is given by

`$0=\frac{d}{dt}\frac{\partial L\left(t,x,\underset{}{\overset{˙}{x}}\right)}{\partial \underset{}{\overset{˙}{x}}}-\frac{\partial L\left(t,x,\underset{}{\overset{˙}{x}}\right)}{\partial x}$`

Evaluate the term $\partial L/\partial \underset{}{\overset{˙}{x}}$.

`D1 = diff(L,diff(x(t),t))`
```D1 =  ```

Evaluate the second term $\partial L/\partial x$.

`D2 = diff(L,x)`
`D2(t) = $-k x\left(t\right)$`

Find the Euler–Lagrange equation of motion of the mass-spring system.

`diff(D1,t) - D2 == 0`
```ans(t) =  ```

To evaluate derivatives with respect to vectors, you can use symbolic matrix variables. For example, find the derivatives $\partial \alpha /\partial x$ and $\partial \alpha /\partial y$ for the expression $\alpha ={y}^{T}Ax$, where $y$ is a 3-by-1 vector, $A$ is a 3-by-4 matrix, and $x$ is a 4-by-1 vector.

Create three symbolic matrix variables `x`, `y`, and `A`, of the appropriate sizes, and use them to define `alpha`.

```syms x [4 1] matrix syms y [3 1] matrix syms A [3 4] matrix alpha = y.'*A*x```
`alpha = ${y}^{\mathrm{T}} A x$`

Find the derivative of `alpha` with respect to the vectors $x$ and $y$.

`Dx = diff(alpha,x)`
`Dx = ${y}^{\mathrm{T}} A$`
`Dy = diff(alpha,y)`
`Dy = ${x}^{\mathrm{T}} {A}^{\mathrm{T}}$`

To evaluate a derivative with respect to a matrix, you can use symbolic matrix variables. For example, find the derivative $\partial Y/\partial A$ for the expression $Y={X}^{T}AX$, where $X$ is a 3-by-1 vector, and $A$ is a 3-by-3 matrix. Here, $Y$ is a scalar that is a function of the vector $X$ and the matrix $A$.

Create two symbolic matrix variables to represent $X$ and $A$. Define $Y$.

```syms X [3 1] matrix syms A [3 3] matrix Y = X.'*A*X```
`Y = ${X}^{\mathrm{T}} A X$`

Find the derivative of $Y$ with respect to the matrix $A$.

`D = diff(Y,A)`
`D = ${X}^{\mathrm{T}}\otimes X$`

The result is a Kronecker tensor product between ${X}^{T}$ and $X$, which is a 3-by-3 matrix.

`size(D)`
```ans = 1×2 3 3 ```

Differentiate a symbolic matrix function with respect to its matrix argument.

Find the derivative of the function $\mathit{t}\left(\mathbit{X}\right)=\mathbit{A}\cdot \mathrm{sin}\left(\mathbit{B}\cdot \mathbit{X}\right)$, where $\mathbit{A}$ is a 1-by-3 matrix, $\mathbit{B}$ is a 3-by-2 matrix, and $\mathbit{X}$ is a 2-by-1 matrix. Create $\mathbit{A}$, $\mathbit{B}$, and $\mathbit{X}$ as symbolic matrix variables and $\mathit{t}\left(\mathbit{X}\right)$ as a symbolic matrix function.

```syms A [1 3] matrix syms B [3 2] matrix syms X [2 1] matrix syms t(X) [1 1] matrix keepargs t(X) = A*sin(B*X)```
`t(X) = $A \mathrm{sin}\left(B X\right)$`

Differentiate the function with respect to $\mathbit{X}$ using `diff`.

`Dt = diff(t,X)`
`Dt(X) = $A \left(\mathrm{cos}\left(B X\right)\odot B\right)$`

## Input Arguments

collapse all

Expression or function to differentiate, specified as one of these values:

• a symbolic expression

• a symbolic function

• a symbolic vector or a symbolic matrix (a vector or a matrix of symbolic expressions or functions)

• a symbolic matrix variable

• a symbolic matrix function

If `f` is a symbolic vector or matrix, `diff` differentiates each element of `f` and returns a vector or a matrix of the same size as `f`.

Data Types: `single` | `double` | `sym` | `symfun` | `symmatrix` | `symfunmatrix`

Order of derivative, specified as a nonnegative integer.

Differentiation parameter, specified as a symbolic scalar variable, symbolic function, or a derivative function created using the `diff` function.

If you specify differentiation with respect to the symbolic function `var = f(x)` or the derivative function ```var = diff(f(x),x)```, then the first argument `f` must not contain any of these:

• Integral transforms, such as `fourier`, `ifourier`, `laplace`, `ilaplace`, `htrans`, `ihtrans`, `ztrans`, and `iztrans`

• Unevaluated symbolic expressions that include `limit` or `int`

• Symbolic functions evaluated at a specific point, such as `f(3)` or `g(0)`

Data Types: `single` | `double` | `sym` | `symfun`

Differentiation parameters, specified as symbolic scalar variables, symbolic functions, or derivative functions created using the `diff` function.

Data Types: `single` | `double` | `sym` | `symfun`

Differentiation parameter, specified as a symbolic matrix variable.

When using a symbolic matrix variable as the differentiation parameter, `f` must be a differentiable scalar function, where `mvar` can represent a scalar, vector, or matrix. The derivative of `f` cannot be a tensor or a matrix in terms of tensors. For example, see Differentiate with Respect to Vectors and Differentiate with Respect to Matrix.

Data Types: `symmatrix`

## Limitations

• The `diff` function does not support tensor derivatives when using a symbolic matrix variable as the differentiation parameter. If the derivative is a tensor, or the derivative is a matrix in terms of tensors, then the `diff` function will error.

## Tips

• When computing mixed higher-order derivatives with more than one variable, do not use `n` to specify the order of derivative. 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. The reason is that 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`, the arguments must be 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.

## Version History

Introduced before R2006a

expand all