Evaluating Triple Integrals

43
Evaluating Triple Integrals copyright © 2009 by Jonathan Rosenberg based on an earlier M-book, copyright © 2000 by Paul Green and Jonathan Rosenberg Contents Evaluating Triple Iterated Integrals Example 1 Problem 1 Finding the Limits Example 2 Problem 2 Example 3 Problem 3 Additional Problems: Evaluating Triple Iterated Integrals In passing from double to triple integrals, there is much less that is novel than in passing from single to double integrals. MATLAB has a built-in triple integrator triplequad similar to dblquad, but again, it only integrates over rectangular boxes. However, more general threefold iterated integrals can be formulated and evaluated using the symbolic toolbox's routines, in the same manner as twofold iterated integrals. And if the first of the three integrals can be evaluated symbolically, it is then possible to use dblquad for the remaining integrations. Example 1 We will begin by considering the iterated integral We can evaluate this easily as syms x y z firstans=int(int(int(x*y*z,z,0,3-x-y),y,0,3-x),x,0,3)

Transcript of Evaluating Triple Integrals

Page 1: Evaluating Triple Integrals

Evaluating Triple Integralscopyright © 2009 by Jonathan Rosenberg based on an earlier M-book, copyright © 2000 by Paul Green and Jonathan Rosenberg

Contents

Evaluating Triple Iterated Integrals Example 1 Problem 1 Finding the Limits Example 2 Problem 2 Example 3 Problem 3 Additional Problems:

Evaluating Triple Iterated Integrals

In passing from double to triple integrals, there is much less that is novel than in passing from single to double integrals. MATLAB has a built-in triple integrator triplequad similar to dblquad, but again, it only integrates over rectangular boxes. However, more general threefold iterated integrals can be formulated and evaluated using the symbolic toolbox's routines, in the same manner as twofold iterated integrals. And if the first of the three integrals can be evaluated symbolically, it is then possible to use dblquad for the remaining integrations.

Example 1

We will begin by considering the iterated integral

We can evaluate this easily as

syms x y zfirstans=int(int(int(x*y*z,z,0,3-x-y),y,0,3-x),x,0,3) firstans = 81/80

We can also evaluate the first integral only, and then use dblquad or numint2 for the remaining two integrations:

f=int(x*y*z,z,0,3-x-y)secondans=numint2(f,y,0,3-x,x,0,3) f =

Page 2: Evaluating Triple Integrals

(x*y*(x + y - 3)^2)/2

secondans =

1.0125

Let's see how the answers compare:

double(firstans-secondans)ans =

0

To use triplequad here, we would again have to make a change of variables

So in the new coordinates, the integral is over the box 0<x<3, 0<u<1, 0<v<1 and the integrand is

and the integral can be computed as

h = @(x,u,v) x.*(3-x).^4.*u.*(1-u).^2.*v;thirdans = triplequad(h, 0, 3, 0, 1, 0, 1)thirdans =

1.0125

with accuracy

double(firstans-thirdans)ans =

0

But because dblquad is sometimes slow (and the same algorithm extrapolated to three dimensions would be even slower) or doesn't work very well, we have looked for an alternative, based on a freeware package of M-files called the numerical integration toolbox. This toolbox is much faster and more adaptable (though admittedly sometimes harder to use) than dblquad. Copy this package from http://www.math.umd.edu/users/jmr/241/mfiles/nit/ into a subdirectory of your working directory called nit. To use this package, first add the package with the command:

Page 3: Evaluating Triple Integrals

addpath nit

To see how it works, we first try out an alternative version of numint2, based on nit, called newnumint2. The syntax is the same as for numint2, but there is an optional final parameter, which is the number of subdivision points to use in each direction. (The default is 20.) The higher this parameter, the more accurate the results; however, the running time of the algorithm is proportional to the square of this parameter, so be careful about increasing it.

fourthans=newnumint2(f,y,0,3-x,x,0,3)fourthans =

1.0125

Note that this answer looks the same as the first two to four decimal places, but that we are spared all the annoying warning messages. The accuracy is:

double(firstans-fourthans)ans =

0

which is pretty good!! Now we try a totally numerical integration. The analogue of newnumint2 for triple integrals is called numint3. Again there is an optional final parameter, which is the number of subdivision points to use in each direction. (The default is 8.) The higher this parameter, the more accurate the results; however, the running time of the algorithm is proportional to the cube of this parameter, so be extra careful about increasing it.

fourthans=numint3(x*y*z,z,0,3-x-y,y,0,3-x,x,0,3)fourthans =

1.0125

The accuracy is:

double(firstans-fourthans)ans =

0

Again pretty good!

Problem 1

Evaluate the threefold iterated integral

Page 4: Evaluating Triple Integrals

both symbolically and numerically. (For the numerical integration, first do the integral over z, then use newnumint2 on this intermediate result.)

Finding the Limits

The remaining issue in the evaluation of triple integrals is the determination of limits. Once this has been done, the actual integration can always be performed by one of the methods illustrated above.

Example 2

Let us consider the region above the paraboloid

and inside the ellipsoid

We might begin by plotting the two surfaces simultaneously. Since z is never negative on or above the paraboloid, we are interested only in positive values of z. Consequently, we will solve the equation of the ellipsoid for positive z and use ezmesh to plot both surfaces.

