Lab 1B - Graphing Functions of Two Variables and Quadric Surfaces
Math 2374 - University of Minnesota
http://www.math.umn.edu/math2374
Questions to: rogness@math.umn.edu

Introduction

Remember to evaluate the initialization cells in math2374.nb before continuing on with this lab.

At the end of Lab 1A you learned how to use Mathematica to plot the graph of equations such as y = (((x - 1))^2) or (x^2) + (y^2) = 1.  As you hopefully remember, we have to use different commands to plot the graphs of these two equations.  In the first case, we have explicitly solved for y as a function of x; there is a single y on the left hand side of the equation, and no occurrences of y on the right hand side.  In cases like this we can use the Plot function to show a graph of y.  In the second case we have an implicit function of y.  We can't solve explicitly for y because we end up with ±(Sqrt[1 - (x^2)])on the right hand side.  (This is not a well defined function because for a given value of x we can only have one value, not a positive and a negative value.)  We learned how to use the command ImplicitPlot to handle equations like this.

In this lab we're going to work with the three-dimensional analogs of these commands and, as you might expect, we'll have to consider two different cases.  The first is when we have a function of x and y which is explicitly solved for z, e.g.

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

We'll also consider implicit functions of z, such as

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

(Note that if you tried to solve this last equation for z, you'd have the same problem with a ± sign.)

The first case is considerably easier, and we'll deal with that one first.  One other comment before we move on: some of the equations in this lab might look a bit small on your screen.  If you're having trouble reading them, try the menu option Format : Screen Style Environment : Presentation.

Plot3D

Suppose we define a function z=f(x,y) of two variables, e.g.

f[x_, y_] = x^2  +  y^2

What does a graph of this function even mean?  The definition usually given is "the set of all points of the form (x, y, f(x,y)) such that (x,y) is in the domain of f," but you may not find this particularly enlightening.

Notice that the function f takes two inputs, x and y, and returns a single number, which we call z.  If we draw the x-y-z coordinate axes in the standard way, the z-axis represents height, and this is the key to graphing f(x,y).  If you choose a point (x,y) in the xy-plane, then z=f(x,y) represents the height of the graph at that point.  For example, here's the graph of a simple function, g(x,y)=1.  This means that no matter what values you choose for x and y, the function g will always return ("a height of") one.

g[x_, y_] = 1
Plot3D[g[x, y], {x, -1, 1}, {y, -1, 1}]

As you can see, we're using the command Plot3D to create this graph.  The syntax of Plot3D is very similar to that of Plot; you first give it a function of x and y, and then ranges for x and y.

Here's a slightly more complicated function.  Before you evaluate this cell, see if you can predict what the graph will look like.

g[x_, y_] = x + y
Plot3D[g[x, y], {x, -1, 1}, {y, -1, 1}]

This graph does not have a constant height of one, but if you look at the definition of g(x,y) you should be able to make some observations:

▪ if y = -x, then g(x,y)=0.
▪ if x and y are both positive, then g(x, y) ≻ 0.
▪ if x and y are both negative, then g(x, y) ≺ 0.

Looking back at the picture, are these things true?  One thing you have to notice is that the z-axis goes from -2 to 2, which means the "height" is zero halfway up the box, not at the bottom.  Another thing worth pointing out is that the z-axis is scaled differently than the x- and y-axes.  If you want to change the scaling, you can use the option BoxRatios, which you can look up in the Help Browser:

Plot3D[g[x, y], {x, -1, 1}, {y, -1, 1}, 
      BoxRatios -> {1, 1, 2}]

Let's plot the function f(x, y) = (x^2) + (y^2) we considered previously so that we have something slightly more interesting to work with.

Plot3D[x^2 + y^2,  {x, -2, 2}, {y, -2, 2}]

Another useful option is ViewPoint.  The effect of this option is to pretend you're floating in space at some point (x,y,z) -- which you specify -- looking back at the origin.  For instance, if we want to look at the previous graph from straight above, we can set the viewpoint to be (0,0,10):

Plot3D[x^2 + y^2,  {x, -2, 2}, {y, -2, 2}, 
      ViewPoint -> {0, 0, 10}]

The output isn't terribly interesting.  (Why?)  You should try graphing f(x,y) on your own a few times, choosing different viewpoints each time.  You should also try using the BoxRatios option and see if it makes a difference.

We could spend an entire lab having you plot the graphs of all sorts of  functions.  Some of the most interesting involve trigonometric functions like Sin:

Plot3D[Sin[x*y], {x, -Pi, Pi}, {y, -Pi, Pi}]

This graph moves up and down so much that Mathematica's picture is kind of choppy.  We can fix this by forcing Mathematica to compute the height of the graph (i..e. the z-value) at more points before it starts to fill in the gridlines.  This is done with the PlotPoints option:

Plot3D[Sin[x*y], {x, -Pi, Pi}, {y, -Pi, Pi},  
      PlotPoints -> 40]

If you don't like the gridlines, you can turn them off with the Mesh option:

Plot3D[Sin[x*y], {x, -Pi, Pi}, {y, -Pi, Pi}, 
      PlotPoints -> 40, Mesh -> False]

Sometimes when you're changing viewpoints it's easy to lose track of which axis is the x-axis, and which is the y-axis.  You can label them with the AxesLabel option:

Plot3D[Sin[x*y], {x, -Pi, Pi}, {y, -Pi, Pi}, 
      PlotPoints -> 40, Mesh -> False,  
      AxesLabel -> {"x", "y", "z"}]

Or, if you don't want to have the axes numbered, you can use the Axes option.  (If you want the box to disappear entirely, try adding Boxed->False to this command.)

Plot3D[Sin[x*y], {x, -Pi, Pi}, {y, -Pi, Pi}, 
      PlotPoints -> 40, Mesh -> False, 
      Axes -> False]

Exercise 1

Download the "Addendum to Exercise 1" for this lab from the course website.  Use Mathematica to graph the functions in that document with the pictures below.  You will make your life easier if you match the x- and y- ranges in the Plot3D commands to the ranges in the figures.  The PlotPoints and BoxRatios options might also be useful.  Also note that the viewpoints in the figures might be different than the default viewpoint in Mathematica.  

If you name the functions before plotting them, remember to use lower-case names as discussed last week or you will run into problems.

While we'd like to you to put some effort into identifying each graph, you will only hand in a careful write up one particular match.  For example, if you're told to write about B(x,y), you should carefully explain why B(x,y) produces the graph that it does.  Use the definition of the function to explain the shape -- why is it high in some areas, low in others?  Is it ever equal to zero?  Is it ever negative, or is it always positive? etc.

Your TA will tell you which function has been chosen for you to describe.

ContourPlot

Before we work with implicit functions, we're going to introduce one more way to examine the height of a surface.  If you've ever done any kind of hiking outdoors or fishing on a big lake you're probably familiar with topographic maps.  These maps use so-called "contour lines" to represent elevation.  For instance, on standard United States Geological Survey maps, each contour line represents 10 feet of elevation.  If you'd like to see an example of a topographic map, copy the following link and paste it into a web browser to see to a topographic map of Eagle Mountain, the highest mountain (well... hill)  in Minnesota.

http://www.dnr.state.mn.us/maps/tomo.html?mode=recenter&size=3&layer=24k&col=513&row=243

The contour lines on this map represent elevation above sea level.  Notice how in some places the lines are very close together, which represents a steep slope.  In other places the lines are further apart, which represents a more gradual slope.  Not surprisingly, the steepest slopes seem to be very near the summits of Eagle Mountain and Moose Mountain.

Mathematica can draw a topographic map of a surface for us.  For example, let's look at a map of this function:

f[x_, y_] = x^2  +  y^2;

To draw the topographic map, or "contour diagram," of the function, we use the command ContourPlot. We have to give the command the function we want to plot, and ranges for x and y:

ContourPlot[x^2 + y^2, {x, -2, 2}, {y, -2, 2}]

Notice that Mathematica shades the picture according to elevation.  The darker regions represent the lower points; the lighter shades represent higher points.  Scroll back and look at the graph of f(x,y) generated by Plot3D and see if this contour diagram makes sense to you.  Notice in particular that the contour lines get closer to each other near the edges of the diagram.  Why is this and what does this mean?  (If you're not sure, talk to the students next to you and/or your TA before you go on.)

ContourPlot3D

The three-dimensional equivalent of ImplicitPlot, which we used to graph equations such as (x^2) + (y^2) = 1, is not called ImplicitPlot3D as you might expect.  It's actually called ContourPlot3D, and it's technically the three-dimensional analog of ContourPlot.  (As it turns out, ImplicitPlot is really a special case of ContourPlot, but you don't need to understand why.  The reason has something to do with level curves, which are defined in chapter 3.  The reason ContourPlot3D is the generalization of ImplicitPlot has something to do with level surfaces, which are also defined in chapter 3.)

Suppose we want to graph an implicit function of z like this:

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

(Hopefully you recognize that this is the equation for the unit sphere.)  To graph this function using ContourPlot3D, you'll have to accept on faith alone that you should move everything to the left hand side of the equation, so you have a zero on the right hand side:

(x^2) + (y^2) + (z^2) - 1 = 0

The reasons for doing this aren't really important now, and we can't really explain it until we've covered level surfaces later in the course, so you'll just have to trust us for now.  

Next we pass the expression on the left hand side of this equation to ContourPlot3D, along with ranges for x, y, and z!

ContourPlot3D[
      x^2 + y^2 + z^2 - 1, {x, -1, 1}, {y, -1, 1}, {z, -1, 
        1}]

Notice that we chose the ranges for x, y, and z to go from -1 to 1.  These ranges have a great influence over the resulting picture.  If you make them too large, the command will run much faster but the picture will look awful.  If you make them too small, you won't see your sphere (because you'll actually be inside it).  Try changing all of the ranges above to {_,-3,3} and {_,-1/2,1/2} to see examples of this.

The point is this: when you use ContourPlot3D you should put careful thought into your ranges.

One other note: you can plot any kind of equation with x, y, and z using ContourPlot3D.  It doesn't have to be an implicit function of z like the equation of the sphere.  For example, to graph this equation from above,

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

we first move everything to the left hand side:

z - (x^2) + (y^2) = 0.

Then we use the command:

ContourPlot3D[
      z - x^2 - y^2, {x, -1, 1}, {y, -1, 1}, {z, 0, 2}]

However, if you can use Plot3D, it generally makes better pictures.  Compare the picture from the previous command to this one:

Plot3D[x^2 + y^2, {x, -1, 1}, {y, -1, 1}, 
      BoxRatios -> {1, 1, 2}]

Interactive 3D Graphics

The commands in this section are defined in math2374.nb, and they depend on a certain Java program being located in the correct place.  If you're trying to use Mathematica at home, this command may not work.  You can look at the course web page to find tips about how to make it function correctly.

The three-dimensional graphs that Mathematica can produce are great, but it can be very tedious to adjust the ViewPoints if you want to get a different look at your function.  In today's interactive world, we could argue, we ought to be able to rotate the pictures, zoom in and out, and so on.  While Mathematica has never put much effort into these capabilities, we've incorporated a Mathematica add-on called "LiveGraphics3D" which will let you rotate things to your hearts content.

It works like this.  Suppose you want to look at a certain picture, like this one from earlier:

graph = 
      Plot3D[Sin[x*y], {x, -Pi, Pi}, {y, -Pi, Pi}, 
        PlotPoints -> 40,  
        AxesLabel -> {"x", "y", "z"}]

Notice that we used a name -- "graph" -- for the picture.  Now, instead of using the Show command, we'll use a new command called ShowLive:

ShowLive[graph]

If all goes well, evaluating the previous cell will make an extra window pop up on your monitor.  Now you can interact with the picture in this new window.  Click on it and move your mouse to rotate it.  If you hold the shift key down, you can click on it and move your mouse up and down to zoom in and out.  To reset the image, press the "Home" key.

You can basically use the ShowLive command anytime that you would use the Show command with a 3D picture, although it a few things may behave differently.  If something goes wrong you'll get a small windows with a (possibly incomprehensible) error message.  Just close the window and try something different.

Note that every time you use the ShowLive command, you'll create a new little window.  Make sure you close them when you're done, or you'll end up with a lot of clutter on your virtual desktop.

There's also an interactive version of Plot3D you can use, called Plot3DLive, which means you can use LiveGraphics3D to look at a graph without having to plot it separately before using ShowLive:

Plot3DLive[Cos[x*y], {x, -Pi, Pi}, {y, -Pi, Pi}, 
      PlotPoints -> 40,  
      AxesLabel -> {"x", "y", "z"}]

Note that using the Live commands can really slow down some computers, especially if you use a high PlotPoints setting.

The downside to using Plot3DLive is that the input and output is not sorted next to each other, so you can lose track of what command created what picture.  Also, if you'd like to print out a picture, you'll have to use the regular old Plot3D command.

LiveGraphics3D can do some other really cool things.  For example, instead of just adjusting your viewpoint and magnification of a surface, we've set up some examples where you can interactively change the picture!  You may have already used some of the interactive demos posted online.  Although it's possible to run these from within Mathematica, we'll usually ask you to go to a web page instead.

Quadric Surfaces

The graphs of functions of two variables are examples of what we call surfaces.  More generally, a set of points (x,y,z) that satisfy an equation relating all three variables is often a surface.  A simple example is the unit sphere, which you graphed above with ContourPlot3D.  (The sphere is the set of points which satisfy the equation (x^2) + (y^2) + (z^2) = 1. )

In this section we're going to analyze some equations which involve x,  y,  z,  (x^2),  (y^2),  and (z^2),  and we'll look at their graphs.  The surfaces in question are known as quadric surfaces, and will provide important examples for the rest of the course.  There are six different quadric surfaces: the ellipsoid, the elliptic paraboloid, the hyperbolic paraboloid, the elliptic cone, and hyperboloids of one and two sheets.  

In the first lecture of the semester, you talked about 3D graphs and how to analyze cross-sections of quadric surfaces.  This is where you choose a specific value for x,  y, or z.  For example, consider the quadric surface given by the equation

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

To check the horizontal cross-sections, we'd choose values for z, such as z = 36.  In this case,

36 = 4  (x^2) + 9 (y^2)

or

((x^2)/9) + ((y^2)/4) = 1

So we see that the cross section in the plane z=36 is an ellipse which stretches 3 units in the positive and negative x-direction, and 2 units in the positive and negative y-direction.

If you don't remember how to graph ellipses and hyperbolas, this might be a good time to ask your TA to review those things.

Rather than have you read through a long section where we work out the cross sections of all the quadric surfaces, we're going to send you to an interactive gallery instead.  Go to the following page in your web browser.  

http://www.math.umn.edu/~rogness/quadrics/

The gallery also includes another short review about cross sections, along with a picture.  Once you've spent some time browsing through the gallery, you're ready to continue on with this lab.  You can use the gallery as a reference throughout the semester.

Graphing Quadric Surfaces in Mathematica

You can use the commands you learned earlier in this lab to graph any of the quadric surfaces.  Some of the equations, such as z = (x^2) + (y^2), are explicitly solved for z, so you can graph using Plot3D.  Others, like z = (x^2) + (y^2)or (x^2) + (y^2) + (z^2) = 1, are implicit functions, so you have to use ContourPlot3D.  Below you can see an example of graphing each kind of surface.

Elliptic Paraboloid

This shows the graph of z = (x^2) + (y^2).

Plot3D[x^2 + y^2, {x, -1, 1}, {y, -1, 1}, 
      AxesLabel -> {"x", "y", "z"}]

Hyperbolic Paraboloid (Saddle)

This shows the graph of z = (x^2) - (y^2).

Plot3D[x^2 - y^2, {x, -1, 1}, {y, -1, 1}, 
      AxesLabel -> {"x", "y", "z"}]

Ellipsoid

This displays the graph of ((x^2)/9) + ((y^2)/4) + ((z^2)/1) = 1.

ContourPlot3D[
      x^2 / 9 +  y^2 / 4  +  z^2 / 1  - 1, {x, -3, 
        3}, {y, -2, 2}, {z, -1, 1}, Axes -> True, 
      AxesLabel -> {"x", "y", "z"}]

Double Cone

This shows the graph of (z^2) = (x^2) + (y^2).

ContourPlot3D[
      x^2 + y^2 - z^2, {x, -1, 1}, {y, -1, 1}, {z, -1, 1}, 
      Axes -> True, 
      AxesLabel -> {"x", "y", "z"}]

Alternatively, you can solve for z and graph the upper and lower parts separately before putting them together.

upper = 
      Plot3D[Sqrt[x^2 + y^2], {x, -1, 1}, {y, -1, 
          1}]
lower = 
      Plot3D[-Sqrt[x^2 + y^2], {x, -1, 1}, {y, -1, 
          1}]
Show[upper, lower, BoxRatios -> {1, 1, 2}]

Hyperboloid of One Sheet

This shows the graph of (x^2) + (y^2) - (z^2) = 1.

ContourPlot3D[
      x^2 + y^2 - z^2 - 1, {x, -1.5, 1.5}, {y, -1.5, 
        1.5}, {z, -1, 1}, Axes -> True, 
      AxesLabel -> {"x", "y", "z"}]

Hyperboloid of Two Sheets

This shows the graph of -(x^2) - (y^2) + (z^2) = 1.

ContourPlot3D[-x^2 - y^2 + z^2 - 1, {x, -1, 1}, {y, -1, 
        1}, {z, -3, 3}, Axes -> True, 
      AxesLabel -> {"x", "y", "z"}]

Wrapup and Exercises

A preview of what's to come...

Some people feel that using Plot3D to graph a paraboloid or cone results in an ugly picture, because the surfaces are plotted over a square in the xy-plane.  (For example, the square where -1≤x≤1 and -1≤y≤1.)  To get nicer pictures, like those in the book, we'd like plot these surfaces over a circular domain in the xy-plane.  We don't have the resources to do that yet, but you'll learn how in Lab 2B.  If you want a little preview, you can evaluate the followingcells.  Don't bother trying to understand the commands yet!

paraboloid = 
      ParametricPlot3D[{r*Cos[t],  r*Sin[t], r^2}, {r, 0, 2}, {t, 0, 
          2  Pi}]

paraboloid = 
      ParametricPlot3D[{r*Cos[t],  r*Sin[t], 
          r^2 (Cos[t]^2 - Sin[t]^2)}, {r, 0, 1}, {t, 0, 
          2  Pi}]

uppercone = 
        ParametricPlot3D[{s Cos[t],  s Sin[t], s}, {s, 0, 2}, {t, 0, 
            2  Pi}, DisplayFunction -> 
            Identity];
lowercone = 
        ParametricPlot3D[{s Cos[t],  s Sin[t], -s}, {s, 0, 2}, {t, 0, 
            2  Pi}, DisplayFunction -> 
            Identity];
Show[uppercone, lowercone, 
      DisplayFunction -> $DisplayFunction]

Exercise 2

Find a function whose contour diagram is as similar as possible to the following picture.  You don't need to completely match every detail, but your answer should have contour lines which are not evenly spaced, and they should be ellipses, not circles or squares.  Pay careful attention to the colors; recall that the shades from ContourPlot correspond to the height of the function, and lighter shades are higher.  If your contour lines are correct, but the shading scheme is reversed, then you haven't finished the problem.

Hint:  Use the "Student Axiom," i.e. if this exercise is part of the section on quadric surfaces, then the required functions are probably quadric surfaces.



Exercise 3

Do Exercise 2 for the following new graph.  Again, be careful about the colors, and notice that the contour lines are evenly spaced in this picture.



Exercise 4

Consider the surface given by (x^2) + (y^2) - 2  (z^2) = 4.  Make an accurate picture of this quadric surface by drawing the cross sections given by z=0, z=±2, x=0, x=±2, y=0, y=±2, and any others which might be helpful.  Note that this exercise is easier to do by hand than with Mathematica, although the online gallery of quadric surfaces could be useful.

Exercise 5

Consider the surface given by z = 2  (x^2) - (y^2).  Make an accurate picture of this surface by drawing the cross sections given by x=0, x=±1, y=0, y=±1, z=0, z=±1, and any others which might be helpful.  As with Exercise 4, this is probably easier to do by hand, but the online gallery could help.

Exercise 6

So far all of our quadric surfaces have been centered at the origin, but that's not always the case.  You can move them around using the same methods you've used in Calculus and PreCalculus -- replacing "x" with "(x-h)" and so on.  Find the equation for a double cone which:

(1) has a vertex at (1,1,2),
(2) and intersects the xy-plane (i.e. the plane z=0) in a circle of radius 4.

Credits

This lab was written from scratch in January 2002.  Most of the sections were written with the future in mind; I know what we're going to do with future labs, and therefore know what we'd like students to learn about plotting functions in Mathematica.  The commands used in the Quadric Surfaces section were also written from scratch.  I'm not entirely happy with them, but they do provide some insight with the cross sections.  Please send me any comments or questions!

Update: I rewrote parts of this lab in January 2004.  I edited exercises, removed references to the unsupported and undocumented RealTime3D package, and replaced it with the section on ShowLive.  I also revamped the section on quadric surfaces.  The old version was a regurgitation of the textbook.  The new version is more interactive and will hopefully be more interesting.

(Minor changes made in September 2004; just housekeeping stuff.)

This lab is copyright 2002, 2004 by Jonathan Rogness (rogness@math.umn.edu) and is protected by the Creative Commons Attribution-NonCommercial-ShareAlike License.  You can find more information on this license at http://creativecommons.org/licenses/by-nc-sa/1.0/

Although it's not specifically required by the license, I'd appreciate it if you let me know if you use parts of our labs, just so I can keep track of it.  Please send me any questions or comments!