Contour Slices of a Surface
z = f[x,y]

by
K. D.  Stroyan
University of Iowa

copyright 1997 by Academic Press, Inc. - All rights reserved.

Special Mathematica Functions used in this NoteBook

SetOptions[Plot3D,ViewPoint->{0.479, -3.035, 1.418},
	BoxRatios ->Automatic,PlotPoints->30,
	AxesLabel->{"x","y","z"}];
SetOptions[ContourPlot,PlotPoints->30,
	AxesLabel->{"x","y"}];

plot3DContour[f_,xCnt_,yCnt_,scale_]:= 
Module[{zSs,surface,contour,cont,baseContour,combined},
zSs = Table[N[f[x,y]],{x,xCnt-scale,xCnt+scale,scale/30},
			{y,yCnt-scale,yCnt+scale,scale/30}];
zMIN = Min[zSs];

surface = Plot3D[ f[x,y] , {x,xCnt-scale,xCnt+scale},
						{y,yCnt-scale,yCnt+scale},
			PlotPoints->30,BoxRatios->Automatic,
			Ticks->None,
			ViewPoint->{1.149, -3.086, 0.779},
			DisplayFunction->Identity];

contour = ContourPlot[ f[x,y] ,{x,xCnt-scale,xCnt+scale},
			{y,yCnt-scale,yCnt+scale},PlotPoints->30,
			DisplayFunction -> Identity];

cont = Show[contour,ContourShading->False,
				DisplayFunction -> Identity];

baseContour = First[Graphics[cont]];
					
baseContour = Graphics3D[baseContour /. Line[x_]:>
			Line[Map[Append[#,zMIN]&,x]]];

combined = Show[surface,baseContour,
		DisplayFunction->$DisplayFunction];
Return[combined]
		];

sliceAt[f_,xCnt_,yCnt_,scale_,z_,zMIN_] :=
	Module[{surface,plane,contour,pLz,contLz,w},

	surface = Plot3D[ f[x,y] , {x,xCnt-scale,xCnt+scale},
						{y,yCnt-scale,yCnt+scale},
				PlotPoints->30,BoxRatios->Automatic,
				AxesLabel->{"x","y","z"},
				ViewPoint->{1.149, -3.086, 0.779},
				DisplayFunction->Identity];


	plane[w_] := Plot3D[ w , {x,xCnt-scale,xCnt+scale},
						{y,yCnt-scale,yCnt+scale},
				PlotPoints->2,BoxRatios->Automatic,
				Ticks->None,DisplayFunction->Identity];
						
	contour[w_] := ContourPlot[ f[x,y] ,
		{x,xCnt-scale,xCnt+scale},{y,yCnt-scale,yCnt+scale},
		PlotPoints->30, Contours->{w},DisplayFunction->Identity];

				
	pLz = plane[z];

	contLz = Show[Evaluate[contour[z]],
			ContourShading->False,
			DisplayFunction -> Identity];

	contLz = First[Graphics[contLz]];
					
	contLz = Graphics3D[contLz /. Line[x_]:>
			Line[Map[Append[#,zMIN]&,x]]];

Show[surface,pLz,contLz,
		DisplayFunction->$DisplayFunction];]

FlyBy[
	theGraphic_, 
	thePath_List:{pathVariable, -0.5, 1.5},
	pathRange_List:{pathVariable, -3, 3, 0.5}]:= 
	Do[
		Show[theGraphic, ViewPoint->thePath]
	,pathRange]

Notebook Overview

This NoteBook shows the intersection of a horizontal plane with the explicit surface  z = f[x,y] and draws the curve of intersection in a plane beneath the surfaces.  This intersection is one "contour" or "level curve" of the function.  The collection of all the level curves is called the contour map or contour plot of the function.

References to the Text

This NoteBook goes with Chapter 17 of the text, especially Section 17.5.

Step-by-step

We begin by defining a function:
Move the cursor into the next cell, click once and then press the <Enter> key on the number pad or hold down the <Shift> key and press <Return>.  This <Enter>s the definition into Mathematica's kernel.

Clear[f, x, y]; f[x_, y_] := 9 x y Exp[-((x^2) + (y^2))]; f[x, y]

Next, we give a center point for our graph and a scale and draw the explicit surface  z = f[x,y].
(<Enter> the next computation now.)

xCnt = 0; yCnt = 0; scale = 2; surface = 
      Plot3D[f[x, y], {x, xCnt - scale, xCnt + scale}, {y, yCnt - scale, 
          yCnt + scale}];

Now let's view the slice with a horizontal plane  z = 1 and draw the contour curve at  z = -2

z = 1; sliceAt[f, xCnt, yCnt, scale, z, -2]

Practice Exercises

Define a new function

Copy, paste and edit the definition of a function  f[x,y] fom above to define the linear function  z = f[x,y] = -2 x/3 + 5 y/4
	
<Enter> your definition.

\(Clear[f, x, y]; 
\(?? ?\)\)

Make an explicit plot

Copy, past and edit the Plot3D command above to plot your function for  1 - 2 < x < 1 + 2, and 3 - 2 < y < 3 + 2, that is, make the (x,y)-center of your plot  (1,3)  and the scale + or - 2.

\(xCnt\  = \ \(?? 
yCnt\ =\ ??
scale\  = \ \(?? 
???\)\)\)

Slice your surface

Slice your linear plot at the level  z = 2 and draw the contour at level  -1.  (Copy, pase and edit the similar commands above.)

Slicing Animation

The next computtion makes a movie of the slice plane going from the bottom of the surface to the top.  <Enter> it and wait.  It takes a long time to draw all the frames of the movie.

When it is finished, highlight eht cell bar containing all the grapha ans press <Command> + <y> (or choose "Animate selected graphics" from the Graph menu.)

Clear[f, x, y]; f[x_, y_] := 9 x y Exp[-((x^2) + (y^2))]; f[x, y]xCnt = 0; yCnt = 0; scale = 2; zSs = 
      Table[N[f[x, y]], {x, xCnt - scale, xCnt + scale, (scale/30)}, {y, 
          yCnt - scale, yCnt + scale, (scale/30)}]; zMIN = Min[zSs]; zMAX = Max[zSs]; Do[sliceAt[f, xCnt, yCnt, scale, z, zMIN], {z, zMIN, zMAX,  .2}];

All Contours Together

Clear[f, x, y]; f[x_, y_] := 9 x y Exp[-((x^2) + (y^2))]; f[x, y]xCnt = 0; yCnt = 0; scale = 2; combined = plot3DContour[f, xCnt, yCnt, scale];

FlyBy The Combination

The next computation lets you "fly by" the combined figure, so you can view it from different positions.

FlyBy[combined, {3 + 2 u, 5 + u, 1}, {u, -4, 4, 0.5}];

Exercises

Exercise 17.5.2

Sketch several of the contours of the functions
		
a)  f[x,y] = (x^2) + (y^2)

b)  f[x,y] = ((((x/5)))^2) + ((((y/3)))^2)

c)  f[x,y] = x (y^3)

d)  f[x,y] = (x^2) (y^3)

e)  f[x,y] = Sin[x] + Cos[y]

f)  f[x,y] =x Cos[2 π y]

g)  f[x,y] = Exp[-((x^2) + (y^2))]= (e^(-((x^2) + (y^2))))

h)  f[x,y] = Sin[(x^2) + (y^2)] Exp[-((x^2) + (y^2))]

by hand and then make a Contour[.] drawing of the family of contours on an appropriate scale like  10,  -10<x<10, -10<y<10.

a

Clear[f, x, y]; f[x_, y_] := (x^2) + (y^2)f[x, y]xCnt = 0; yCnt = 0; scale = 2; combined = plot3DContour[f, xCnt, yCnt, scale];