Main Content

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 = 5cos(5x)

As another example, specify another expression that uses exp(x) to represent ex.

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

Differentiate the expression g.

y = diff(g)
y = excos(x)-exsin(x)

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 = -2exsin(x)

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

D2g = diff(diff(g))
D2g = -2exsin(x)

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 f/t by using diff and specifying the variable to differentiate as t.

Df_t = diff(f,t)
Df_t = scos(st)

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

Df_s = diff(f,s)
Df_s = tcos(st)

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 = -s2sin(st)

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

More Examples About Derivatives

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 = 

νJbesseljν(z)z-Jbesseljν+1(z)

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 = 

(cos(ax)sin(ax)-sin(ax)cos(ax))

Find the derivative of A with respect to x.

DA = diff(A)
DA = 

(-asin(ax)acos(ax)-acos(ax)-asin(ax))

You can also perform differentiation of a vector function with respect to a vector argument. Consider the transformation from Cartesian coordinates (x,y,z) to spherical coordinates (r,λ,φ) as given by

x=rcosλcosφ,

y=rcosλsinφ,

z=rsinλ.

Here, λ corresponds to elevation or latitude, while φ denotes azimuth or longitude.

A point in 3-D space can be represented in Cartesian coordinates (x,y,z) or spherical coordinates (r,lambda,phi)

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

J=(x,y,z)(r,λ,φ).

For the purposes of computing the Jacobian, use l to represent λ and f to represent φ. 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 = 

(cos(f)cos(l)-rcos(f)sin(l)-rcos(l)sin(f)cos(l)sin(f)-rsin(f)sin(l)rcos(f)cos(l)sin(l)rcos(l)0)

Find the determinant of this Jacobian and simplify the result.

detJ = simplify(det(J))
detJ = -r2cos(l)

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

dfdx

diff(f) or diff(f,x)

dfda

diff(f,a)

d2fdb2

diff(f,b,2)

J=(r,t)(u,v)

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

See Also

| | | | | |

External Websites