Calculus Matlab Help

Calculus
In Chapter 8 we discussed techniques for performing numerical differentiation and numerical integration; this section covers differentiation and integration of symbolic expressions to obtain closed form results for the derivatives and integrals.
Differentiation
The di f f function is used to obtain the symbolic derivative. Although this function has the same name as the function used to compute numerical differences (see Chapter 8), MATLAB detects whether or not a symbolic expression is used in the argument and directs the calculation accordingly. The basic syntax is di f f (E) ,
which returns the derivative of the expression E with respect to the default independent variable.
For example, the derivatives
dxn — = IlXn-1
dx
dlnx ~=;
d sin2 x .
— = 2smxcosx
dx
dsiny –;;y =cosy

are obtained with the following session:
»syms n x y
»diff(x”n)
ans =
x”n*n/x
»simplify (ans)
ans =
x”(n-l)*n
»diff (log (x))
ans =
l/x
»diff«sin(x))”2)
ans =
2*sin(x)*cos(x)
»diff (sin (y))
ans =.
cos(y)
If the expression contains more than one variable. tbedi f f function operates on the variable x, or the variable closest to x, unless told to do otherwise. When there is more than one variable. the di f f function computes thepartial derivative.
For example. if
fix; y) = sin(xy)
then
af = ycos(xy) ax
The corresponding session is
»diff(sin(x*y))
ans =
cas (x*y)”*y
There are three ways to use the di f f function. but the third way is preferred. Using the function x2 as an example. the first way is
»E = ‘x”2’;
»diff (E)
ans
2*x
The second way is
»diff (‘x”2’)
ans
2*x

The third way is
»syms x
»diff (x”2)
ans =
2*x
Note that the expression to be differentiated need not be placed in quotes if the variable has been declared to be symbolic. This method is preferred because the use of quoted strings bypasses the-use of symbolic expressions, which is the whole point of the Symbolic Math toolbox. There are three other forms of the di ff function. The function di ff (E,v) returns the derivative of the expression E with respect to the variable v. For
example,
-o-[x–s-i’n’-(x-y’-)-] =. 2 x cos(xy)
ay
is given by
»syms x y
»diff(x·sin(~y),y)
ans ==
x-“2*cos(x*y)
The function di f f (E, n ) returns the nth derivative of the expression E with respect to the default independent variable. For example,
d2(x3) –=6x
dx2
is given by
»syms x
»diff (x”3,2)
ans =
6*x
The function di f f (E,v, n).returns the nth derivative of the expression E with respect to the variable v. For example.
a2[x sin(xy)] 3 .
–a-y-=2—‘– = -x sm(xy)
is given by
»syms x y
»diff(x*sin(x*y),y,2)
ans =
-x”3*sin(x*y)
Table 10.3-1 summarizes the differentiation functions.

Capture

