# Differentiation

This examples shows how to find derivatives using Symbolic Math Toolbox™.

First create a symbolic expression.

```syms x f = sin(5*x);```

Differentiate the expression `f` with respect to `x`.

`Df = diff(f)`
`Df = $5 \mathrm{cos}\left(5 x\right)$`

As another example, specify another expression that uses `exp(x)` to represent ${e}^{x}$.

`g = exp(x)*cos(x);`

Differentiate the expression `g`.

`y = diff(g)`
`y = ${\mathrm{e}}^{x} \mathrm{cos}\left(x\right)-{\mathrm{e}}^{x} \mathrm{sin}\left(x\right)$`

To find the derivative of `g` for a given value of `x`, substitute `x` for the value using `subs` and return a numerical value using `vpa`. Find the derivative of `g` at `x = 2`.

`y_eval = vpa(subs(y,x,2))`
`y_eval = $-9.7937820180676088383807818261614$`

To take the second derivative of `g`, use `diff(g,2)`.

`D2g = diff(g,2)`
`D2g = $-2 {\mathrm{e}}^{x} \mathrm{sin}\left(x\right)$`

You can get the same result by taking the derivative twice.

`D2g = diff(diff(g))`
`D2g = $-2 {\mathrm{e}}^{x} \mathrm{sin}\left(x\right)$`

In this example, Symbolic Math Toolbox returns a simplified answer. However, in some cases, the answer is not simplified, in which case you can use the `simplify` command. The next section of this example will discuss the cases of such simplification.

Note that to take the derivative of a constant, you must first define the constant as a symbolic expression.

```c = sym("5"); Dc = diff(c)```
`Dc = $0$`

If you use `diff` directly on a constant number, such as `5`, the result is an empty array because the number is not a symbolic object but a `double` numeric type.

`Dc = diff(5)`
```Dc = [] ```

### Derivatives of Expressions with Several Variables

To differentiate an expression that contains more than one symbolic variable, specify the variable that you want to differentiate with respect to. The `diff` command then calculates the partial derivative of the expression with respect to that variable. For example, specify a symbolic expression with two variables.

```syms s t f = sin(s*t);```

Find the partial derivative $\partial f/\partial t$ by using `diff` and specifying the variable to differentiate as `t`.

`Df_t = diff(f,t)`
`Df_t = $s \mathrm{cos}\left(s t\right)$`

To differentiate `f` with respect to the variable `s`, specify the variable to differentiate as `s`.

`Df_s = diff(f,s)`
`Df_s = $t \mathrm{cos}\left(s t\right)$`

If you do not specify a variable to differentiate with respect to, `diff` uses the default variable. Basically, the default variable is the letter closest to x in the alphabet. See the complete set of rules in Find Symbolic Variables in Expressions, Functions, and Matrices. In the preceding example, `diff(f)` takes the derivative of `f` with respect to `t` because the letter `t` is closer to x in the alphabet than the letter `s` is. To determine the default variable that MATLAB differentiates with respect to, use `symvar`.

`fvar = symvar(f,1)`
`fvar = $t$`

Calculate the second derivative of `f` with respect to `t`.

`D2f = diff(f,t,2)`
`D2f = $-{s}^{2} \mathrm{sin}\left(s t\right)$`

Note that `diff(f,2)` returns the same answer because `t` is the default variable.

To further illustrate the `diff` function on other expressions, define the symbolic variables `a`, `b`, `x`, `n`, `t`, and `theta.`

`syms a b x n t theta`

This table illustrates the results of using `diff` on several other expressions.

`f`

`diff(f)`

`>> syms x n`

`>> f = x^n;`

`>> Df = diff(f)`

`Df =`

`n*x^(n - 1)`

`>> syms a b t`

`>> f = sin(a*t + b);`

`>> Df = diff(f)`

`Df =`

`a*cos(b + a*t)`

`>> syms theta`

`>> f = exp(i*theta);`

`>> Df = diff(f)`

`Df =`

`exp(theta*1i)*1i`

You can differentiate the Bessel function of the first kind, `besselj(nu,z)`, with respect to `z`.

```syms nu z b = besselj(nu,z); Db = diff(b)```
```Db =  $\frac{\nu {\mathrm{J}\text{besselj}}_{\nu }\left(z\right)}{z}-{\mathrm{J}\text{besselj}}_{\nu +1}\left(z\right)$```

The `diff` function can also take a symbolic matrix as its input. In this case, the differentiation is done element-by-element.

```syms a x A = [cos(a*x),sin(a*x);-sin(a*x),cos(a*x)]```
```A =  $\left(\begin{array}{cc}\mathrm{cos}\left(a x\right)& \mathrm{sin}\left(a x\right)\\ -\mathrm{sin}\left(a x\right)& \mathrm{cos}\left(a x\right)\end{array}\right)$```

Find the derivative of `A` with respect to `x`.

`DA = diff(A)`
```DA =  $\left(\begin{array}{cc}-a \mathrm{sin}\left(a x\right)& a \mathrm{cos}\left(a x\right)\\ -a \mathrm{cos}\left(a x\right)& -a \mathrm{sin}\left(a x\right)\end{array}\right)$```

You can also perform differentiation of a vector function with respect to a vector argument. Consider the transformation from Cartesian coordinates $\left(x,y,z\right)$ to spherical coordinates $\left(r,\lambda ,\phi \right)$ as given by

$x=r\phantom{\rule{0.2222222222222222em}{0ex}}\mathrm{cos}\lambda \phantom{\rule{0.2222222222222222em}{0ex}}\mathrm{cos}\phi$,

$y=r\phantom{\rule{0.2222222222222222em}{0ex}}\mathrm{cos}\lambda \phantom{\rule{0.2222222222222222em}{0ex}}\mathrm{sin}\phi$,

$z=r\phantom{\rule{0.2222222222222222em}{0ex}}\mathrm{sin}\lambda$.

Here, $\lambda$ corresponds to elevation or latitude, while $\phi$ denotes azimuth or longitude.

To calculate the Jacobian matrix, $J$, of this transformation, use the `jacobian` function. The mathematical notation for $J$ is

$J=\frac{\partial \left(x,y,z\right)}{\partial \left(r,\lambda ,\phi \right)}$.

For the purposes of computing the Jacobian, use `l` to represent $\lambda$ and `f` to represent $\phi$. Find the Jacobian.

```syms r l f x = r*cos(l)*cos(f); y = r*cos(l)*sin(f); z = r*sin(l); J = jacobian([x; y; z], [r l f])```
```J =  $\left(\begin{array}{ccc}\mathrm{cos}\left(f\right) \mathrm{cos}\left(l\right)& -r \mathrm{cos}\left(f\right) \mathrm{sin}\left(l\right)& -r \mathrm{cos}\left(l\right) \mathrm{sin}\left(f\right)\\ \mathrm{cos}\left(l\right) \mathrm{sin}\left(f\right)& -r \mathrm{sin}\left(f\right) \mathrm{sin}\left(l\right)& r \mathrm{cos}\left(f\right) \mathrm{cos}\left(l\right)\\ \mathrm{sin}\left(l\right)& r \mathrm{cos}\left(l\right)& 0\end{array}\right)$```

Find the determinant of this Jacobian and simplify the result.

`detJ = simplify(det(J))`
`detJ = $-{r}^{2} \mathrm{cos}\left(l\right)$`

The arguments of the `jacobian` function can be column or row vectors. Moreover, since the determinant of the Jacobian is a rather complicated trigonometric expression, you can use `simplify` to make trigonometric substitutions and reductions (simplifications).

A table summarizing `diff` and `jacobian` follows.

Mathematical Operator

Command Using Symbolic Math Toolbox

$\frac{df}{dx}$

`diff(f)` or `diff(f,x)`

$\frac{df}{da}$

`diff(f,a)`

$\frac{{d}^{2}f}{d{b}^{2}}$

`diff(f,b,2)`

$J=\frac{\partial \left(r,t\right)}{\partial \left(u,v\right)}$

`J = jacobian([r; t],[u; v])`