| Symbolic Math Toolbox | ![]() |
The Symbolic Math Toolbox provides functions to do the basic operations of calculus. The following sections describe these functions:
Differentiation
To illustrate how to take derivatives using the Symbolic Math Toolbox, first create a symbolic expression:
differentiates f with respect to x:
where exp(x) denotes ex, and differentiate g:
To take the second derivative of g, enter
You can get the same result by taking the derivative twice:
In this example, MATLAB automatically simplifies the answer. However, in some cases, MATLAB might not simply an answer, in which case you can use the simplify command. For an example of this, see More Examples.
Note that to take the derivative of a constant, you must first define the constant as a symbolic expression. For example, entering
because 5 is not a symbolic expression.
Derivatives of Expressions with Several Variables
To differentiate an expression that contains more than one symbolic variable, you must 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, given the symbolic expression
calculates the partial derivative
. The result is
To differentiate f with respect to the variable s, enter
If you do not specify a variable to differentiate with respect to, MATLAB chooses a default variable by the same rule described in The subs Command. For one-letter variables, the default variable is the letter closest to x in the alphabet. In the preceding example, diff(f) takes the derivative of f with respect to t because t is closer to x in the alphabet than s is. To determine the default variable that MATLAB differentiates with respect to, use the findsym command:
To calculate the second derivative of f with respect to t, enter
Note that diff(f,2) returns the same answer because t is the default variable.
More Examples
To further illustrate the diff command, define a, b, x, n, t, and theta in the MATLAB workspace by entering
The table below illustrates the results of entering diff(f).
| f |
diff(f) |
x^n |
x^n*n/x |
sin(a*t+b) |
cos(a*t+b)*a |
exp(i*theta) |
i*exp(i*theta) |
In the first example, MATLAB does not automatically simplify the answer. To simplify the answer, enter
To differentiate the Bessel function of the first kind, besselj(nu,z), with respect to z, type
The diff function can also take a symbolic matrix as its input. In this case, the differentiation is done element-by-element. Consider the example
You can also perform differentiation of a column vector with respect to a row vector. Consider the transformation from Euclidean (x, y, z) to spherical
coordinates as given by
,
, and
. Note that
corresponds to elevation or latitude while
denotes azimuth or longitude.
To calculate the Jacobian matrix, J, of this transformation, use the jacobian function. The mathematical notation for J is
For the purposes of toolbox syntax, use l for
and f for
. The commands
J = [ cos(l)*cos(f), -r*sin(l)*cos(f), -r*cos(l)*sin(f)] [ cos(l)*sin(f), -r*sin(l)*sin(f), r*cos(l)*cos(f)] [ sin(l), r*cos(l), 0]
Notice that the first argument of the jacobian function must be a column vector and the second argument a row vector. Moreover, since the determinant of the Jacobian is a rather complicated trigonometric expression, you can use the simple command to make trigonometric substitutions and reductions (simplifications). The section Simplifications and Substitutions discusses simplification in more detail.
A table summarizing diff and jacobian follows.
| Mathematical Operator |
MATLAB Command |
![]() |
diff(f) or diff(f,x) |
![]() |
diff(f,a) |
![]() |
diff(f,b,2) |
![]() |
J = jacobian([r;t],[u,v]) |
| Using the Symbolic Math Toolbox | Limits | ![]() |
Learn more about the latest releases of MathWorks products: |
| © 1994-2009 The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |