Chapter3.mws

Chapter 3: Calculus

One of the things Maple can help you with the most is calculus. It can take derivatives, do integrals, make series expansions, do infinite sums, and a whole lot more. In this chapter we will concentrate just on the basics.

Limits

Debugging

The most basic idea in calculus is that of a limit, and Maple knows how to do them. For example, to find the value of the limit limit(sin(3*x)/x,x = 0) , just use the Maple command limit on the expression like this

> limit(sin(3*x)/x,x=0);

3

You can also do this with a Maple function

> y:=x->sin(3*x)/x;limit(y(x),x=0);

y := proc (x) options operator, arrow; sin(3*x)/x e...

3

>

You can use ?limit to see some details about how this command works, but this is about all there is to it.

Problem 3.1

Try this one just for practice limit((cos(x)-1)/(x^2),x = 0) .

Go to top of section

Differentiation

Debugging

Derivatives are relatively simple, so this section is simple. Maple can take derivatives of elementary functions and special functions with equal ease, so there is almost nothing to do in this section except show you Maple's two differentiation commands, one for expressions and one for functions.

First let's do expressions. I suggest you use the following forms for taking the 1st, 2nd, and third derivatives, as illustrated below using the tangent function. You can either use diff , which gives you the derivative directly, or Diff and value, which display the derivative to be taken, then evaluate it. Diff is actually more useful than you might think because it gives you a chance to see if the derivative you have asked Maple to take is the one you really want.

> diff(tan(x),x);

1+tan(x)^2

> diff(tan(x),x$2);

2*tan(x)*(1+tan(x)^2)

> d:=Diff(tan(x),x$3);

> d:=value(d);

d := Diff(tan(x),`$`(x,3))

d := 2*(1+tan(x)^2)^2+4*tan(x)^2*(1+tan(x)^2)

> d:=simplify(d);

d := 2+8*tan(x)^2+6*tan(x)^4

Now let's see how to do functions.

> f:=x->tan(x)/x;

f := proc (x) options operator, arrow; tan(x)/x end...

Diff doesn't work on functions; instead we have to use Maple's D command. This is a very powerful command in spite of its small size. It can take multiple derivatives of multiple functions (to see how to use all it can do type ?D ) but we will only be taking first derivatives of a single function. The first derivative is easy

> fp:=D(f);

