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 ±(1 - x^2)^(1/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.

In[41]:=

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

Out[41]=

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.

In[42]:=

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

Out[42]=

1

[Graphics:HTMLFiles/index_10.gif]

Out[43]=

⁃SurfaceGraphics⁃

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.

In[44]:=

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

Out[44]=

x + y

[Graphics:HTMLFiles/index_14.gif]

Out[45]=

⁃SurfaceGraphics⁃

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:

In[46]:=

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

[Graphics:HTMLFiles/index_20.gif]

Out[46]=

⁃SurfaceGraphics⁃

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.

In[47]:=

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

[Graphics:HTMLFiles/index_24.gif]

Out[47]=

⁃SurfaceGraphics⁃

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):

In[48]:=

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

[Graphics:HTMLFiles/index_27.gif]

Out[48]=

⁃SurfaceGraphics⁃

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:

In[49]:=

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

[Graphics:HTMLFiles/index_30.gif]

Out[49]=

⁃SurfaceGraphics⁃

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:

In[50]:=

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

[Graphics:HTMLFiles/index_33.gif]

Out[50]=

⁃SurfaceGraphics⁃

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

In[51]:=

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

[Graphics:HTMLFiles/index_36.gif]

Out[51]=

⁃SurfaceGraphics⁃

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:

In[52]:=

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

[Graphics:HTMLFiles/index_39.gif]

Out[52]=

⁃SurfaceGraphics⁃

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.)

In[53]:=

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

[Graphics:HTMLFiles/index_42.gif]

Out[53]=

⁃SurfaceGraphics⁃

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://maps.dnr.state.mn.us/cgi-bin/drgs.pl?x=672154.0000&y=5281552.0000&size=3&layer=100k

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:

In[54]:=

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:

In[55]:=

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

[Graphics:HTMLFiles/index_46.gif]

Out[55]=

⁃ContourGraphics⁃

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!

In[56]:=

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

[Graphics:HTMLFiles/index_52.gif]

Out[56]=

⁃Graphics3D⁃

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:

In[57]:=

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

[Graphics:HTMLFiles/index_57.gif]

Out[57]=

⁃Graphics3D⁃

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

In[58]:=

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

[Graphics:HTMLFiles/index_60.gif]

Out[58]=

⁃SurfaceGraphics⁃

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:

In[59]:=

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

[Graphics:HTMLFiles/index_63.gif]

Out[59]=

⁃SurfaceGraphics⁃

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:

In[60]:=

ShowLive[graph]

Out[60]=

⁃Graphics3D⁃

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:

In[61]:=

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

Out[61]=

⁃Graphics3D⁃

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 = 4x^2 + 9 y^2

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

36 = 4x^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^2or 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.

In[62]:=

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

[Graphics:HTMLFiles/index_83.gif]

Out[62]=

⁃SurfaceGraphics⁃

Hyperbolic Paraboloid (Saddle)

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

In[63]:=

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

[Graphics:HTMLFiles/index_87.gif]

Out[63]=

⁃SurfaceGraphics⁃

Ellipsoid

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

In[64]:=

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"}]

[Graphics:HTMLFiles/index_91.gif]

Out[64]=

⁃Graphics3D⁃

Double Cone

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

In[65]:=

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

[Graphics:HTMLFiles/index_95.gif]

Out[65]=

⁃Graphics3D⁃

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

In[66]:=

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}]

[Graphics:HTMLFiles/index_98.gif]

Out[66]=

⁃SurfaceGraphics⁃

[Graphics:HTMLFiles/index_100.gif]

Out[67]=

⁃SurfaceGraphics⁃

[Graphics:HTMLFiles/index_102.gif]

Out[68]=

⁃Graphics3D⁃

Hyperboloid of One Sheet

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

In[69]:=

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"}]

[Graphics:HTMLFiles/index_106.gif]

Out[69]=

⁃Graphics3D⁃

Hyperboloid of Two Sheets

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

In[70]:=

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

[Graphics:HTMLFiles/index_110.gif]

Out[70]=

⁃Graphics3D⁃

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!

In[71]:=

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

[Graphics:HTMLFiles/index_113.gif]

Out[71]=

⁃Graphics3D⁃

In[72]:=

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

[Graphics:HTMLFiles/index_116.gif]

Out[72]=

⁃Graphics3D⁃

In[73]:=

uppercone = ParametricPlot3D[{s Cos[t], s Sin[t], s}, {s, 0, 2}, {t, 0, 2Pi}, DisplayFunction& ... ayFunctionIdentity] ; Show[uppercone, lowercone, DisplayFunction$DisplayFunction]

[Graphics:HTMLFiles/index_119.gif]

Out[75]=

⁃Graphics3D⁃

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.

[Graphics:HTMLFiles/index_121.gif]

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.

[Graphics:HTMLFiles/index_122.gif]

Exercise 4

Consider the surface given by x^2 + y^2 - 2z^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 = 2x^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


Created by Mathematica  (January 24, 2005)