ezmesh(sqrt(20-3*x^2-2*y^2),[-3,3,-2,2]);hold onezmesh(2*x^2+3*y^2, [-3,3,-2,2])view([1,2,3])hold off;

Page 5: Evaluating Triple Integrals

From the viewpoint we have chosen, using MATLAB's view comand, we can see that there is a region inside the ellipsoid that is bounded below by the paraboloid and above by the ellipsoid. This determines the lower and upper limits for z. Accordingly, we set

z1=2*x^2 + 3*y^2z2=sqrt(20-3*x^2-2*y^2) z1 = 2*x^2 + 3*y^2 z2 = (20 - 2*y^2 - 3*x^2)^(1/2)

To determine the limits for x and y, it may be helpful to plot the curve along which the upper and lower limits for z coincide. We will do this using ezplot:

ezplot(z2-z1, [-3, 3, -2, 2])

Page 6: Evaluating Triple Integrals

Although this looks like an ellipse, it is actually a quartic curve. We will establish the limits by solving the equation of the curve for y as a function of x. We expect two solutions, corresponding to the upper and lower y limits, and we should be able to determine the x limits by setting the y limits equal to each other.

ylims=solve(z2-z1,y) ylims = ((4*(181 - 15*x^2)^(1/2))/9 - (8*x^2)/3 - 4/9)^(1/2)/2 (- (8*x^2)/3 - (4*(181 - 15*x^2)^(1/2))/9 - 4/9)^(1/2)/2 -((4*(181 - 15*x^2)^(1/2))/9 - (8*x^2)/3 - 4/9)^(1/2)/2 -(- (8*x^2)/3 - (4*(181 - 15*x^2)^(1/2))/9 - 4/9)^(1/2)/2

Here solve has found four solutions instead of two. This should not surprise us too much since the equation in question is quartic. Of the four solutions, however, two of them are complex, as we can see by substituting x=0.

double(subs(ylims,x,0))ans =

1.1763 0.0000 + 1.2673i -1.1763 -0.0000 - 1.2673i

Page 7: Evaluating Triple Integrals

Thus solution #3 should be the lower limit for y, solution #1 should be the upper limit, and solutions #2 and 4 are complex and should be discarded. So we set

y1=ylims(3)y2=ylims(1)xlims=solve(y2-y1,x)x1=double(xlims(2))x2=double(xlims(1)) y1 = -((4*(181 - 15*x^2)^(1/2))/9 - (8*x^2)/3 - 4/9)^(1/2)/2 y2 = ((4*(181 - 15*x^2)^(1/2))/9 - (8*x^2)/3 - 4/9)^(1/2)/2 xlims = (329^(1/2)/2 - 3/2)^(1/2)/2 -(329^(1/2)/2 - 3/2)^(1/2)/2

x1 =

-1.3756

x2 =

1.3756

We can now check our limits for geometric plausibility. For this purpose we have written an M-file called viewSolid.

type viewSolidfunction viewSolid(zvar, F, G, yvar, f, g, xvar, a, b)%VIEWSOLID is a version for MATLAB of the routine on page 161% of "Multivariable Calculus and Mathematica" for viewing the region% bounded by two surfaces for the purpose of setting up triple integrals. % The arguments are entered from the inside out. % There are two forms of the command --- either f, g,% F, and G can be vectorized functions, or else they can% be symbolic expressions. xvar, yvar, and zvar can be% either symbolic variables or strings.% The variable xvar (x, for example) is on the % OUTSIDE of the triple integral, and goes between CONSTANT limits a and b.% The variable yvar goes in the MIDDLE of the triple integral, and goes % between limits which must be expressions in one variable [xvar].% The variable zvar goes in the INSIDE of the triple integral, and goes% between limits which must be expressions in two % variables [xvar and yvar]. The lower surface is plotted in red, the

Page 8: Evaluating Triple Integrals

% upper one in blue, and the "hatching" in cyan.%% Examples: viewSolid(z, 0, (x+y)/4, y, x/2, x, x, 1, 2)% gives the picture on page 163 of "Multivariable Calculus and Mathematica" % and the picture on page 164 of "Multivariable Calculus and Mathematica"% can be produced by% viewSolid(z, x^2+3*y^2, 4-y^2, y, -sqrt(4-x^2)/2, sqrt(4-x^2)/2, ...% x, -2, 2,)% One can also type viewSolid('z', @(x,y) 0, ...% @(x,y)(x+y)/4, 'y', @(x) x/2, @(x) x, 'x', 1, 2)%

if isa(f, 'sym') % case of symbolic input ffun=inline(vectorize(f+0*xvar),char(xvar)); gfun=inline(vectorize(g+0*xvar),char(xvar)); Ffun=inline(vectorize(F+0*xvar),char(xvar),char(yvar)); Gfun=inline(vectorize(G+0*xvar),char(xvar),char(yvar)); oldviewSolid(char(xvar), double(a), double(b), ... char(yvar), ffun, gfun, char(zvar), Ffun, Gfun)else oldviewSolid(char(xvar), double(a), double(b), ... char(yvar), f, g, char(zvar), F, G)end%%%%%%% subfunction goes here %%%%%%function oldviewSolid(xvar, a, b, yvar, f, g, zvar, F, G)for counter=0:20 xx = a + (counter/20)*(b-a); YY = f(xx)*ones(1, 21)+((g(xx)-f(xx))/20)*(0:20); XX = xx*ones(1, 21);%% The next lines inserted to make bounding curves thicker. widthpar=0.5; if counter==0, widthpar=2; end if counter==20, widthpar=2; end%% Plot curves of constant x on surface patches. plot3(XX, YY, F(XX, YY).*ones(1,21), 'r', 'LineWidth', widthpar); hold on plot3(XX, YY, G(XX, YY).*ones(1,21), 'b', 'LineWidth', widthpar);end;%% Now do the same thing in the other direction.XX = a*ones(1, 21)+((b-a)/20)*(0:20); %% Normalize sizes of vectors.YY=0:2; ZZ1=0:20; ZZ2=0:20;for counter=0:20,%% The next lines inserted to make bounding curves thicker. widthpar=0.5; if counter==0, widthpar=2; end if counter==20, widthpar=2; end for i=1:21, YY(i)=f(XX(i))+(counter/20)*(g(XX(i))-f(XX(i))); ZZ1(i)=F(XX(i),YY(i)); ZZ2(i)=G(XX(i),YY(i)); end; plot3(XX, YY, ZZ1, 'r', 'LineWidth',widthpar); plot3(XX, YY, ZZ2, 'b', 'LineWidth',widthpar);end;%% Now plot vertical lines.for u = 0:0.2:1, for v = 0:0.2:1,

Page 9: Evaluating Triple Integrals

x=a + (b-a)*u; y = f(a + (b-a)*u) +(g(a + (b-a)*u)-f(a + (b-a)*u))*v; plot3([x, x], [y, y], [F(x,y), G(x, y)], 'c'); end;end;xlabel(xvar)ylabel(yvar)zlabel(zvar)hold off

It takes a proposed set of limits of integration (written from the inside to the outside) and draws a picture of the corresponding region.

viewSolid(z,z1,z2,y,y1,y2,x,x1,x2)

This looks reasonable; we may now use the limits we have found to integrate any function we like over the region in question.

Problem 2

Find the appropriate limits for integrating over the region bounded by the paraboloids

and

Page 10: Evaluating Triple Integrals

Check your results using viewSolid.

What is special about the examples we have just considered is that the region in question is bounded by two surfaces, each of which has an equation specifying z as a function of x and y. The procedure we have used in this case will work for any such region, as long as solve is able to solve the associated equations. We will look now at an example of another sort.

Example 3

We consider the region interior to both of the elliptical cylinders, x^2+2y^2 = 9 and 2x^2+z^2 = 8. We begin by observing that the condition that a point be interior to both cylinders constrains x^2 to be at most 4. We also observe that, for fixed x, the limits for y and z are determined independently of each other. So we'll want to put the x-integral on the outside. We can immediately set

x1=-2; x2=2;y1=-sqrt((9-x^2)/2); y2=-y1;z1=-sqrt(8-2*x^2); z2=-z1;

We can again use viewSolid to check the plausibility of these limits, although the resulting plot is more difficult to interpret in this case, since it shows only the upper and lower boundaries as surfaces; the side boundaries are shown as ensembles of vertical lines.

viewSolid(z,z1,z2,y,y1,y2,x,x1,x2)

Problem 3

Page 11: Evaluating Triple Integrals

Find limits appropriate for integrating over the solid region bounded by the paraboloid y = x^2 + z^2 - 2 and the cylinder x^2 + y^2 = 1. Check your results with viewSolid.

Additional Problems:

1. Find the volume of each of the solid regions considered in Examples 2 and 3 and Problems 2 and 3 above. Use at least two different methods in each case and compare the answers.

2. Integrate the function x^2 + y*z over the solid region above the paraboloid z = x^2 + y^2 and below the plane x + y + z = 10.

3. Integrate the function x^2 + 2y^2 + 3z^2 over the solid region interior to both the sphere x^2 + y^2 + z^2 = 9 and the cylinder (x-1)^2 + y^2 = 1.

Published with MATLAB® 7.8

ezsurf('sin(sqrt(x^2+y^2))',[0,pi,0,pi])

Multiple Integrals

Matlab can handle iterated multiple integrals easily just by nesting regular integrals. For example think about what the following command does. Hint: Think from the inside out.

syms x y;int(int(x*y,y,x^3,x^2),x,0,1)

Page 12: Evaluating Triple Integrals

The Justin Guide to MATLAB for MATH 241 plus Project 3.The basic format of this guide is that you will sit down with this page open and a MATLAB window open and you will try the guide as you go. Once you are finished following through the guide the project should be very easy.

Contents

M-Files and Publishing Parametrized Surfaces Surface Integrals of Functions Surface Integrals of Vector Fields. Line Integrals of Functions. Line Integrals of Vector Fields. Vector Fields Vector Fields in 3D Project 3

M-Files and Publishing

Your third project will be submitted as a published M-file, just as your second project was.

For your project simply publish your M-file and print the resulting HTML document.

Parametrized Surfaces

Matlab can plot parametrized surfaces quite easily. If we have a surface parametrized as r(u,v)=x(u,v)i+y(u,v)j+z(u,v)k then we give this to Matlab as a vector. For example here is a cylinder defined in terms of theta and z. We'll call it rbar.

syms theta zrbar=[2*cos(theta),2*sin(theta),z];

We then plot this with ezsurf and that's when we give it the restrictions on u and v, or in this case z and theta. However ezsurf wants the three components separately so we access them like a vector. Note the axis equal command which makes sure that the aspect ratio is proper.

ezsurf(rbar(1),rbar(2),rbar(3),[0,2*pi,0,5])axis equal

Page 13: Evaluating Triple Integrals

It's extremely important to know that the constraints are in alphabetical order so 0,2*pi corresponds to theta and the 0,5 corresponds to z.

Here's another example, this time a piece of the plane 2x+y+z=10 with 0<=x<=5 and -2<=z<=2. In this case we solve for y=10-2x-z:

syms x zrbar=[x,10-2*x-z,z];ezsurf(rbar(1),rbar(2),rbar(3),[0,5,-2,2])axis equal

Page 14: Evaluating Triple Integrals

Here's a piece of a sphere:

syms phi thetarbar=[2*sin(phi)*cos(theta),2*sin(phi)*sin(theta),2*cos(phi)]ezsurf(rbar(1),rbar(2),rbar(3),[0,pi/2,0,3*pi/2])axis equal rbar = [ 2*cos(theta)*sin(phi), 2*sin(phi)*sin(theta), 2*cos(phi)]

Page 15: Evaluating Triple Integrals

Surface Integrals of Functions

The nice thing about having a surface in Matlab like this is that now we can do things with it. For example we know that if we want to integrate a real-valued function over a surface we need the magnitude of the cross products of the derivatives of r. Since the matlab norm command has issues with variables we need to create our own command to find the length of a vector. This is just the square root of the dot product of a vector with its transpose.

rbar=[2*sin(phi)*cos(theta),2*sin(phi)*sin(theta),2*cos(phi)]mylength=@(u) sqrt(u*transpose(u));simplify(mylength(cross(diff(rbar,phi),diff(rbar,theta)))) rbar = [ 2*cos(theta)*sin(phi), 2*sin(phi)*sin(theta), 2*cos(phi)] ans = 4*(sin(phi)^2)^(1/2)

So now here's the really nice thing. Suppose we want to integrate the function f(x,y,z)=x^2+y^2 over that surface. This involves plugging the i,|j|,|k| components in for x, y, z, multiplying by that magnitude and integrate over the appropriate limits. Here it is all together:

clear

Page 16: Evaluating Triple Integrals

syms phi theta x y z;rbar=[2*sin(phi)*cos(theta),2*sin(phi)*sin(theta),2*cos(phi)]f=x^2+y^2;mylength=@(u) sqrt(u*transpose(u));mag=simplify(mylength(cross(diff(rbar,phi),diff(rbar,theta))));sub=subs(f,[x,y,z],rbar);int(int(sub*mag,phi,0,pi/2),theta,0,3*pi/2) rbar = [ 2*cos(theta)*sin(phi), 2*sin(phi)*sin(theta), 2*cos(phi)] ans = 16*pi

Make sure you read this carefully (after the clear) to see what each line does. The first line sets the symbolic variables. The second line is the parametrization of the surface. The third line is the function to integrate. The fourth line creates a length function we'll need later. The fifth line find the magnitude of the cross product of the derivatives. The sixth line substitutes the components from the parametrization into the real-valued function we want to integrate. The seventh and final line does the double integral required.

Surface Integrals of Vector Fields.

Similarly we can take the surface integral of a vector field. We only need to be careful in that Matlab can't take care of orientation so we'll need to do that and instead of needing the magnitude of the cross product we just need the cross product. Here's problem 6 from the 15.6 exercises.

clearsyms theta x y z;rbar=[cos(theta),y,sin(theta)];F=[x,y,z];kross=simplify(cross(diff(rbar,theta),diff(rbar,y)));sub=subs(F,[x,y,z],rbar);int(int(dot(sub,kross),y,-2,1),theta,0,2*pi) ans = (-6)*pi

Again read this carefully. After the clear the first line sets the symbolic variables. The second sets the parametrization and the third sets the vector field. The fourth finds the cross product of the derivatives. The fifth substitutes the parametrization into the vector field. The sixth does the double integral of the dot product as required for the surface integral of a vector field.

Line Integrals of Functions.

Page 17: Evaluating Triple Integrals

Back in the first guide we figured out how to plot curves. we can do line integrals in a systematic way like we did surface integrals. For example here's example 1 from page 1006 of the text. Read this carefully to see how it works!

clearsyms t x y z;rbar=[t,-3*t,2*t];f=x+y^2-2*z;mylength=@(u) sqrt(u*transpose(u));mag=simplify(mylength(diff(rbar,t)));sub=subs(f,[x,y,z],rbar);int(sub*mag,t,0,1) ans = (3*14^(1/2))/2

Line Integrals of Vector Fields.

Lastly here's example 6 from page 1011 of the text. Again read carefully and understand!

clearsyms t x y z;rbar=[t,t^2,t^3];F=[x*y,3*z*x,-5*x^2*y*z];sub=subs(F,[x,y,z],rbar);int(dot(sub,diff(rbar,t)),0,1) ans = -1/4

Vector Fields

Matlab can plot vector fields using the quiver command, which basically draws a bunch of arrows. This is not completely obvious though. First we have to set up a grid of points for which to plot arrows.

Suppose we want our grid to have x going from -5 to 5 of x and the same for y. We do

[x,y]=meshgrid(-5:1:5,-5:1:5);

Now suppose we wish to plot the vector field F(x,y)=(y/5)i-(x/5)j. To do this we type

quiver(x,y,y/5,-x/5,0);

Page 18: Evaluating Triple Integrals

A window will pop up with the vector field on it. The 0 at the end just ensures that Matlab does not do any tricky rescaling of the vectors, something it usually does to make things fit nicely.

Just so you know the first two entries x,y indicate that vectors should be placed at x,y. The second two entries form the vector itself.

A note of caution. Suppose we want the vector field F(x,y)=(y/5)i-(x/y)j. Since x and y are collections of numbers, to divide them we can't use /, we must use ./ instead. This is a Matlab quirk since we're not working with individual numbers. Thus we would need

quiver(x,y,y/5,-x./y,0)

Page 19: Evaluating Triple Integrals

Note that this is a pretty ugly vector field. Try it!

Vector Fields in 3D

For 3D vector fields we use quiver3. Here's an example. It's the only one we'll do because they can be pretty overwhelming.

[x,y,z]=meshgrid(-5:2:5,-5:2:5,-5:2:5);quiver3(x,y,z,-y./sqrt(x.^2+y.^2),x./sqrt(x.^2+y.^2),0)

Page 20: Evaluating Triple Integrals

Project 3

See the project on the main course page.

Published with MATLAB® 7.8

Cylindrical Coordinates in Matlab

In this activity we will show that a suitable change of coordinates can greatly

imporve the look of a surface in three-space. We will concentrate on cylindrical

coordinates in this activity, but we will address spherical coordinates in a later

activity.

Suppose, for example, that we wish to use Matlab to plot the surface

represented by the function

`z=sqrt(x^2+y^2)`.

Page 21: Evaluating Triple Integrals

A standard drawing technique involves drawing traces of the function in the

coordinate planes. For example, to see the trace in the `xz`-plane, set `y=0` to

obtain `z=sqrt(x^2)`, whose graph is shown in Figure 1 (Recall that

`sqrt(x^2)=|x|`.)

The graph of `z=sqrt(x^2)` in the `xz`-plane.

In similar fashion, set `x=0` in `z=sqrt(x^2+y^2)` to obtain the trace in the

`yz`-plane having equation `z=sqrt(y^2)`. See Figure 2.

The graph of `z=sqrt(y^2)` in the `yz`-plane.

Setting `z=0` in `z=sqrt(x^2+y^2)` yields `0=sqrt(x^2+y^2)`, or

equivalently, `0=x^2+y^2`, whose graph is the single point `(0,0)`. Thus, the

trace in the `xy`-plane is the point `(0,0)`.

To flesh out the rest of the surface, we take parallel cross sections for various

values of `z`.

If we let `z=1` in `z=sqrt(x^2+y^2)`, we eventually arrive at

`x^2+y^2=1`, which is a circle of radius 1, which we sketch at a

height of `z=1`.

If we let `z=2` in `z=sqrt(x^2+y^2)`, we eventually arrive at

`x^2+y^2=4`, which is a circle of radius 2, which we sketch at a

height of `z=2`.

Putting together the traces and the parallel cross sections leads us to believe

that the surface defined by `z=sqrt(x^2+y^2)` is a cone.

Sketching the Surface in Cartesian Coordinates

Continuing with our example, let's sketch the surface represented by

`z=sqrt(x^2+y^2)` using Cartesian coordinates. First, create a mesh on the

domain `{(x,y): -2 le x,y le 2}`.

x=linspace(-3,3,40);

Page 22: Evaluating Triple Integrals

y=linspace(-3,3,40);[x,y]=meshgrid(x,y);

Note that the above commands set up a grid of points on the domain

`{(x,y):\ -3 le x, y le 3}`.

Next, calculate the value of `z` (using `z=y^2`) at each point in the grid

determined by the matrices x and y returned by the meshgrid command. You

can either enter the following commands in a script or you can execute them at

the Matlab prompt in the command window.

x=linspace(-3,3,40);y=x;[x,y]=meshgrid(x,y);

Remember, Matlab's meshgrid command returns matrices in the variables x

and y. Thus, we want to use array operations (dot-notation) when evaluating

the function `z=sqrt(x^2+y^2)` at each point of the grid determined by the

matrices x and y.

z=sqrt(x.^2+y.^2);

Use the mesh

command to draw the surface.

mesh(x,y,z)

Add a grid and turn "on" the box, adding depth to the visualization. We can also

"tighten" the axes to the surface with the axis tight command.

grid onbox onaxis tight

Finally, add some annotations.

xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')title('The graph of z = sqrt(x^2+y^2) in three-space.')

Page 23: Evaluating Triple Integrals

Finally, rotate the view into the standard orientation used in class.

view([130,30])

The above sequence of commands will produce the image in Figure 3.

The graph of `z=sqrt(x^2+y^2)` on `{(x,y): -3 le x, y le 3}`.

The result in Figure 3 is adequate, but somewhat disappointing. The traces in

the `xz`- and `yz`-planes are certainly the "V's" we saw in Figures 1 and 2, and

it's easy to note that cross sections parallel to the `xy`-plane are circles. Still,

the image in Figure 3 is not what we usually think of when we hear the word

"cone." We're more apt to think of something shaped like a paper snow-cone

holder.

We can greatly improve the image in Figure 3 if we use cylindrical coordinates.

Cylindrical Coordinates

Page 24: Evaluating Triple Integrals

In Figure 4, we rotate the `xz`-plane through an angle `theta`, then move

outward a radial distance `r`, then upward a distance `z`. The cylindrical

coordinates of the point `P` are then `(r,theta,z)`.

Point `P` has cylindrical coordinates `(r,theta,z)`.

The connection to polar coordinates in Figure 4 is clear, as the `x`-, `y`- and

`z`-values of point `P` are given by the equations of transformation

$\begin{eqnarray} x&=&r\cos\theta\\ y&=&r\sin\theta\\ z&=&z \end{eqnarray}

$

Moreover, note that

`r=sqrt(x^2+y^2)`.

Now, substitute this last result in our equation of the cone, `z=sqrt(x^2+y^2)`,

to obtain the equation in cylindrical coordinates:

`z=r`.

In `z=r`, it is important to note that `z` is a function of `r` and `theta`, even

though `theta` is not explicitly mentioned. That is, `z=f(r,theta)`. This indicates

that we must use Matlab's meshgrid command to create a grid of `(r,theta)`

points to substitute into the equation `z=r`.

Let's choose `r` so that `0 le r le 2` (remember that parallel cross section that

was a circle of radius 2?). Also, let's choose `theta` such that `0 le theta le 2

pi`. The reason for this last choice is clear if you examine anew Figure 4.

Clearly, to obtain the desired cone, we must allow a complete revolution about

the `z`-axis.

r=linspace(0,2,20);theta=linspace(0,2*pi,40);[r,theta]=meshgrid(r,theta);

Page 25: Evaluating Triple Integrals

Now we use the equations that transform cylindrical coordinates into Cartesian

coordinates, namely `x=r cos theta` and `y=r sin theta`. Remember, r and

theta are matrices, so we use array notation.

x=r.*cos(theta);y=r.*sin(theta);

Next, compute `z`.

z=r;

Draw the mesh.

mesh(x,y,z)

Add the usual amendments and annotations.

grid onbox onaxis tightview([130,30])xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')title('The graph of z = sqrt(x^2 + y^2) in three-space.')

The above sequence results in the beautifully formed cone shown in Figure 5.

Page 26: Evaluating Triple Integrals

The surface `z=r` is a cone.

How About a Cylinder?

We would expect that cylindrical coordinates would be an excellent way to

describe and graph a cylinder! Consider, for example, the equation

`x^2+y^2=1`.

As we saw in the activity Drawing Cylinders in Matlab

, the sketch of `x^2+y^2=1` was a cylinder of radius 1. If we use the

cylindrical transformation `r=sqrt(x^2+y^2)`, or equivalently,

`r^2=x^2+y^2`, then `x^2+y^2=1` becomes

`r^2=1`.

Taking the square root,

`r=1`.

Page 27: Evaluating Triple Integrals

There is no need for the negative square root of 1 here as making a complete

revolution in `theta` (`0 le theta le 2 pi`) allows access to all octants in three

space. Indeed, `r=1` is completely expected, as `x^2+y^2=1` is a cylinder of

radius 1.

To plot the surface defined by `r=1` in Matlab, we must first realize that `r` is a

function of `z` and `theta`, even though they are not explicitly mentioned in

the equation `r=1`. That is, `r=f(z,theta)`. We need to make this observation in

order to understand that we must create a grid of `(z,theta)` points to

substitute into the equation `r=1`.

Let's choose `z` so that `-2 le z le 2`. Let's choose `theta` again so that `0 le

theta le 2 pi`.

z=linspace(-2,2,40);theta=linspace(0,2*pi,40);[z,theta]=meshgrid(z,theta);

Compute `r`.

r=1;

Compute `x` and `y` using the equations that transform cylindrical into

Cartesian coordinates.

x=r*cos(theta);y=r*sin(theta);

Note that `r=1` is a scalar, so ordinary multiplication is used in this case.

Draw the mesh.

mesh(x,y,z)

Add the usual amendments and annotations.

grid onbox onaxis tightview([130,30])

Page 28: Evaluating Triple Integrals

xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')title('The graph of x^2 + y^2 = 1 in three-space.')

The above sequence results in the beautifully formed cylinder shown in Figure

6.

The surface `r=1` is a cylinder.

Matlab Files

Although the following file features advanced use of Matlab, we include it here

for those interested in discovering how we generated the images for this

activity. You can download the Matlab file at the following link. Download the

file to a directory or folder on your system.

coordcyl.m

The file coordcyl.m is designed to be run in "cell mode." Open the file

coordcyl.m in the Matlab editor, then enable cell mode from the Cell Menu.

Page 29: Evaluating Triple Integrals

After that, use the entries on the Cell Menu or the icons on the toolbar to

execute the code in the cells provided in the file. There are options for

executing both single and multiple cells. After executing a cell, examine the

contents of your folder and note that a PNG file was generated by executing the

cell.

Exercises

For each of the following exercises, include the following in your homework

submission:

Pencil and paper calculations needed to change the given equation to

cylindrical coordinates.

A printout of the final plot.

A printout of the M-code used to create the plot.

1. Use cylindrical coordinates to sketch the surface defined by the

equation `z=x^2+y^2`. This surface is called a paraboloid.

2. Use cylindrical coordinates to sketch the surface defined by the

equation `x^2+y^2=9`.

3. Use cylindrical coordinates to sketch the surface defined by the

equation `y=2 sqrt(x^2+z^2)`.

o Hint: Note that in this case `y` is a function of `x` and `z`;

i.e., `y=f(x,z)`. Thus, you need to set up a grid of `(x,z)`

points to substitute into the function.

o Additional hint: Instead of the rotation shown in Figure 4,

imagine a rotation of the of the `xy`-plane about the `y`-

axis by an angle of `theta`, then moving outward a radial

distance `r`, then upward a distance `y` (provide a hand

drawn sketch of this image). Show then that the cylindrical

coordinate transformations become:

Page 30: Evaluating Triple Integrals

$\begin{eqnarray} x&=&r\cos\theta,\\ y&=&y,\\ z&=&r \sin\

theta. \end{eqnarray}$

Use these transformations to sketch the surface

represented by the equation `y=2sqrt(x^2+z^2)`.

4. Sketch the cylindrical surface defined by the equation

`y^2+z^2=16`. Hint: See the hint in exercise #3.

Surface Integralscopyright © 2009 by Jonathan Rosenberg based on an earlier M-book, copyright © 2000 by Paul Green and Jonathan Rosenberg

Contents

Surface Area and Surface Integrals Example 1 Example 2 Problem 1 Flux Integrals Example 3 Problem 2 The Divergence Theorem Example 4 Problem 3 Additional Problems

Surface Area and Surface Integrals

In this lesson, we will study integrals over parametrized surfaces. Recall that a surface is an object in 3-dimensional space that locally looks like a plane. In other words, the surface is given by a vector-valued function P (encoding the x, y, and z coordinates of points on the surface) depending on two parameters, say u and v. The key idea behind all the computations is summarized in the formula

Since P is vector-valued,

Page 31: Evaluating Triple Integrals

are vectors, and their cross-product is a vector with two important properties: it is normal to the surface parametrized by P, and its length gives the scale factor between area in the parameter space and the corresponding area on the surface. Thus, taking lengths on both sides of the above formula above gives

which enables us to compute the area of a parametrized surface, or to integrate any function along the surface with respect to surface area.

Example 1

To see how this works, let us compute the surface area of the ellipsoid whose equation is

We may parametrize this ellipsoid as we have done in the past, using modified spherical coordinates:

syms x y z p tellipsoid=[2*sin(p)*cos(t),3*sin(p)*sin(t),cos(p)] ellipsoid = [ 2*cos(t)*sin(p), 3*sin(p)*sin(t), cos(p)]

To check that this really is a parametrization, we verify the original equation:

simplify(subs((x^2/4)+(y^2/9)+z^2,[x,y,z],ellipsoid)) ans = 1

And we can also draw a picture with ezsurf:

ezsurf(ellipsoid(1),ellipsoid(2),ellipsoid(3),[0,pi,0,2*pi])

Page 32: Evaluating Triple Integrals

This is as it should be. Now we compute the surface area factor:

realdot = @(u,v) u*transpose(v);veclength = @(u) sqrt(realdot(u,u));surffactor = simple(veclength(cross(diff(ellipsoid,t), ... diff(ellipsoid,p)))) surffactor = (36*sin(p)^2 - 27*sin(p)^4 - 5*sin(p)^4*sin(t)^2)^(1/2)

This is going to be too complicated to integrate symbolically, so we do a numerical integration, using the numerical integration toolbox at http://www.math.umd.edu/users/jmr/241/mfiles/nit/.

addpath nitarea=newnumint2(surffactor,p,0,pi,t,0,2*pi)area =

48.8822

To integrate a function, such as

over the surface, we must express it in terms of the parameters and insert the result as a factor in the integrand.

Page 33: Evaluating Triple Integrals

func=subs(x^2+2*z^2,[x,y,z],ellipsoid)integral=newnumint2(surffactor*func,p,0,pi,t,0,2*pi) func = 2*cos(p)^2 + 4*cos(t)^2*sin(p)^2

integral =

100.5002

Example 2

Here's another example: suppose we want the surface area of the portion of the cone z^2 = x^2 + y^2 between z = 0 and z = 4. The equation of the cone in cylindrical coordinates is just z = r, so we can take as our parameters r and t (representing theta).

syms r; cone=[r*cos(t),r*sin(t),r] cone = [ r*cos(t), r*sin(t), r]

The picture is:

ezsurf(cone(1),cone(2),cone(3),[0,4,0,2*pi])conesurffactor=simple(veclength(cross(diff(cone,r),diff(cone,t)) )) conesurffactor = (2*r^2)^(1/2)

Page 34: Evaluating Triple Integrals

Since r is positive, we can ignore the sign csgn(r), and the surface area is

conesurf=symint2(2^(1/2)*r,r,0,4,t,0,2*pi) conesurf = 16*pi*2^(1/2)

This is right since we can also "unwrap" the cone to a sector of a circular disk, with radius 4*sqrt(2) and outer circumference 8*pi (compared to 8*pi*sqrt(2) for the whole circle), so the surface area is

Problem 1

(a) Compute the surface area of the portion of the paraboloid z = 9 - x^2 - y^2 that lies above the xy-plane.

(b) Evaluate

where Sigma is the surface whose area you found in part (a).

Page 35: Evaluating Triple Integrals

Flux Integrals

The formula

also allows us to compute flux integrals over parametrized surfaces.

Example 3

Let us compute

where the integral is taken over the ellipsoid E of Example 1, F is the vector field defined by the following input line, and n is the outward normal to the ellipsoid.

F = [x,y,z]ndS = cross(diff(ellipsoid,p),diff(ellipsoid,t)) F = [ x, y, z] ndS = [ 3*cos(t)*sin(p)^2, 2*sin(p)^2*sin(t), 6*cos(p)*sin(p)*cos(t)^2 + 6*cos(p)*sin(p)*sin(t)^2]

We can check that we have the outward normal by setting p=pi/2 and t=0, giving us the point of the ellipsoid that is on the positive x-axis. The outward normal should point in the positive x direction.

subs(ndS,[p,t],[pi/2,0])ans =

3.0000 0 0.0000

Before we can evaluate the integral, we must evaluate F in terms of the parameters.

Fpar = subs(F,[x,y,z],ellipsoid)flux = symint2(realdot(Fpar,ndS),p,0,pi,t,0,2*pi) Fpar = [ 2*cos(t)*sin(p), 3*sin(p)*sin(t), cos(p)] flux =

Page 36: Evaluating Triple Integrals

24*pi

Problem 2

Evaluate

where Sigma is the surface of Problem 1 and G is defined by the input line below.

G=[2*x,y,z-2] G = [ 2*x, y, z - 2]

The Divergence Theorem

Example 4

The Divergence Theorem predicts that we can also evaluate the integral in Example 3 by integrating the divergence of the vector field F over the solid region bounded by the ellipsoid. But one caution: the Divergence Theorem only applies to closed surfaces. That's OK here since the ellipsoid is such a surface. To facilitate the calculation we have written a short M-file http://www.math.umd.edu/users/jmr/241/mfiles/div.m. Although the computation of the divergence is trivial in the present case, we illustrate the syntax:

divF = div(F,[x,y,z]) divF = 3

We must now parametrize the solid ellipsoid. We can do this most easily by adding a radial factor r that takes values between 0 and 1. Note that since ellipsoid is a vector, all we have to do is "scalar multiply" it by r:

syms rsolid = r*ellipsoid solid = [ 2*r*cos(t)*sin(p), 3*r*sin(p)*sin(t), r*cos(p)]

Recall that to integrate in the coordinates r, p, and t, we are going to have to insert a scale factor, the absolute value of the determinant of the Jacobian matrix of the change of variables. As we indicated is last week's lesson, it's usually easiest to leave off the absolute value at first, and then change the sign if the scale factor comes out negative.

Page 37: Evaluating Triple Integrals

scale=simple(det(jacobian(solid,[r,p,t]))) scale = 6*r^2*sin(p)

That's clearly positive, and it's simple enough to make a symbolic integration feasible.

answer = int(symint2(3*scale,r,0,1,p,0,pi),t,0,2*pi) answer = 24*pi

This agrees with our calculation of the flux. The numerical value is:

double(24*pi)ans =

75.3982

Problem 3

With G and Sigma as in Problems 1 and 2, integrate the divergence of G over the solid region bounded above by Sigma and below by the xy-plane. Why is your answer not the same as the answer to Problem 2?

Additional Problems

1. Check the accuracy of the computation in Example 1 above by repeating the integration over the ellipsoid, using x and y as the parameters and solving for z as a function of x and y. (Hint: since there are two solutions for z, but the surface is symmetric, just integrate over the top half of the ellipsoid and then double the result.)

2. Let

Compute the flux of the gradient of f through the ellipsoid

both directly and by using the Divergence Theorem.

3. Let T be the torus with equation z^2 + (r-2)^2 = 1 in cylindrical coordinates. Parametrize the torus and use the answer to compute the surface area.

4. Let T be the same torus as in Additional Problem 3 just above. Compute the volume enclosed by the torus two ways: by triple integration, and by computing the flux of the vector field F = (x, y, z) through T and by using the Divergence Theorem. Check

Page 38: Evaluating Triple Integrals

that the results agree, and also that they agree with the prediction of Pappus's Theorem (see Ellis and Gulick): the volume of a solid obtained by rotating a region R in the xz-plane around the z-axis is the area of R times the circumference of the circle traced out by the center of mass of R as it rotates around the z-axis.

Published with MATLAB® 7.8

HelixHere is a way to plot a helix on a cylinder. It will use a parametric description of the cylinder. The cylinder is a right circular cylinder of radius 1 centered on the z-axis. It is the set of all points of the form

I'll limit the graph to

I begin by making a 50x50 mesh of the points [U,V] I'll use in the graph.

u=linspace(0,2*pi,50); v=linspace(0,6*pi,50);[U,V]=meshgrid(u,v);

To be able to get a thick curve for the helix, I use the plot3 command. I plot the helix as a very thick curve and use the command surf to plot the cylinder. The command colormap white prevents shading of the cylinder.

t=linspace(0,6*pi,300);plot3(cos(t),sin(t),t,'LineWidth',3), hold ontitle('Helix on a cylinder')surf(cos(U),sin(U),V), colormap white, hold off

Page 39: Evaluating Triple Integrals

Published with MATLAB® 7.9