fp := proc (x) options operator, arrow; (1+tan(x)^2...

Note that assigning the result of D(f) to fp produces the function fp(x). There is more than one way to take higher derivatives, but here is the most general one.

> fpp:=D[1$2](f);

fpp := proc (x) options operator, arrow; 2*tan(x)*(...

>

In the square brackets the 1 means differentiate with respect to the first (in this case the only) variable in the argument list and $2 means do it twice just as in diff .

Well, that's about it. Here are some exercises to practice on.

Problem 3.2

Take the indicated derivatives of the functions listed below. Use expression form on most of them but use function form on (a) and (d). When you get a mess try using simplify to clean it up. You will discover that simplify doesn't work on functions; to make the result look nice use the mouse to copy the mess you want to simplify to the clipboard, assign it to a new variable, cut off the extraneous junk, execute the command, then rebuild the derivative function using cut and paste again. This combination of Maple and editing is a good way to do mistake-free algebra.

(a) Diff(sqrt(1+x^3),`$`(x,3)) (b) Diff(J[0](x),x) (c) Diff(I[1](x),x) (d) Diff(exp(tan(x)),`$`(x,2)) (e) diff(GAMMA(x),x)

(f) diff(erf(x),x) (g) diff(K(k),k) (this is the complete elliptic integral of the first kind, Maple's EllipticK .)

-------------------------------------------------------------------

Problem 3.3

Here's a find-the-maxima-and minima problem like ones you used to do in high school. Consider the function ln(x)*J[0](x) . (Note: I am using the word function in the mathematical sense, not in the Maple sense. This problem is easier if you just use a Maple expression to define the function above.) (a) First plot this function on the interval [0,10]. (b) Look at the graph and find, approximately, the values of x where the function has maxima and minima. Then take the derivative of the function and use the fsolve command to find these values of x precisely. If your expression for the derivative is called f , and if you wanted to find a zero of it near 1.1, you would do this: fsolve(f,x=1.1) ;

-------------------------------------------------------------------

Problem 3.4

In quantum mechanics you will encounter a close relative of the Legendre functions P[n](x) that we have seen already. These new functions are called the Associated Legendre functions P[n]^m . For each integer n these functions are defined for values of m in the interval [0 .. n] , with the m = 0 function being plain old P[n](x) . These functions are defined in terms of derivatives of the Legendre functions:

P[n]^m = (-1)^m*(1-x^2)^(m/2)*diff(P[n](x),`$`(x,m)... . This definition is too cumbersome for most computer languages, but Maple handles it with ease because it thinks symbolically instead of numerically. Here is a function that evaluates it

> with(orthopoly);

[G, H, L, P, T, U]

> Pnm:=(n,m,x)-> (-1)^m*(1-x^2)^(m/2)*diff(P(n,x),x$m);

Pnm := proc (n, m, x) options operator, arrow; (-1)...

Before we do anything fancy with it we ought to test it, so let's put in numbers for n , m , and x .

> Pnm(3,1,.5);

Error, (in Pnm) wrong number (or type) of parameters in function diff

Well, we're in trouble again. The problem is with what P(n,x) returns. As we saw in the section in Chapter 2 on this function, it doesn't return a number; it returns a polynomial. When we put in 0.5 for x it goes into our Pnm function, defined above, in place of x, and then diff tries to take a derivative with respect to 0.5, which makes no sense. Watch what happens when we put in a variable in place of x instead of a number

> Pnm(3,1,t);

-sqrt(1-t^2)*(15/2*t^2-3/2)

So if you want a numerical result you have to do something like this

> a:=Pnm(3,1,t);t:=0.5;a;

a := -sqrt(1-t^2)*(15/2*t^2-3/2)

t := .5

-.3247595264

This is annoying, but on the other hand, just think about it; why do you need a number in Maple anyway? You want to plot the function, differentiate it, integrate it, use it in differential equations, etc.. What could be better than having an explicit expression for the thing? So Maple doesn't think this is a problem; it's a feature! And this feature is shared by all of the orthogonal functions you get when you use with(orthopoly).

There is one other annoying thing about the function Pnm. Watch what happens when we try to evaluate it with m=0

> Pnm(5,0,x);

Error, (in Pnm) wrong number (or type) of parameters in function diff

>

With m=0 it is supposed to give us Pn(x) back, but it doesn't. The reason it doesn't is that we just asked it to take the 0th derivative of a function, which Maple's diff command can't handle. Later when we study procedures we will come back to this problem and fix it so it works with m=0.

Ok, I have shown you how this can be done. Now I want you to make a plot of the 5 associated Legendre functions that go with P(5,x), i.e., n=5 and m=1,2,3,4,5. Plot them from x=-1..1. Put all five plots on one set of axes with different colors so you can see what happens as m ranges from 1 up to n=5. After you look at the picture you will probably want to rescale the functions so they all look about the same size. In the upcoming section on integration we will redo this plot and learn a natural way to make the functions nearer to the same size.

------------------------------------------------------------------

Problem 3.5

Here is an electricity problem borrowed from the integration section that follows this one. The potential function for a charged hemisphere of radius R with surface charge density sigma as a function of z , where z goes up the symmetry axis of the hemisphere, is given by the expression

> V:=-1/2*sigma*R*(-sqrt(R^2+z^2)+sqrt((z-R)^2))/(z*e0);

V := -1/2*sigma*R*(-sqrt(R^2+z^2)+sqrt((z-R)^2))/(z...

where e0 is shorthand for the electrical constant epsilon[0] .

>

The electric field component E[z] can be obtained from the potential V by differentiating: E[z] = -diff(V,z) . Use Maple to take this derivative and obtain an (ugly) expression for E[z] . Simplify it. You will see an unfamiliar function called csgn ; look this function up with ?csgn and make sure you understand what it does. Then set sigma = 1 , R = 1 , and e0 = 1 and plot both V and E[z] from z = -4 to z = 4 . It is a theorem in electromagnetism that when you cross surface charge density, the electric field jumps up by sigma/epsilon[0] . (You will notice that in the definition of V above I used e0 in place of epsilon[0] . This was intentional. Avoid subscripted variables whenever possible because subscripts in Maple reference array elements.) Verify on your plot that the correct jump is obtained. On your graph negative z is below the circular rim of the hemisphere; positive z from 0 to R is inside the hemisphere; and positive z from R to infinity is above the dome. Interpret your graph physically and convince yourself that it makes sense.

------------------------------------------------------------------

Problem 3.6

Here is a fancy kind of differentiation called implicit differentiation that Maple knows how to do. Suppose that you have an equation involving both x and y , like this one x^2+y^3 = 3 . You would like to find dy/dx without solving for y(x) . The way to do this is to differentiate this equation implicitly to get 2*x+3*y^2*diff(y,x) = 0 , then solve for dy/dx . Maple knows how to do it, provided that you tell it that y depends on x , like this.

> restart;

> eq:=x^2 + y(x)^3 = 3;

eq := x^2+y(x)^3 = 3

> deq:=diff(eq,x);

deq := 2*x+3*y(x)^2*diff(y(x),x) = 0

> dydx:=solve(deq,diff(y(x),x));

dydx := -2/3*x/(y(x)^2)

>

If you don't want to type y(x) all of the time, you can use Maple's alias command to tell it to turn y into y(x) (only in Maple's internal processing) whenever it is encountered.

> restart;

Allow us to use y in place of y(x)

> alias(y=y(x));

y

> eq:=x^2 + y^3 = 3;

eq := x^2+y^3 = 3

> deq:=diff(eq,x);

deq := 2*x+3*y^2*diff(y,x) = 0

> dydx:=solve(deq,diff(y,x));

dydx := -2/3*x/(y^2)

>

Here's an example where this shows up in physics. The dispersion relation for electromagnetic waves in a plasma is omega^2 = wp^2+k^2*c^2 , where wp is a frequency called the plasma frequency. The phase velocity of these waves is given by omega/k while the group velocity is given by d*omega/(d*k) . First use Maple to find formulas for both the phase and group velocities in terms of wp , k , and c by solving for omega(k) and differentiating. Then use implicit differentiation to get the group velocity in terms of k , c , and omega .

---------------------------------------------------------------------

Finally, Maple also knows how to take partial derivatives. Consider the function of x and y f(x,y) = cos(x*y)/y . Here are its derivatives with respect to x, y, and both x and y, using the expression form

> restart;f:=cos(x*y)/y;

f := cos(x*y)/y

> diff(f,x);diff(f,y);diff(f,x,y);

-sin(x*y)

-sin(x*y)*x/y-cos(x*y)/(y^2)

-cos(x*y)*x

And here is the same thing using Maple function notation

> restart;f:=(x,y)->cos(x*y)/y;

f := proc (x, y) options operator, arrow; cos(x*y)/...

> D[1](f);D[2](f);D[1,2](f);

proc (x, y) options operator, arrow; -sin(x*y) end ...

proc (x, y) options operator, arrow; -sin(x*y)*x/y-...

proc (x, y) options operator, arrow; -cos(x*y)*x en...

>

------------------------------------------------------------------

Problem 3.7

Find the two first derivatives and all three second derivatives (double x, double y, and mixed xy) of the following function

K(sqrt(4*x*y/((x+y)^2))) , where K is the complete elliptic integral EllipticK . Use expression notation with diff. Try using expand and simplify to clean up the messes that result.

Go to top of section

Integration

The single thing you will use Maple for the most is integration. In fact, many of you don't have any more idea of what an integral table is than you have about slide rules. Mostly this is OK because Maple is easily available to you and is pretty good. But it doesn't know how to do everything (as you will see in the some of the examples in this section), so you need to know where to go when Maple fails. The best place to go is a math reference book called A Table of Series and Integrals by Gradshteyn and Ryzhik. You can find it in the math reference section of the library, or maybe even in our department library, if some faculty member hasn't stolen it.

Elementary integrals

Debugging

Maple knows how to do all of the integration problems you encountered in your first calculus course. The command to do this is int , and with expressions you use it like this

> int(sin(x),x);

-cos(x)

or

> f:=sin(x)*x;int(f,x);

f := sin(x)*x

sin(x)-x*cos(x)

Note: do not use f(x) as the argument if f is an expression.

With functions the integration command works like this

> g:=(x,y)->sin(x*y)*x;

g := proc (x, y) options operator, arrow; sin(x*y)*...

> int(g(x,y),x);

(sin(x*y)-x*y*cos(x*y))/(y^2)

T

There is also an inert form of int, namely Int , which is used to display integrals. You would use this form for documenting a worksheet. Try this:

> s1:=Int(exp(x),x);

s1 := Int(exp(x),x)

But be warned: Int only displays, it doesn't do math. "But if it doesn't do anything, why would I want to use it?", you ask? Because it will help you see if you have entered the integral properly, that's why. This makes Int a very valuable debugging tool. And after you have seen that it looks right in displayed form, use value(s1) to get the answer. So the right way to do the simple integral above and get an answer is this

> s1:=Int(exp(x),x);

> s1:=value(s1);

s1 := Int(exp(x),x)

s1 := exp(x)

I suggest that you always do integrals this way, combining Int and value . It is one of those habits that will decrease the number of hours you will spend looking for stupid mistakes.

And you can do definite integrals too, like this

> s2:=Int(tan(x),x=0..1);

> s2:=value(s2);

s2 := Int(tan(x),x = 0 .. 1)

s2 := -ln(cos(1))

And if you need a number there is always

> evalf(s2);

.6156264703

Oh, and if you just want to get a numerical answer without going through evalf , just give int floating point limits and you will get it right away.

> s2:=Int(tan(x),x=0..1.);

> value(s2);

s2 := Int(tan(x),x = 0 .. 1.)

.6156264704

>

You should also know that Maple can do integrals where the limits are infinite, but you may need to give it some guidance by using the assume command. Well, that's about all you have to know if Maple can do the integral. Try ?int to see some of the extra integration options Maple has available. Now let's do some exercises.

Problem 3.8

Do the following integrals with Maple using expression notation for (a)-(d) and function notation for (e)-(g). Obtain numerical values for (e) and (f). You will have trouble with (g), and even when you get it to work the answer will look ugly. Try simplifying it with simplify .

(a) Int(ln(x),x) (b) Int(sqrt(1-x^2),x) (c) Int(x/(1+x^3),x) (d) Int(cosh(x),x)

(e) Int(sqrt((1+x)/(1-x)),x = 0 .. 1) (f) Int(x/(x^3-1),x = 0 .. 1/2) (try using both 1/2 and 1./2. as the upper limit) (g) Int(exp(-a*x)*cos(x),x = 0 .. infinity) (Don't know how to handle infinity ? Use online help.)

------------------------------------------------------------------

Problem 3.9

A common way that integrals show up in physics problems is in calculating voltages and electric fields (remember Physics 122?) Here is one that you might encounter in Physics 441 that Maple knows how to do. Consider a hemisphere described in spherical coordinates by r = R and theta = 0 .. pi/2 . Let it have surface charge density sigma . You are to find the electrostatic potential (voltage) V(z) along the z -axis.

I will get you started. The vector distance to the observation point on the z -axis is r =z k , where k is the unit vector in the z -direction. The vector distance to a little bit of charge dq = sigma*R^2*sin(theta)*d*theta*d*phi is r '=R r' where r ' is a unit vector pointing from the origin to the little bit of charge. Using the law of cosines we can then write

V(z) = Int(Int(sigma*R^2*sin(theta)/sqrt(R^2+z^2-2*...

where the integral sign here means integrate over both theta and phi . The phi integral just gives 2*pi and we are left with

V(z) = int(sigma*R^2*sin(theta)/sqrt(R^2+z^2-2*R*z*...

Use Maple's Int command to build a function V(z) that we can use later. The syntax for function building is this (setting V(z) to the dying exponential function).

> V:=z->exp(-z);

V := proc (z) options operator, arrow; exp(-z) end ...

Note: recall that you had better not use the symbol V(z); Maple interprets this literally, i.e., V(z) is the name of the function you are defining, requiring you to use the awkward syntax V(z)(z) when using it. As defined above you type V(z) when you use it. You can do the same thing with int , like this V:=z-> int(....) . In place of the symbol epsilon*o use e0, and define the constants so Maple can plot numbers. Here, I'll get you started; you fill in what goes in (...)

> restart:e0:=8.854e-12;sigma:=1e-10;R:=0.5;V:=z->int(sigma*R^2*sin(theta)/sqrt(R^2+z^2-2*R*z*cos(theta)),theta = 0 .. Pi/2)/(2*e0);

e0 := .8854e-11

sigma := .1e-9

R := .5

V := proc (z) options operator, arrow; 1/2*int(sigm...

>

You will notice that Maple doesn't give you a formula for the answer this time; it just displays the integral. I don't know why. But now that it is a function you can plot it as a function of z. Do so, from z=-5 to z=5.

Now calculate the total charge on the hemisphere and overlay plots of the true potential and the corresponding point charge potential. The two plots should agree for |z|>>R. You can get a better fit by putting the point charge somewhere other than at the origin; can you graphically find about the best value of z at which to put the point charge? Recall that the formula for the potential of a point charge is V(r) = q/(4*pi*epsilon[0]*r) .

------------------------------------------------------------------

Problem 3.10

Another place integrals show up is in classical mechanics. Consider the equation of motion of a pendulum:

diff(theta,`$`(t,2)) = -Omega^2*sin(theta)

where Omega is the small-oscillation frequency of the pendulum. If we multiply this equation by d*theta/dt and integrate once with respect to time, we get the conservation of energy theorem for this problem:

(d*theta/dt)^2/2 = Omega^2*cos(theta)-Omega^2*cos(t...

where theta[0] is the initial angular position of the pendulum before we release it from rest. We would like to know how long it takes the pendulum to get from its initial angle down to theta = 0 because this time is T/4 , one-quarter of the period of a pendulum with initial amplitude theta[0] . (a) Show that the energy equation above can be rewritten in the form

d*theta/(Omega*sqrt(2*(cos(theta)-cos(theta[0])))) ... .

This means that the quarter period of the motion is given by the integral

Int(1/(Omega*sqrt(2*(cos(theta)-cos(theta[0])))),th... .

(b) Have Maple do this integral in an attempt to get a formula for the period T of a pendulum, using s and s0 instead of theta and theta[0] . Once you try it you will probably be unimpressed with the beauty of the result. Part of the problem is that Maple doesn't know what you are going to use for theta[0] , so it gives you something incredibly general (as far as it knows, you might even want theta[0] to be complex). To tie Maple down a little, use the assume command, like this

> assume(theta0,real,0<theta0,theta0<Pi);

>

(Note: when you see these variables again they will have little ~ signs attached to them. This is Maple's way of showing that they have been restricted in meaning by an assume command.) Now you can try integrating again; it will look a little better, but it is still ugly.

You can often help Maple out by using integration tricks, like u-substitutions. Try this integral again, but use the identity cos(theta) = 1-2*sin(theta/2)^2 to change the functions inside the square root. You and Maple are a team; don't be afraid to use what you have learned in your math classes.

(c) Plot the period T as function of initial angle theta[0] from 0 to pi . Does your plot make physical sense? Check the plot for theta[0] both small and near pi .

------------------------------------------------------------------

Problem 3.11

Integrals also show up in physics when calculating the moments of inertia of spinning objects. The moment of inertia is defined by the integral int(s^2*rho,V) where s is the perpendicular distance from the spin axis to a point in the object, where rho is the mass density, and where dV is the differential volume element, e.g., dx*dy*dz in Cartesian coordinates. (a) Use Maple to find the moment of inertia of a sphere of radius R about an axis through its center. This is most easily done in spherical coordinates in which dV = r^2*sin(theta)*dr*d*theta*d*phi . If we use the z axis as the spin axis, then s = r*sin(theta) (draw a picture and convince yourself that this is true). Assume that the mass density is constant so that rho = M/(4*pi*R^3/3) . The answer is 2*M*R^2/5 . (b) Use Maple to find the moment of inertia of a cone of height L and base radius R about the symmetry axis of the cone (the axis from the sharp tip to the center of the bottom). This is most easily done in cylindrical coordinates in which dV = r*dr*d*phi*dz and s = r . Assume again that the mass density is constant so that rho = M/(pi*R^2*L/3) .

Go to top of section

Integrating special functions

Debugging

Maple can also do integrals of special functions like Bessel functions, Legendre functions, etc.. Sometimes it will give you a nice answer and sometimes it will either just give you the integral back (meaning it doesn't know how to do it) or give you an answer so ugly it makes your eyeballs bleed. When either of these bad things happens, don't give up. Sometimes if you play with the integral a little you can get a better answer. And if all you need is a number, then Maple can always give you one via evalf . For instance, suppose you needed to do this Bessel function integral

Int(J[0](x),x = 0 .. 1)

You might first try this

> restart;

> s1:=Int(BesselJ(0,x),x=0..1);

> value(s1);

s1 := Int(BesselJ(0,x),x = 0 .. 1)

BesselJ(0,1)-1/2*Pi*(BesselJ(0,1)*StruveH(1,1)-Bess...

and be annoyed because you haven't ever heard of Struve functions. What you really want is just a number, which you can get via evalf , or by the following subtle change in the command (watch the decimal points).

> s1:=Int(BesselJ(0,x),x=0..1.);

> value(s1);

s1 := Int(BesselJ(0,x),x = 0 .. 1.)

.9197304101

>

That little decimal point on the 1. makes all the difference. Ok, here are some exercises for you to practice on.

Problem 3.12

Try doing the following Bessel function integrals both at indefinite integrals and as definite integrals on the interval [1.0..2.0].

(a) int(J[0](x),x) (b) int(J[0](x)*x,x) (c) int(K[0](x),x) (d) int(I[1](x),x)

(Note that you get these functions with BesselJ(0,x) , BesselK(0,x) , and BesselI(1,x) .

------------------------------------------------------------------

Problem 3.13

Try doing the following Legendre polynomial integrals both as indefinite integrals and as definite integrals on the interval [-1.0..1.0].

(a) int(P[2](x)*x^2,x) (b) int(P[3](x)*P[5](x),x) (Note: to get these functions you have to use the command with(orthopoly) first, then P(3,x) , etc..)

------------------------------------------------------------------

Problem 3.14

You might have been surprised at the answer to part (b) of the previous problem when you did it as a definite integral. This is not just a surprise, it is a miracle, and it goes by the name of orthogonality. You will study this idea in detail in Physics 318, but this seems like a nice place to give you an introduction to it.

If I were to ask you to check whether two vectors were orthogonal, you would know just what to do. You would take their dot product, and if it turned out to be zero, then you would know that they are orthogonal. And how would you take the dot product? You would multiply the like components of the two vectors together and add them up, e.g.,

[1,2,3] dot [4,5,6] = 4 + 10 + 18 = 32

Well, an integral like this int(P[3](x)*P[5](x),x = -1 .. 1) is really just the same thing. At each value of x the two function values are multiplied together and the integral adds them all up. So it makes sense to say that two functions are orthogonal. It turns out that many functions have nice orthogonality properties, and in this exercise you will use Maple to see what they are like.

(a) Show, using Maple, that the functions sin(m*x) and cos(m*x) are orthogonal to each other when integrated over the interval [0, 2*Pi] by finding the values of the following integrals both with m and n unequal and with m = n .

int(cos(m*x)*cos(n*x),x = 0 .. 2*Pi) int(sin(m*x)*sin(n*x),x = 0 .. 2*Pi) int(cos(m*x)*sin(n*x),x = 0 .. 2*Pi)

(Hint: you will get a mess unless you use assume to tell Maple that m and n are integers. Apparently Maple assumes more than that they are integers; it also assumes that they are different integers, so you will have to do the m = n case by using the command m:=n .)

(b) Show, using Maple (this problem is an extension of Problem 3.13 above), that the Legendre functions P[n](x) are orthogonal to each other when two functions with different n -values are integrated over the interval [-1.0..1.0]. Also find the value of int(P[n](x)^2,x = -1 .. 1) . You will need to use the with(orthopoly) command to tell Maple you are going to use these functions. And when you try to find the value of the integral of P[n](x)^2 , you will probably need to try several different values of n and see if you can find the general formula empirically. This will be easier if you don't force floating point answers by using decimal points on the limits, i.e., use 1 and -1 instead of -1. and 1..

(c) Show, using Maple, that the Bessel functions J[0](alpha[n]*x) are orthogonal to each other with respect to the integral int(J[0](alpha[p]*x)*J[0](alpha[q]*x)*x,x = 0 .. 1)... , where the numbers alpha[p] and alpha[q] are the zeros of J[0](x) . These zeros are available from the Maple function BesselJZeros(n,m) , where n is the order of the Bessel function, i.e., n = 0 for J[0] , n = 1 for J[1] , etc., and where m counts the zeros ( p and q are m -values), starting with the first one. (For example BesselJZeros(0.,1) gives 2.404825558; notice the decimal point--it is important.) Then see if you can find a formula for this integral when p = q . (Hint: when you set p = q you will just get meaningless numbers because BesselJZeros gives you floating point numbers back. You can do better than this. Replace BesselJZeros(0.,n) with BesselJZeros(0,n) (no decimal point) and let Maple try to do the integral symbolically. You will get a simple answer.

------------------------------------------------------------------

Problem 3.15

Let's go back to the associated Legendre functions and plot them so that they all have about the same size. We could scale them by their maximum values, but then we would have to find the zeros of large polynomials, which is hard. It is actually easier to "calculate their magnitudes" in the same function-dot-product sense used in Problem 3.14. To find the magnitude of a vector we square the components, add the squares up, then take the square root. The corresponding operation for functions is: square the function, integrate it over the relevant interval, then take the square root. Here is the associated Legendre function definition again from Problem 3.4

> Pnm:=(n,m,x)-> (-1)^m*sqrt(1-x^2)^m*diff(P(n,x),x$m);

Pnm := proc (n, m, x) options operator, arrow; (-1)...

>

The magnitude of this function is obtained from abs(P[nm]) = sqrt(int(P[nm](x)^2,x = -1 .. 1)) . Redo the plots in Problem 3.4, but divide each of the plotted functions by this magnitude and check to see that they all have about the same size on the new plot.

---------------------------------------------------------------------------------

Problem 3.16

Finally, let's see what Maple does with the integrals of a few miscellaneous functions. Try to get Maple to do (a-c) both as indefinite integrals and as definite integrals between .5 and 1. Do (d) only as a definite integral.

(a) int(GAMMA(x),x)

(b) int(erf(x),x)

(c) int(K(x),x) (Maple's EllipticK )

(d) int(exp(-x^2.3),x = 0 .. 1) (Note: try this integral with both integer 1 and floating point 1. as the upper limit.)



This should teach you that sometimes Maple can't do indefinite integrals, that it can give you a floating point number for a definite integral but you might have to insist on it by using evalf , and that sometimes it will give you the wrong answer! (Plot exp(-x^2.3) between 0 and 1 to figure out which of the two answers Maple gave you for (d) is wrong.) Maple doesn't make many mistakes, but occasionally it louses up. This means that you have to be very careful and not trust Maple, or any other computer program, too much. Indeed, a good rule to follow upon receiving an answer from any computer program is to quietly mutter under your breath, "Well, that's probably wrong."

Go to top of section

Multiple Integrals

Debugging

Multiple integrals show up all the time in physics problems, things like Int(Int(erf(x*sin(y)),x = 0 .. 1),y = 0 .. 1) . A double integral like this can be done in Maple with nested int commands, like this

> restart;

> s1:=Int(Int(erf(x*sin(y)),x=0..1),y=0..1);

> s1:=value(s1);

s1 := Int(Int(erf(x*sin(y)),x = 0 .. 1),y = 0 .. 1)...

s1 := int(1/2*csc(y)*(-2+2*exp(-sin(y)^2)+2*sin(y)*...

As usual, if Maple just gives it back to you as a displayed integral it means it can't do it, but if you want a number you can use evalf

> evalf(s1);

.2440534496

Or, if you just wanted a number in the first place, you can tell Maple to do the integral numerically using this command

> evalf(Int(Int(erf(x*sin(y)),x=0..1.),y=0..1.));

.2440534496

A variation on this command tells Maple to only compute the integral numerically accurate to 6 (or any other number) of significant figures.

> evalf(Int(Int(erf(x*sin(y)),x=0..1.,6),y=0..1.,6));

.244054

>

You might want to use this if Maple is taking forever to give you the answer--it will go faster if you ask for fewer significant figures.

Another way to do double, and even triple, integrals is with the routines in the student package. These are a collection of routines written to support a calculus course and you can gain access to them with the command

> with(student);

[D, Diff, Doubleint, Int, Limit, Lineint, Product, ...
[D, Diff, Doubleint, Int, Limit, Lineint, Product, ...
[D, Diff, Doubleint, Int, Limit, Lineint, Product, ...

To see what these routines do and how to use them use

> ?student

------------------------------------------------------------------

Problem 3.17

Get the numerical answer to this double integral Int(Int(J[0](sqrt(x*y)),x = 1 .. 2),y = 1 .. 2) .

------------------------------------------------------------------

Advanced topic: procedure for triple integrals

It also often happens that you want to do triple integrals, perhaps like Int(Int(Int(erf(x*sin(y*z)),x = 0 .. 1),y = 0 .. 1)... . The straightforward Maple command to do this is

> restart;

> s1:=int(int(int(erf(x*sin(y*z)),x=0..1),y=0..1),z=0..1);

s1 := int(int(1/2*csc(y*z)*(-2+2*exp(-sin(y*z)^2)+2...

And when Maple gives it back unevaluated, you could use evalf again. But when you do you will notice something rather annoying--Maple takes forever to do it. The problem is that for triple integrals there are so many calculations to do that Maple itself is just too slow. You need a real number cruncher program to get the job done. And for integrals of dimension 4 or higher, even good number crunchers can take a long time. Go ahead and try evalf, but be prepared to stop the computation with the stop sign on the toolbar when you can't take it anymore.

> evalf(s1);

.1308867190

Even using Maple's numerical integration command with a limited number of significant figures takes forever.

> evalf(Int(Int(Int(erf(x*sin(y*z)),x=0..1.,4),y=0..1.,4),z=0..1.,4));

.1309

Fortunately, there is an alternative within Maple: write your own procedure to turn Maple into a "number cruncher" (insofar as this is possible). This requires that we use Maple's procedure capability which will be discussed in a later section. Don't worry yet about how it works; just know that it's here and that you can come copy it if you have to use Maple to do a triple integral.

Here is the numerical integration procedure together with the definition of the function f(x,y,z) corresponding to the example we just did so you can see how much faster it is than three nested int's. In the int3d command after the procedure try different numbers of intervals nx, ny, and nz to see if the answer converges to what Maple got when it did it to 4 significant figures. For example, you could first have nx=ny=nz=4, then nx=ny=nz=8, etc..

> f:=(x,y,z)-> erf(x*sin(y*z));

f := proc (x, y, z) options operator, arrow; erf(x*...

> int3d:=proc(x1,x2,y1,y2,z1,z2,nx,ny,nz,f) # 3-d integration procedure

>

> # x limits: x1,x2 y limits: y1,y2 z limits: z1,z2

> # number of subintervals (3 points per subinterval): nx, ny, nz

> # function f(x,y,z) passed in to be integrated: f

>

> local x,y,z,dx,dy,dz,wx,wy,wz,i,j,k,s; # define variables local to this procedure

> wx:=array(1..3*nx);wy:=array(1..3*ny);wz:=array(1..3*nz); # declare the weight arrays

>

> dx:=(x2-x1)/nx/3;dy:=(y2-y1)/ny/3;dz:=(z2-z1)/nz/3; # set the integration step sizes

>

> x:=array(1..3*nx);y:=array(1..3*ny);z:=array(1..3*nz); # declare the integration point arrays

>

> for i from 1 to nx do # load x integration points and weights

> wx[3*i-2]:=9/8.*dx;wx[3*i-1]:=3/4.*dx;wx[3*i]:=9/8.*dx;

> x[3*i-2]:=x1+(3*i-2.5)*dx;x[3*i-1]:=x1+(3*i-1.5)*dx;x[3*i]:=x1+(3*i-.5)*dx;

> end do;

>

> for j from 1 to ny do # load y integration points and weights

> wy[3*j-2]:=9/8.*dy;wy[3*j-1]:=3/4.*dy;wy[3*j]:=9/8.*dy;

> y[3*j-2]:=y1+(3*j-2.5)*dy;y[3*j-1]:=y1+(3*j-1.5)*dy;y[3*j]:=y1+(3*j-.5)*dy;

> end do;

>

> for k from 1 to nz do # load z integration points and weights

> wz[3*k-2]:=9/8.*dz;wz[3*k-1]:=3/4.*dz;wz[3*k]:=9/8.*dz;

> z[3*k-2]:=z1+(3*k-2.5)*dz;z[3*k-1]:=z1+(3*k-1.5)*dz;z[3*k]:=z1+(3*k-.5)*dz;

> end do;

>

>

> s:=0.; # set the final answer to zero

> for i from 1 to 3*nx do

> for j from 1 to 3*ny do

> for k from 1 to 3*nz do

> # loop over all the points, evaluate the function, multiply by the weights, and

> # add each contribution into the final answer using Maple's fast hardware floating

> # point eval command

>

> s:=s + evalhf(wx[i]*wy[j]*wz[k]*f(x[i],y[j],z[k])):

>

> end do:end do:end do:

>

>

>

> end; # end of int3d procedure

int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...
int3d := proc (x1, x2, y1, y2, z1, z2, nx, ny, nz, ...

Test the int3d procedure

> int3d(0,1,0,1,0,1,4,4,4,f);

.1308863653

>

I recommend that you use this procedure whenever you need to do triple integrals in Maple because it is much faster. And to tell whether you have the accuracy you desire, just try increasing (nx,ny,nz) and watch how many digits stay the same. But if you are really serious about doing a time-consuming problem like this and Maple is just taking way too long, there is still hope. There are lots faster ways of doing numerical calculations like this and you ought to learn about them sometime. For instance I coded this triple integral in two other computing languages, MATLAB and FORTRAN. All three give the same answer for the numerical approximation to the integral as long as the same number of points is used. For example, with a 12,12,12 in the int3d command above Maple takes 48 seconds to do this integral on my workstation. MATLAB takes 3.7 seconds and FORTRAN takes 0.24 seconds, 200 times faster than Maple! Number crunching like this using Maple turns your Pentium into a clunky old 286.

Problem 3.18

Use the procedure int3d defined above to get the numerical value of the following triple integral in spherical coordinates

Int(Int(Int(r^6*sin(theta)^3*cos(r*theta*phi)^2*(1+... .

Make sure your answer is accurate to 4 significant figures. Even this level of accuracy will take some time to achieve. It would be worth your while to try to find a way to do one of these integrals analytically so that you only have to do a double integral instead of a triple one.

Go to top of section

Go to top of section

Series expansions

Debugging

One of the most important mathematical ideas in all of physics is the idea of a series solution to a problem. This is so important in physics because we usually are not interested in the most exact solution to a problem, but rather in the most illuminating solution to a problem. Consider, for example, the function sech(x) . This is a function you should know, and even learned once, but have since forgotten. And even if you happen to remember that it is the reciprocal of the hyperbolic cosine function, this probably doesn't help you to visualize it very well. But what if I were to tell you that near zero this function is approximated by the simple function 1-x^2/2 ? Now you can see that it is 1 at x = 0 and that it falls off like a parabola in both directions from x = 0 . And then what if I told you that for abs(x) very large the function is approximated by the function 2*e^(-abs(x)) ? Now you can see that it falls to zero exponentially away from x = 0 , so the function just looks like a haystack.

Maple knows how to find approximations to most of the functions it deals with. The commands that find these approximations are taylor , series , and asympt ; we will see how they work in this section.

Taylor's theorem is: that if a function f(x) is defined and has well-defined derivatives at the point x = a , then for x near a the function can be approximated by the series

f(x) = f(a)+diff(f(a),x)*(x-a)+diff(f(a),`$`(x,2))*... + . . .

Maple knows this theorem and can use it to generate all kinds of series with the command taylor . For instance, here is the one for the sech function discussed above

> taylor(sech(x),x=0,20);

series(1-1/2*x^2+5/24*x^4-61/720*x^6+277/8064*x^8-5...
series(1-1/2*x^2+5/24*x^4-61/720*x^6+277/8064*x^8-5...

>

The x=0 option tells Maple to take the expansion about 0 and the 20 tells it to include terms until the error term is of order x^20 .

Problem 3.19

Find the Taylor expansions of the following functions about x = 0 and keeping terms out to x^10 .

(a) sin(x) (b) cos(x) (c) arctan(x) (d) exp(x) (e) ln(1+x) (f) (1+x)^p

Your answers will look pretty impressive, but you will find that in most physics problems where expansions are important, we only keep the first two terms, so you can often get what you want without all of the clutter by using taylor(f(x),x=0,3) , which just goes through x^2 .

------------------------------------------------------------------

Problem 3.20

Maple can also do Taylor expansions of special functions. Find the expansions of (a) J[0](x) , (b) I[2](x) , (c) K(x) (complete elliptic integral EllipticK ), and (d) GAMMA(x) . Do (a)-(c) about x = 0 and (d) about x = 1 .Do each one through x^10 . The answer to (d) is pretty awful--see if you can get something more reasonable by using evalf , or by telling Maple to expand about x=1.0 (decimal point again).

------------------------------------------------------------------

Problem 3.21

Now try expanding the Bessel function K[0](x) about x = 0 . Maple will fail, but it will make a suggestion, and it is a good one. If the function in question has a singularity of some kind at the expansion point, then either the function or some of its derivatives do not exist at that point and a Taylor series is not possible. But there are other kinds of series that can approximate functions, and Maple's series command, which works almost like taylor (but is more general) can give them to you. Try series(BesselK(0,x),x=0,3); and see what happens. Notice that by ignoring most of this mess and concentrating just on the leading term you can tell that K[0](x) is logarithmically singular. (Notice also that Euler's constant gamma appears in this expansion. To learn a little about it use ?gamma .) When you get this to work, try the same thing on the following functions:

(a) cot(x) (b) 1/tanh(x) (c) sqrt(x)/sin(x^2) .

Try different numbers in place of the 3 in the series command above and see if you can tell precisely what it means.

------------------------------------------------------------------

Problem 3.22

There is another kind of expansion that is very useful in mathematical physics: the asymptotic expansion. It is an expansion about infinity , which seems odd, but all it means is that we want to know how a function behaves when its argument is really big. The Maple command that does this is asympt(f,x,2) (or series(f,x=infinity,2) ) where f is a Maple expression. Try this on the following functions using series on (a)-(b) and asympt on (c)-(d). Only keep 2 terms, as indicated in the sample commands just above.

(a) J[0](x) (b) I[1](x) (c) K[0](x) (d) GAMMA(x) .

I have suggested that you only put a 2 in the last slot because usually all we care about in an asymptotic expansion is the leading term because it answers the most important questions: (1) does the function blow up or die? and (2) in what way does it do it?

------------------------------------------------------------------

Problem 3.23

Here's an example of how we use this idea to solve physics problems. The equation of