Open Live Script
This examples shows how to find derivatives using Symbolic Math Toolbox™.
First create a symbolic expression.
syms xf = sin(5*x);
Differentiate the expression f
with respect to x
.
Df = diff(f)
Df =
As another example, specify another expression that uses exp(x)
to represent .
g = exp(x)*cos(x);
Differentiate the expression g
.
y = diff(g)
y =
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 =
To take the second derivative of g
, use diff(g,2)
.
D2g = diff(g,2)
D2g =
You can get the same result by taking the derivative twice.
D2g =
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 =
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 tf = sin(s*t);
Find the partial derivative by using diff
and specifying the variable to differentiate as t
.
Df_t = diff(f,t)
Df_t =
To differentiate f
with respect to the variable s
, specify the variable to differentiate as s
.
Df_s = diff(f,s)
Df_s =
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 =
Calculate the second derivative of f
with respect to t
.
D2f = diff(f,t,2)
D2f =
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.
|
|
---|---|
|
|
|
|
|
|
You can differentiate the Bessel function of the first kind, besselj(nu,z)
, with respect to z
.
syms nu zb = besselj(nu,z);Db = diff(b)
Db =
The diff
function can also take a symbolic matrix as its input. In this case, the differentiation is done element-by-element.
syms a xA = [cos(a*x),sin(a*x);-sin(a*x),cos(a*x)]
A =
Find the derivative of A
with respect to x
.
DA = diff(A)
DA =
You can also perform differentiation of a vector function with respect to a vector argument. Consider the transformation from Cartesian coordinates to spherical coordinates as given by
,
,
.
Here, corresponds to elevation or latitude, while denotes azimuth or longitude.
To calculate the Jacobian matrix, , of this transformation, use the jacobian
function. The mathematical notation for is
.
For the purposes of computing the Jacobian, use l
to represent and f
to represent . Find the Jacobian.
syms r l fx = r*cos(l)*cos(f);y = r*cos(l)*sin(f);z = r*sin(l);J = jacobian([x; y; z], [r l f])
J =
Find the determinant of this Jacobian and simplify the result.
detJ = simplify(det(J))
detJ =
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 |
---|---|
| |
| |
| |
|
See Also
diff | int | jacobian | gradient | curl | laplacian | functionalDerivative
External Websites
- Calculus Derivatives (MathWorks Teaching Resources)
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office