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

Description

example

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

example

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

example

````Df = diff(f,var)` differentiates `f` with respect to the differentiation parameter `var`. `var` can be a symbolic 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`.```

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

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 parameter, specified as a symbolic variable, symbolic function, or a derivative `diff` function.

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

• 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 certain points, such as `f(2)` or `g(0)`

Differentiation parameters, specified as symbolic variables, symbolic functions, or symbolic `diff` functions.

Differentiation order, specified as a nonnegative integer.

Tips

• When computing mixed higher-order derivatives with more than one variable, 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.

Introduced before R2006a

Symbolic Math Toolbox Documentation

Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos