differentiation -凯发k8网页登录

main content

differentiation

to illustrate how to take derivatives using symbolic math toolbox™ software, first create a symbolic expression:

syms x
f = sin(5*x);

the command

diff(f)

differentiates f with respect to x:

ans =
5*cos(5*x)

as another example, let

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

where exp(x) denotes ex, and differentiate g:

y = diff(g)
y =
exp(x)*cos(x) - exp(x)*sin(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.

vpa(subs(y,x,2))
ans =
-9.7937820180676088383807818261614

to take the second derivative of g, enter

diff(g,2)
ans =
-2*exp(x)*sin(x)

you can get the same result by taking the derivative twice:

diff(diff(g))
ans =
-2*exp(x)*sin(x)

in this example, matlab® software automatically simplifies the answer. however, in some cases, matlab might not simplify an answer, in which case you can use the simplify command. for an example of such simplification, 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

c = sym('5');
diff(c)

returns

ans =
0

if you just enter

diff(5)

matlab returns

ans =
     []

because 5 is not a symbolic expression.

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, given the symbolic expression

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

the command

diff(f,t)

calculates the partial derivative f/t. the result is

ans = 
s*cos(s*t)

to differentiate f with respect to the variable s, enter

diff(f,s)

which returns:

ans =
t*cos(s*t)

if you do not specify a variable to differentiate with respect to, matlab chooses a default variable. basically, the default variable is the letter closest to x in the alphabet. see the complete set of rules in . 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(f,1)
ans = 
t

calculate the second derivative of f with respect to t:

diff(f,t,2)

this command returns

ans =
-s^2*sin(s*t)

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

syms a b x n t theta

this table illustrates the results of entering diff(f).

f

diff(f)

syms x n
f = x^n;
diff(f)
ans =
n*x^(n - 1)
syms a b t
f = sin(a*t   b);
diff(f)
ans =
a*cos(b   a*t)
syms theta
f = exp(i*theta);
diff(f)
ans =
exp(theta*1i)*1i

to differentiate the bessel function of the first kind, besselj(nu,z), with respect to z, type

syms nu z
b = besselj(nu,z);
db = diff(b)

which returns

db =
(nu*besselj(nu,z))/z - besselj(nu   1,z)

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

syms a x
a = [cos(a*x),sin(a*x);-sin(a*x),cos(a*x)]

which returns

a =
[  cos(a*x), sin(a*x)]
[ -sin(a*x), cos(a*x)]

the command

diff(a)

returns

ans =
[ -a*sin(a*x),  a*cos(a*x)]
[ -a*cos(a*x), -a*sin(a*x)]

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ϕ, and z=rsinλ. note that λ 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 toolbox syntax, use l for λ and f for φ. the commands

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

return the jacobian

j =
[ cos(f)*cos(l), -r*cos(f)*sin(l), -r*cos(l)*sin(f)]
[ cos(l)*sin(f), -r*sin(f)*sin(l),  r*cos(f)*cos(l)]
[        sin(l),         r*cos(l),                0]

and the command

detj = simplify(det(j))

returns

detj = 
-r^2*cos(l)

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

a table summarizing diff and jacobian follows.

mathematical operator

matlab command

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

    网站地图