Max-Min Problems
The derivative can be used to find the maximum or minimum of a continuo function, say, f(x), over an interval a :::x ::: b. A local maximum or local minimum (one that does not occur at one of the boundaries x = a or x = b) can occ
only at a critical point, which is a point where either df [dx =0 or dfldx does n exist. If d2 fldx2 > 0, the point is a relative minimum; if d2 f/dx2 < 0, the poi is a Illative maximum. If d2 f/dx2 = 0, the point is neither a minimum nor a maximum, but is an inflection point. If multiple candidates exist, you must evalu the function at each point to determine the global maximum and global minimu

Test Your Understanding
T1 .1-1 Given lhat y = siDb(3.r)cosh(5x), use MATLAB to Dud dyf4x al
x =0.2.
(Answer: 9.2288.)
11 .J-2 Given that z = 5 coti(2r)ln(4y)., use MAlLAB to find azfay.
(Answer: 5 cos(2x)fy.)

Integration

The int (E) fuoction· i_ gUile .asymbolic expression E. It atte to find the symbolic expres· I di f f (I )=E. If intep dtvs _IS exi in closed form or MA1LAB .find the integral C\al if . function will return the e~ uoewalliated.

As with the diff function, there are three ways to use the int function; two of the ways use quoted strings, but the following way is preferred. Using the function 2x as an example, the method is 1be function int (E) returns the integral of the expression E with respect to the default independmt variable. For example, you can obtain the following integrals with the next session:
! xn+1
x”dx =–
n+l J ~dX = lox
!cos x dx = sinx
f sinydy =-cosy
»syms n x y
»int(x”n)
ans =
x”(n+l)/(n+l)
»int (l/x)
ans =
log (x)
»int (cos (x))
ans =
sin(x)
»int (sin(y))
ans =
-cos{y)
HeR are the oda forms of the int function.1be fonn int (E, v) returns .ukgraI of the expression E wi respect to the variable v. For example. the
result
J ” XX
d« =-
lox
»syms x
»int(2*x)
ans =
x”2
»syms n x
»int{x~Mn’
ans =
1/109 (x “‘x”’n

The form int (E,a, b) returns the integral of the expression E with respect to the default independent variable evaluated over the interval [a, b], where a and b are numeric expressions. For example, the result
15x2dx = x-315 = 39
232
is obtained as follows:
»syms x
»int(x”2,2,5)
ans
39
The form int (E, v, a, b) returns the integral of the expression E with respect to the variable v evaluated over the interval [a, b], where a and b are numeric quantities. For example, the result
r5
xldy == xy315 = 125 x
10 3 0 3
is obtained from
»syms x y
»int(xy”2,y,Q,5)
ans
125/3*x
The result
is obtained from
»syms a b x
»int(x”2,a,b)
ans
1/3 *b”3-1/3 *a”3
The form in t (E, m, n) returns the integral of the expression E with respect to the default independent variable evaluated over the interval [m, n], where m and nare symbolic expressions. For example,
l’ x dx = x2
2
1: = lt2
– ~
je’ sinx dx = -cosxl~’ = -cos(e’) +cos t

are given by this session:
»syms t x
»int(x,l,t)
ans =
1/2*t”2-1/2
int(sin(x),t,exp(t))
ans
-cos(exp(t)) + cos(t)
The following session gives an example for which no integral can be found. The indefinite integral exists, but the definite integral does not exist if the limits of integration include the singularity at x = I. The integral is I\ J-I-dX =lnlx -11
x-I
The session is
»syms x
»int (II (x-l))
ans =
log (x-I)
»int(1/(x-l),Q,2)
ans =
NaN
Table 10.3-1 summarizes the integration functions.

Test Your Understanding
110.3-3 Given that y = x sin(3x). use MATLAB to find J y dx,
(Answer: [sin(3x) – 3x cos(3x»/9.)
110.3-4 Given that z = 6y2 tan(8x). use MATLAB to find J z dy,
. (Answer: 2y3 tan(8x).) . .
110.3-6 Use MATLAB to evaluate
1~x sin(3x) dx
-2
(Answer: 0.6672.) .

Taylor Series Taylor’s theorem states that a function I(;t) can be represented in the vicini.
x =a by the expansion
( dI ) I I (d2 I(x) = I(a) + – . (x – a) + – -2I) I . (x – a’)2 + …
I dx x=a . 2 dx x=a
– +, I (d-kkI) I (x – a) ” + … + Rn
k. dx x=a
(lO.~
The term R; is the remainder and is given by
R; =,I (ddn In) I (x – at n. x x=b
(lO.~
“.. where b lies between a and x. These results hold if I(x) has cont.inuous derivatives through order n.
approaches 0 for large n, the expansion is called the Taylor series for I(x) x =a. If a =0, the series is sometimes called the Maclaurin series. Some common examples of the Taylor series are
x3 x5′ x7
sin x = x – ~ + – – – + … -00 < x < 00 3! 5! 7! ‘
x2 x4 x6
cos X = I – – + – – – + … -00 < x < 00 2! 4! 6! ‘.
X2 x3 x4
eX = I+x.+ – + – + – + …. -00 < x < 00’ 2! 3! 4! ‘
where a = 0 in all three examples. The t ay 1 or (f , n, a) function gives the first n -1 terms in the Taylor
for the function defined in the expression f, evaluated at the point x = a. parameter a is omitted the function returns the series evaluated at x = O. are some examples: .
»syms x
»f = exp (x);
»taylor (f ,.4)
.ans =
, 1+x+l/2*xA2+1/6*xA3
»taylor (f, 3,2)
ans
exp(2)+exp(2)*(x-2)+1/2*exp(2)*(x~2)A2

This latter expression corresponds to

Capture

Sums
The symsum (E) function returns the symbolic summation of the expression E;
that is
x-I LE(x) = E(O) + E(1) + E(2) + … + E(x – I)
x=O
613

e symsum (E,a,b) function returns the sum of the expression E as the defaul symbolic variable varies from a to b. That is, if the symbolic variable is x,the S = symsum (E,a,b) returns
b . LE(x) = E(a) + E(a + 1) + E(~. + 2) + … + E(b)
x=a
..
Here are some examples. The summations
10 0 Lk = 0 + 1+ 2 + 3 + … +9. + 10 = 55
k=O ‘
n-I 1 1 Lk = 0 + 1+ 2 + 3 + … +n – 1 = _n2 – -n
k=O . 2 2
4L
e = 1 +4 +-9+ 16 =030
k=1
are given by
»syms k n
»symsum(k,O,IO)
ans =55 I
»syms,um (k”2, I, 4)’
ans =
30
»symsum(k,O,n-l)
ans =
1/2*nA2-1/2*n
»factor(ans)
ans =
1/2*n* (n-I)
I
The latter expression is the standard form of the result. Limits The function 1imi t (E, a) returns the limit.
Jim E{x)

if x is the symbolic variable. There are several variations of this syntax. The basic form 1imi t (E) finds the limit as x ~ O.For example
1
. sin(ax)
im = a
.1″-+0 x
is given by
»syrns a x
»limit{sin{a*x)/x)
ans
a
The form 1imi t (E,v , a) finds the limit as v ~ a. For example,
1
. x – 3 1
Im— =-
.1″-+3 x2 – 9 6
‘. sin(x +h) – sin(x)
hm——-
.1″-+0 h
are given by
»syms h x
»limit{{x-3)/{xA2-9) ,3)
ans =
1/6
»limit{{sin(x+h)-sin{x»/h,h~O)
ans
cos(x)
The forms limit (E,v,a, ‘right’) and limit(E,v,a,’left’)
specify the direction of the limit. For example,
1 .
lim – =-00
..1″-+0- X
1
lim – = 00
.1″-+0+ X
are. given by
»syms x .
»limit(l!x,x,O,’left’)
.ans = ‘
-inf
»limit{l/x,x,O, ‘right’)
ans
inf
Tattle 10.3-1 summarizes theseries and limit functions.

Test Your Understanding
T10.3-6 Use MATLAB to find the first three nonzero terms in the Taylor series for cosx.
(Answer: 1 – x2/2 +x4/24.)
T10.3-7 Use MATLAB to find a formula for the sum
(Answer: m4/4 – m3/2 +m2/4.)
T10.3-8 Use MATLAB to evaluate
7
Lcos(rrn)
n=O
(Answer: 0.)
T10.3-9 Use MATLAB.to evaluate
a-tO
lim –=—
.1″-+5 x3 – 125
(Answer: 2/75.)

Posted on July 24, 2015 in Symbolic Processing with MATLAB

Share the Story

Back to Top
Share This