Professional Documents
Culture Documents
Sixth Lecture (1)
Sixth Lecture (1)
Sixth Lecture (1)
4.1 Differentiation
To illustrate how to take derivatives using the Symbolic Math Toolbox, first create
a symbolic expression:
syms x
f = sin(5*x)
diff(f)
ans = 5*cos(5*x)
syms x
g = exp(x)*cos(x)
diff(g)
ans = exp(x)*cos(x)-exp(x)*sin(x)
diff(g,2)
ans = -2*exp(x)*sin(x)
36
You can get the same result by taking the derivative twice:
diff(diff(g))
ans = -2*exp(x)*sin(x)
cases, Matlab might not simply an answer, in which case you can use the simplify
command.
Note that to take the derivative of a constant, you must first define the constant
c = sym(’5’);
diff(c)
returns
ans = 0
diff(5)
Matlab returns
ans = []
37
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
syms s t
f = sin(s*t)
the command
diff(f,t)
∂f
calculates the partial derivative ∂t . The result is
ans = cos(s*t)*s
diff(f,s)
which returns:
ans = cos(s*t)*t
If you do not specify a variable to differentiate with respect to, Matlab chooses a
default variable. For one-letter variables, the default variable is the letter closest
38
To calculate the second derivative of f with respect to t, enter
diff(f,t,2)
which returns
ans = sin(s ⇤ t) ⇤ s2
Note that diff(f,2) returns the same answer because t is the default variable.
4.2 Limits
“gets close to” or approaches a certain value. Recall that the definition of the
0 f ( x + h) f (x)
f ( x ) = lim
h !0 h
provided this limit exists. The Symbolic Math Toolbox enables you to calculate
syms h n x
which returns
ans = -sin(x)
39
limit((1 + x/n)ˆn,n,inf )
which returns
ans = exp(x)
4.3 Integration
int(f)
attempts to find another symbolic expression, F, so that diff(F) = f. That is, int(f)
int(f,v)
For example
sym x
f= sin(x)
int(f)
returns
ans = -cos(x)
40
Definite integration
int(f,a,b)
and
int(f,v,a,b)
Z b Z b
f ( x ) dx and f (v) dv
a a
respectively.
For example
sym x a
f = a*exp(-xˆ2);
int(f,x,-inf,inf)
This returns
ans = a* pi ˆ(1/2)
41