Graphing Functions of Two Variables

Goal:  Develop intuition for surfaces in 3-space that are defined by functions  z = f(x, y).  Become familiar with 3D graphs of functions of two variables.  Increase spatial intuition and visualization.

The examples show how to graph a surface z = f(x, y) and how to graph some cross-sections.  They also show you to draw level curves for a surface z = f(x, y).  Read the text and the examples carefully to understand the commands.  To execute the commands, press  the ENTER  key over on the right side of the keyboard with the numeric keyboard..

Please follow the directions given in the exercises.  If you are submitting Mathematica output with this assignment, each printout must show the complete Mathematica input line that produced the output and it has to have the number of the exercise on it.  Number your pages.

Examples

Graphing a surface  z = f(x, y)

Surfaces in space that are defined as a function of two variables, z = f(x, y), can be graphed with Plot3D[ f[x, y], {x, a, b}, {y, c, d}].  This produces a graph of the surface over the rectangle described by  a ≤ x ≤ b and  c ≤ y ≤ d.  Many options are possible to improve the graphs.  3D graphing with Mathematica uses a lot of memory; using the option Shading->False suppresses the color and saves memory and speeds up printing.

EXAMPLE 1:  Without specifying any options, the default viewpoint is the point  (1.3, -2.4, 2); that is, Mathematica views the object from the point (1.3, -2.4, 2).   Enter the following input line to look at the hyperbolic paraboloid  f(x,y) = (x^2) - (y^2) .  Note that the AxesLabel option does not label the axes themselves, but merely sides of the box that are parallel to the axes.

Plot3D[(x^2) - (y^2), {x, -3, 3}, {y, -3, 3}, 
        AxesLabel -> {x, y, z}];

Unfortunately, this is not the orientation that we usually see in textbooks;  if we specify a viewpoint with all the entries positive, then we have the standard orientation.

Plot3D[(x^2) - (y^2), {x, -3, 3}, {y, -3, 3}, 
        ViewPoint -> {3, 1, 1}, 
        AxesLabel -> {x, y, z}];

We can also change the size of the box, the default box ratios are {1, 1, 0.4}, BoxRatios->Automatic gives identical scales on each side.  Enter the next lines and make sure you understand the meaning of the changes in the commands

Plot3D[(x^2) - (y^2), {x, -3, 3}, {y, -3, 3}, 
        BoxRatios -> {1, 1, 1}, ViewPoint -> {3, 1, 1}, 
        AxesLabel -> {x, y, z}];

Plot3D[(x^2) - (y^2), {x, -3, 3}, {y, -3, 3}, 
        BoxRatios -> {1, 1, 1}, ViewPoint -> {3, 1, 1}, 
        AxesLabel -> {x, y, z}, Shading -> False];

Plot3D[(x^2) - (y^2), {x, -3, 3}, {y, -3, 3}, 
        BoxRatios -> {1, 1, 1}, ViewPoint -> {0, 0, 4}, 
        AxesLabel -> {x, y, z}];

EXAMPLE 2:  Consider the elliptic paraboloid z = (y^2) + 2 (x^2).

Plot3D[(y^2) + 2  (x^2), {x, -5, 5}, {y, -5, 5}];

To improve the graph, we use some of Mathematica's options.

Plot3D[(y^2) + 2  (x^2), {x, -5, 5}, {y, -5, 5}, 
        ViewPoint -> {3, 2, 1}, BoxRatios -> {1, 1, 1}, 
        AxesLabel -> {x, y, z}, Shading -> False];

Here it is easy to identify the cross-sections  x = 0 and  y = 0.  For  x = 0,  we have the parabola  z = y2;  and for  y = 0, the parabola  z = 2 x2.

EXAMPLE 3:  Sometimes Mathematica "truncates" a graph, we can usually fix that by specifying the range for z as in the following examples.

Plot3D[(E^(-(x^2) - (y^2))), {x, -4, 
        4}, {y, -4, 4}, PlotPoints -> 30, 
      ViewPoint -> {3, 1, 1}, AxesLabel -> {x, y, z}];

Plot3D[(E^(-(x^2) - (y^2))), {x, -4, 
        4}, {y, -4, 4}, PlotPoints -> 30, 
      PlotRange -> {0, 1.5}, ViewPoint -> {3, 1, 1}, 
      AxesLabel -> {x, y, z}]

EXAMPLE 4:  For a more complicated surface consider f(x, y) = (x^4) - (y^4) - 2 (x^2) + 2 (y^2) + 1.

Plot3D[
        (x^4) - (y^4) - 2 (x^2) + 2 (y^2) + 1, {x, -2, 2}, {y, -2, 2}, 
        BoxRatios -> {1, 1, 1}, 
        ViewPoint -> {-1.75, -2.9, 1}, PlotPoints -> 30, 
        Shading -> False];

Here, the cross-sections for  x = 0 and  y = 0  are quartic polynomials.  For  x = 0,  we have  
z = - (y^4)+ 2 (y^2) + 1;  and for  y = 0 we have z =  (x^4) - 2 (x^2) + 1.  Plot both of these:

Plot[-(y^4) + 2 (y^2) + 1, {y, -2, 2}];

Plot[(x^4) - 2 (x^2) + 1, {x, -2, 2}];

Look at the graph of the surface and draw these two curves on the surface.

Graphing level curves of a surface z = f(x, y)

By using ContourPlot[ ] we can draw some of the level curves of the surface defined by z = f(x, y).  Darker portions are lower levels and lighter portions are higher levels. 

EXAMPLE 4 (continued):  Applying this command to the function in the last example above we get:

ContourPlot[
        (x^4) - (y^4) - 2 (x^2) + 2 (y^2) + 1, {x, -2, 2}, {y, -2, 
          2}];

Make sure you can relate this map of the surface to the surface itself.  To improve the plot of the level curves you can use various Mathematica  options.  See the effect of using some of the options:

ContourPlot[
        (x^4) - (y^4) - 2 (x^2) + 2 (y^2) + 1, {x, -2, 2}, {y, -2, 2}, 
        Contours -> 20, PlotPoints -> 40];

If you only want certain level curves, you can specify them.  If we only want the level curves corresponding to z = 0,  z = 3 and z = 5:

ContourPlot[
        (x^4) - (y^4) - 2 (x^2) + 2 (y^2) + 1, {x, -2, 2}, {y, -2, 2}, 
        Contours -> {0, 3, 5}, PlotPoints -> 40];

ContourPlot[
        (x^4) - (y^4) - 2 (x^2) + 2 (y^2) + 1, {x, -2, 2}, {y, -2, 2}, 
        Contours -> {0, 3, 5}, PlotPoints -> 40, 
        ContourShading -> False];

To see what the other options are, go to the help menu.

EXAMPLE 5:  We can try to find the local extremes for a three dimensional graph by using the Mathematica command FindMinimum[f[x,y], {x, (Subscript[x, 0])}, {y, (Subscript[y, 0])}]  which searches for a minimum of  f(x, y)  starting from the estimated point ((Subscript[x, 0]),  (Subscript[y, 0])).  If  f(x, y) = (2y - x +  (e^(1 - (x^2)  -  (y^2))))/(1+(x^2)  +  (y^2)) we can try using different viewpoints until we see some of the features of the graph and also looking at a contour plot.

Clear[f]
f[x_, 
        y_] = ((2 y - x + Exp[1 - (x^2) - (y^2)])/(1 + ((x^2) + (y^2))))

Plot3D[f[x, y], {x, -3, 3}, {y, -3, 3}, PlotPoints -> 30, 
      ViewPoint -> {2, -5, 3}, 
      AxesLabel -> {x, y, z}]; 
ContourPlot[f[x, y], {x, -3, 3}, {y, -3, 3}, 
      Contours -> 20, ContourShading -> False];

It appears that the graph has a minimum near the point where x = 1, y = -1.5 and a maximum near the point where x = 0 and y = 0.  If we try to find the minimum first, we'll start with (Subscript[x, 0]) = 1 and (Subscript[y, 0]) = -1.5.

FindMinimum[f[x, y], {x, 1}, {y, -1.5}]

This tells us that the minimum value is -0.946503 and that it occurs when x = 0.700885 and y =-1.40177.  If we want to find the maximum, Mathematica does not have a FindMaximum function but  f  will have a maximum when  -f  has a minimum.

FindMinimum[-f[x, y], {x, 0}, {y, 0}]

This tells us that the maximum value is 2.94127 (remember we took the negative of f) and that it occurs when  x =  -0.0900656 and y = 0.180131.

Later, we will use partial derivatives to find extrema just as we used first derivatives to extrema for functions of one variable.

Exercises

1.  Analyze the function z =  (x^2)- 2(y^2) + 1 for -3 ≤ x ≤ 3 and -3 ≤ y ≤ 3.  Get two printouts of this function from two different viewpoints and label the axes on both printouts.

2.  Get a printout that shows the features of z = (cos x)(cos y) for -10 ≤ x ≤ 10 and -10 ≤ y ≤ 10.  Obtain another printout that shows this curve for -2 ≤ x ≤ 10  and -10 ≤ x ≤ 2.

3.  Obtain a printout that shows the features of  z = (x^2)  +  9  (y^2) for  -1 ≤ x ≤ 1 and -1 ≤ y ≤ 1 along with contour plots for z = -1, 0, and 10.  Explain what the contour plots indicate.

4.  Obtain a printout that shows the features of  z = (x  -  
            4  y) (e^(-((x^2)  +  (y^2))/3)) for  -1 ≤ x ≤ 1 and -1 ≤ y ≤ 1 along with contour plots for z = -1, 0, and 10.  Explain what the contour plots indicate.

5.  Consider the curve  z = ((x^(2 )) +  3  (y^2)) 
        (e^(-(x^2) - (y^2))) for -5 ≤ x ≤ 5  and  -5 ≤ y ≤ 5.  On this region it appears to have two maximums and one minimum.  Obtain a printout.   Use FindMinimum[ ] to approximate the extreme values and to determine where they occur.

6.  Consider the curve  z = (2(x^(2 )) -  5  x  -  3  (y^2)  +  
            3  y) (e^(-(x^2) - (y^2))) for -5 ≤ x ≤ 5  and  -5 ≤ y ≤ 5.   Obtain a printout.  Use FindMinimum[ ] to approximate the extreme values  over this region and to determine where they occur.