Basic Graphs in 3D

by
K. D.  Stroyan
University of Iowa

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

Special Mathematica Functions

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

Notebook Overview

This NoteBook graphs a variety of basic functions of two variables.  You can run this first or look at the NoteBook on graphs of linear functions first.

There are two basic ways to graph a function of two variables,  z = f[x,y].  One is the explicit plot,  {(x,y,z) : z = f[x,y] } and the other is a contour plot.  The contour plot is like a topographical map of a piece of land; it shows curves in the x-y-plane that would lie below a path of constant altitude on the surface.  These plots are explained more in your text and the examples below.

One basic way to plot z = f[x,y] is to "slice" along x and y, drawing the curves  z = f[x, a] for constants  a  = -1, 0, 1, 2, ... and then  z = f[a,y], for  a = -1, 0, 1, 2, ...  This is laborious by hand, but the computer doesn't mind at all.

The contour plot is also a kind of slicing.  In this case, we plot the curves,  a = f[x,y]  for constants  a = -1, 0, 1, 2, ...  Since  f[x,y]  does not give  y  as a function of  x, these are implicit curves.

Both kinds of slicing are explained in more detail in your text and in the NoteBooks SurfaceSlices.ma and ContourSlices.ma

References to the Text

This NoteBook goes with Chapter 17 of the text.

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 make a plot of the surface  z = f[x,y]  for  -1 < x < 2  and  -3 < y < 4. 
(<Enter> the next computation now.)

Plot3D[f[x, y], {x, -1, 2}, {y, -3, 4}];

Third, we make a contour plot of the same function over the same x-y-range.
(<Enter> the next computation now.)

ContourPlot[f[x, y], {x, -1, 2}, {y, -3, 4}];

Notice that the contour plot is shaded.  The dark portions are low and the light portions are high.  Think of snow capped peaks covered with snow.  We can display this shading on the surface by setting the lighting option as follows.  (<Enter> this conputation now.)

Plot3D[f[x, y], {x, -1, 2}, {y, -3, 4}, 
      Lighting -> False];

Practice Exercise

Modify the commands above to plot the function

z = f[x,y] = 9 - (x^2) - (y^2)

for  -3<x<3, -3<y<3

More Examples

Linear Functions of Two Variables

A linear function in two variable has the form  f[x,y] = m x + n y + c.  Define the general linear function by <Enter>ing the next computation.

Clear[f, x, y, m, n, c]; f[x_, y_] := m x + n y + c; f[x, y]

Now set  m, n and c equal to specific values and plot for  a - d <x< a + d  and  b - d <y< b + d.  <Enter> the next computation now.

m = -(3/2); n = (4/5); c = 1; f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}, 
      PlotPoints -> 2]; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}, 
      PlotPoints -> 8, Lighting -> False]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

The coutour plot at the end of the previous computation is the set of curves in the x-y-plane that lie directly below curves on the surface  z = m x + n y + c  at a constant z-value.  For example, when  z = 1,  this is the curve

1 = 1 - 3 x/2 + 4 y/5  or  - 3 x/2 + 4 y/5 = 0

a line normal to the vector  (-3/2 , 4/5) through (0,0).

When z = 2, this is the line,

2 = 1 - 3 x/2 + 4 y/5  or  - 3 x/2 + 4 y/5 = 1,

the line normal to the vector  (-3/2 , 4/5) through (0,5/4).

When z = 3, this is the line

- 3 x/2 + 4 y/5 = 2,

the line normal to the vector  (-3/2 , 4/5) through (0,5/2).

Notice the important point:

All the contours of the linear function

z = m x + n y + c

are lines perpendicular to the vector  (m,n).

Exercises on Linear Functions in Several Variables

This is explained in more detail in the NoteBooks ContourSlices.ma and ImplExplPlane.ma  See the Plane NoteBook for more exercises on plotting linear functions.

Quadratic Functions

A quadratic function of two variables is of the form:

z = a (x^2) + b x y + c (y^2) + d x + e y + f

If we wish to study these functions in detail, we can show how various geometric - algebraic tricks reduce the general formula to special cases,

z = (x^2) + (y^2)

z = (x^2) - (y^2)

z = (x^2) + y, etc.

Rather than studying the general case, we shall simply look at several of the important special cases and some of the geometric-algebraic transformations.

A Circular Paraboloid

To plot  z = (x^2) + (y^2) by hand, we could fix a value of  y, say  y = -1, and plot z = (x^2) + (((-1))^1) or  z = (x^2) + 1.  Of course this is a parabola in the plane  y = -1, a plane parallel to the x-z-plane.

Next, we might fix  y = 0 and plot  z = (x^2) + (0^2)  or  z = (x^2), another parabola, but in the x-z-plane itself.

Next, we might fix y = 1 and plot  z = (x^2) + (1^2)  or  z = (x^2) + 1, another parabola, but in the plane  y = 1.

Next we might fix  y = 2 and plot  z = (x^2) + (2^2)  or z = (x^2) + 4, another parabola, but in the plane  y = 2.

These are all slices of a surface made parallel to the x-z plane.

Now we could fix  x = -1, and plot  z = (((-1))^2) + (y^2), or  z = (y^2) + 1, another parabola in the plane  x = -1.  Similarly, x = 0, x = 1, x= 2, all produce parabolas in the various planes parallel to the y-z-plane.

Piecing all these parabolas together, we would get the plot produced by Plot3D.  <Enter > the next computation now and notice the various parabolic slices.

f[x_, y_] := 2 - ((x^2) + (y^2)); f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}, 
      Lighting -> False]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

The coutour plot at the end of the previous computation is the set of curves in the x-y-plane that lie directly below curves on the surface  z = (x^2) + (y^2)  at a constant z-value.  For example, when  z = 1,  this is the curve

1 = (x^2) + (y^2), a circle of unit radius,

when z = 2, this is the curve,

2 = (x^2) + (y^2), a circle of radius square root of  2,

when z = 3,

3 = (x^2) + (y^2) is the circle of radius square root of  3,

when z = 4,

(2^2) = (x^2) + (y^2)  is the circle of radius 2.

This is explained in more detail in the NoteBook ContourSlices.ma

Squashing the Circular Paraboloid, An Elliptical Paraboloid

The algebraic act of replacing  x  in a known formula by  x/a  and  y  by  y/b has the effect of stretching the graph by a factor  a  in the x direction and  b  in the y direction.  The next graph replaces  x  by  x/(3/2)  and  y  by  y/(2/3).  Notice the change in shape compared to the previous example.  Notice especially how the level sets in the contour plot have been stretched or squashed.

f[x_, y_] := (((((2 x)/3)))^2) + (((((3 y)/2)))^2); f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Exercises

Plot the following both explicitly and as a contour plot.

(1)  z = x + y

(2)    z =((4 (x^2))/9) + ((36 (y^2))/25)

(3)  z =(x^2) - (y^2)

(4) z = ((4 (x^2))/9) - ((36 (y^2))/25)

(5) z = x + (y^2)

(6)  z = x * y

Functions with Circular Symmetry

It is usually easy to sketch a surface that is obtained by rotating a curve about one of the axes.  The question is: When can we recognize a function as just the rotation of a curve?

In the x-y-plane, if we think of a "radius" length  r  from the origin to a point (x,y), then the value is  the square root of  (x^2) + (y^2).  If a function of x and y can be written as a function of the quantity  (x^2) + (y^2), then it "really" only depends on the square radial distance, not the particular x and y at this distance from the origin.

z = 4 Cos[r]

Sketch the graph  z = 4 Cos[r]  for 0 < r < 2 Pi and sketch the surface you obtain by rotating this curve about the z-axis. Then <enter> the next computation.

Plot[4 Cos[r], {r, 0, 2 π}]; f[x_, y_] := 4 Cos[(Sqrt[(x^2) + (y^2)])]; f[x, y]a = 0; b = 0; d = 2 π; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

An Elaborate Surface of Revolution

<Enter> this one:

Plot[
      ((4 Sin[(r^2)] Exp[-((r^2)/7)])/(r^2)), {r, 0, (Sqrt[2 π])}]; 
    f[x_, y_] := 
      ((4 Sin[(x^2) + (y^2)] 
          Exp[-(1/7) ((x^2) + (y^2))])/((x^2) + (y^2))); f[x, y]a = 0; b = 0; d = (Sqrt[2 π]); Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Exercises

Sketch each function  z = f[r] by hand and rotate it about the z-axis, then plot z = f[(Sqrt[(x^2) + (y^2)])] with Mathematica 

(1)  f[r] = r,  f[x,y] = (Sqrt[(x^2) + (y^2)])

(2)  f[r] = (r^2), f[x,y] =((((Sqrt[(x^2) + (y^2)])))^2)

(3)  f[r] = (r^3), f[x,y] = ((((Sqrt[(x^2) + (y^2)])))^3)

(4) f[r] = 2 Log[r], f[x,y] = Log[((((Sqrt[(x^2) + (y^2)])))^2)]

(5) f[r] = (e^((-r^2))), f[x,y] = Exp[-((((Sqrt[(x^2) + (y^2)])))^2)]

Functions of Two Variables that Really Only Depend on One

z = (x^2), y = anything:

<Enter> this and explain:

f[x_, y_] := (x^2); f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

z = Sin[x], y = anything:

<Enter> this and explain:

f[x_, y_] := Sin[x]; f[x, y]a = 0; b = 0; d = π; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

z = Sin[y], x = anything:

<Enter> this and explain:

f[x_, y_] := Sin[y]; f[x, y]a = 0; b = 0; d = π; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Log in x, anything in y:

<Enter> this and explain:

f[x_, y_] := Log[x]; f[x, y]a = 2.1; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

The Normal Curve in x, anything in y:

<Enter> this and explain:

f[x_, y_] := Exp[-(x^2)]; f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

A more subtle one-dimensionality:

<Enter> the next example and notice that the function really only depends on the distance out along the line  x = y.  A change of variables could reduce this function to one that only depends on one variable.  For now, just look at the graph and notice that this is certainly possible.

f[x_, y_] := Sin[x + y]; f[x, y]a = 0; b = 0; d = 2 π; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Exercises

Try these:

(1)  z =(y^2)

(2)  z = (((x + y))^2)

(3)  z = (e^(-(x^4)))

One Thing in One Direction, Another in the Other

<Enter> these examples and after Mathematica has drawn the graphs, notice that you could have graphed the separate graphs  z = f[x]  and  z = g[y]  and added them.

A parabola and a sine:

f[x_, y_] := ((y^2)/π) + Sin[x]; f[x, y]a = 0; b = 0; d = 2 π; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

An abnormal and a normal curve:

f[x_, y_] := Exp[-(x^4)] + Exp[-(y^2)]; f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Exercises

Sketch z = Cos[y] and z = Log[x] by hand and then Plot3D  z = Cos[y] + Log[x]

Functions with Lots of Ups and Downs, Peaks and Valleys

The following examples are honest two dimensional graphs.  They would be hard to draw by hand, but are easy with Mathematica. <Enter> them.

Parabolic and Cubic Slices

f[x_, y_] := (((x^2) (y^3))/8); f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

A Plain Saddle

f[x_, y_] := (x^2) - (y^2); f[x, y]a = 0; b = 0; d = 1; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

An Egg Carton

f[x_, y_] := 2 Sin[x] Cos[y]; f[x, y]a = 0; b = 0; d = 9; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Diagonal Twin Peaks and Valleys

f[x_, y_] := 4 Sin[x y] Exp[-((x^2) + (y^2))]; f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Front-to-back Twin Peaks and Valleys

f[x_, y_] := 4 (x^2) y Exp[-((x^4) + (y^2))]; f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Exercises

Try these: and give them names:

(1)  z =((Sin[x] Sin[y])/(x y))

(2)  z = Sin[x] + Cos[y]

(3)  z = ((x^2) + 3 (y^2)) Exp[-((x^2) + (y^2))]= ((x^2) + 3 (y^2)) (e^(-((x^2) + (y^2))))

(4)  z =x y Exp[-((x^2) + (y^2))]= x y (e^(-((x^2) + (y^2))))

(5)  z = ((4 x y)/((x^2) + (y^2) + 1))

(6)  z = (x^3) - 3 x (y^2)   (A Monkey Saddle)

(7)  z = (x^4) - 6 (x^2) (y^2) + (y^4)  (A Dog Saddle)

Weird Limits of Functions at Bad Points

We know that the functions  f[x,y] = x+y  and  f[x,y] = x * y   are continuous in two variables.  We also know that  f[x,y] = x/y  is continuous except at  y = 0.  Near the y-axis, the graph is torn infinitly...

If we build a function  f[x,y]  from one variable functions that are continuous using composition, and the operations above, we only expect trouble where the original one variable functions are discontinuous or near division by zero.  This section shows both "good" and "bad" examples that look like division by zero.  These can only work our "good" if the numerator is "cancelling" the zero in the denominator, but in two variables, the "cancellations" can be quite tricky.

The Basic Problem Near Division by Zero

f[x_, y_] := (x/y); f[x, y]a = 0; b = 0; d = 1; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Quadratic Zeros Up and Down

The next example is discontinuous at zero.  Notice the "tear" in the surface plot over zero.  Moreover, notice that the different shaded regions of the contour plot all seem to meet at (0,0).  This is in fact the case and shows that you can approach the origin from different directions and get different limiting values.

f[x_, y_] := ((x y)/((x^2) + (y^2))); f[x, y]a = 0; b = 0; d = 1; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Notice how the different shades of the contour meet at (0,0).  Consider approach to (0,0) along a line  y = k x.  Substituting in f[x,y], we obtain

f[x, y] = 
      ((k (x^2))/((x^2) + (k^2) (x^2))) = (k/(1 + (k^2)))

When k = -1, this gives -1/2, when k = 0, this gives 0, when k = 2, it gives  2/5, etc.

Cubic Up, Quartic Down

This surface has a more complicated tear at (0,0).  Plot it and observe that the different levels of the contour plot also converge at (0,0).

f[x_, y_] := (((x^2) y)/((x^4) + (y^2))); f[x, y]a = 0; b = 0; d = 1; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Even though different contour levels of this example converge at (0,0), the limit of this example along lines still exist.  Let  y = m x and substitute:

f[x, m x] = ((m (x^3))/((x^4) + (m^2) (x^2))) = 
        ((m x)/(1 + (x^4))) -> 0 as x -> 0

The limit along every line is zero.

The limit of  f[x,y]  stil does NOT exist.  Let's try a curved approach,  y = k (x^2).

f[x, k x^2] =((k (x^4))/((x^4) + (k^2) (x^4))) = (k/(1 + (k^2))), which depends on k.

No Problem, The Limit is Fine even if this function isn't

The rela function  g[ u ] = Sin[ u ]/u is actually perfectly smooth when the quantity  u is small.  The formula doesn't work, but we know the series expansion for sine and can factro a  u  out and still have a convergent series.

f[x_, y_] := Sin[x y]/(x y); f[x, y]a = 0; b = 0; d = 2; Plot3D[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}]; ContourPlot[f[x, y], {x, a - d, a + d}, {y, b - d, b + d}];

Exercises

Graph the following functions, say where they have the potential to be discontinuous because of difficulties with their definition , and examine the graph to say whether or not they can be exended continuously to the bad points:

(1)  z =(((x^2) y)/((x^2) + (y^2)))

(2)  z =((x^2)/((x^2) + (y^2)))

(3)  z =(((x^2) y + x (y^2))/((x^2) + (y^2)))

(4)  z =(((x^2) y + (x^2))/((x^2) + (y^2)))

(5)  z =((Sin[x] Sin[y])/(x y))

(6) z = ((1 - Cos[x y])/((x^2) + (y^2)))

(7) z = ((1 - Cos[x y])/((x^3) + (y^3)))

Exercise

None, just do the Practice Exercises in the NoteBook.