Functions of two variables, contour plots and level curves
Department of Mathematics, Statistics and Computer Science
University of Illinois at Chicago
by Paul Brown, Heidi Burgiel, Marc Culler, Heather Dye and John Wood
Copyright © 2000, The University of Illinois at Chicago
Maple is a registered trademark of Waterloo Maple Software
In this lab you will use Maple to visualize graphs of functions of two variables. The graph of a function f(x,y) is the surface described by the points (x, y, f(x,y)) . One way to think of this is that f(x,y) describes the height of the surface above the point (x,y) .
Functions of two variables
Let us start with a simple function -- the distance between the point (x,y) and the origin. We can use Maple to this function as follows:
> f := (x,y) -> sqrt(x^2 + y^2);
If this really is the distance function, f(1,0) should equal 1 and f(1,1) should be the square root of 2.
The graph of the function f will be the surface described by the set of points (x, y, f(x,y)) as x and y range over the entire plane. In this case, the height of the surface above a point (x,y) in the plane will be equal to the distance f(x,y) from that point to the origin. What do you think this graph will look like? Hint: distances can never be negative, and the values of f must increase as the points (x,y) move away from the origin.
We can use Maple's 3D graphics to plot the graph of f . The command for plotting graphs of functions of two variables ( plot3d ) is very similar to the plot command for graphing functions of one variable, but now we must specify ranges for both the x and y variables. The argument color=ZHUE tells Maple to vary the color of the surface according to height.
> plot3d(f(x,y), x=-2..2, y=-2..2, axes=BOX, shading=ZHUE, scaling=constrained);
By "dragging" this plot with the left mouse button you can look at it from different viewpoints. (You may need to use the right mouse menu to redraw the plot after repositioning the graph.) Use the mouse to rotate the plot and view it from different directions. The shape of this graph should be familiar, but it may be difficult to recognize because of the way its corners are squared off. View it from different angles and over different x and y ranges to get a better understanding of this surface.
As with a function of one variable, we can estimate the value of the function at a point by looking at the graph of the function. The diagram above illustrates the fact that f(3,4)=5 .
Vertical slices -- functions of one variable
Another way to think about the graph of a function of two variables is to consider how the function changes if only one variable actually varies. For example, the value f(x,0) describes the distance from point (x,0) to the origin, which is exactly the absolute value of x . We can plot this function using the plot command for functions of one variable (because in this context y no longer varies!)
> plot(f(x,0), x=-2..2, scaling=constrained);
This v shaped graph is exactly what you get if you slice the graph of f(x,y) by the plane y=0 . If we look just at the grid line at y=0 on the surface of the graph of f(x,y) above, this is what we see.
> plot(f(x,2), x=-2..2, scaling=constrained, axes=box);
The above graph shows the shape of the grid line on the surface at y=2 ; this is the curve you see where the graph meets the right side of the bounding box. Notice that the range of values on the z axis runs from about 2 to 3; Maple has adjusted our plot so that the graph exactly fits in the z range. This is the graph of f(x,2) , that is, of the function "distance from (x,2) to the origin". It is a hyperbola.
By repeating this experiment, we could plot different cross sections of the graph of f . If we made wire models of all these cross sections and assembled them properly, we would see that the graph of f is a cone shaped surface. This makes sense -- as a point moves away from the origin along any straight line, its distance from the origin increases linearly as in the absolute value function, so the graph of f should slope smoothly upward away from the point (0,0,0) .
Use the plot command and the function f defined above to graph "distance from (2,y) to (0,0) " as a function of y . Sketch the graph ( units on your axes!) and describe its shape.
Horizontal slices -- contours
In the last section, we discovered that the graph of f was cone shaped by looking at some vertical slices of that graph. You might have had trouble determining this just by looking at the graph of f , since parts of the graph blocked your view of other parts of the graph, and the whole thing was rendered on a flat computer screen.
A common way of displaying information about a surface in three dimensions (for example, the surface of the earth) using only two dimensions (e.g. on a map) is to use level curves to indicate the parts of a surface that are at a certain height. Level curves can be found by slicing the graph with horizontal planes. You may have seen maps which use level curves to indicate the depths of oceans. The topographical maps used by hikers in the mountains also use level curves. Shown below is the graph of the function f , marked with contours showing curves of constant height on the surface of the graph.
> plot3d(f(x,y), x=-2..2, y=-2..2, axes=BOX, shading=ZHUE, style=patchcontour);
Some parts of the graph of f are still hidden behind other parts, but if we look straight down on the graph from the top, we can see the entire surface of the graph with a bull's eye pattern superimposed. (Use Maple to try this!) Each of the concentric circles in the bull's eye pattern is a curve of constant height on the surface of the graph. The regular arrangement of the circles indicates that the height of f(x,y) changes regularly as we move away from the origin.
Maple has a function, contourplot , which plots these level curves in the (x,y) plane. Such a plot is, naturally, called a contour plot . The individual level curves that make up the contour plot are sometimes called contour lines (even though they're usually curved!)
> contourplot(f(x,y), x=-2..2, y=-2..2, scaling=constrained);
The red circles at the center of the bull's eye are contour lines corresponding to small values of f (e.g. .4, .8, 1.2, ...), while the orange contours correspond to larger values of f (2.4 and 2.8). By looking at the flat contour plot we can tell that the three dimensional graph of the function f will be "low" near the origin and rise as you move outward. We also see that the value of f(x,y) depends only on distance from (0,0) , a fact which is not as obvious in the graph of f .
Earlier we said that contour graphs can be gotten by slicing the graph of a function of two variables by a horizontal plane. The slice is the intersection of the plane at height c and the surface f(x,y) , or the set of all points on the graph of f whose height above the xy -plane is c . In the two dimensional contour map, this slice corresponds to the set of points (x,y) for which f(x,y)=c .
The contours of the "distance from the origin" function f were circles. Next, we study a function g(x,y) = x^2 - y^2 whose contours are (mostly) hyperbolas. Use Maple to and graph g as shown below. View the graph from many different angles to get an idea of its shape.
> g := (x,y) -> x^2-y^2;
> plot3d(g(x,y),x=-2..2, y=-2..2, shading=ZHUE, axes=BOX);
The picture below shows the graph of g together with the plane z=1 . The intersection of these surfaces is the two curves that are colored red in the picture, and corresponds to the contour g(x,y)=1 in the (x,y) plane, shown below the graph.
> implicitplot(g(x,y)=1, x=-2..2, y=-2..2, scaling=constrained);
The next pictures shows the slice of the same surface by the plane z=-1 . Its corresponding contour plot shows the values of (x,y) for which f(x,y)=-1 .
> implicitplot(g(x,y)=-1, x=-2..2, y=-2..2, scaling=constrained);
Finally let's look at the slice of the same surface by the plane z=0 , i.e. the intersection of the surface and the xy -plane. In this example, the slice does not consist of two disjoint curves. Instead it consists of two straight lines crossing at the origin. (So these straight lines are contained inside the curved surface!) You can check this by rewriting the equation x^2-y^2 = 0 as (x-y)*(x+y)=0 .
> implicitplot(g(x,y)=0, x=-2..2, y=-2..2, scaling=constrained);
NOTE: These questions are about the distance function f(x,y) = sqrt(x^2 + y^2) from the first section. They can be answered without using Maple or by using the implicitplot function used above. If you get stuck, think about what f(x,y)=c means in terms of distance from the origin.
a) Make a hand drawing or use Maple's implicitplot function to show the intersection of the plane z=1 with the graph of f ; i.e. sketch the set of points (x,y) in the plane for which f(x,y)=1 .
b) Show the intersection of the plane z=0 with the graph of f .
Recall that a level curve for a function f(x,y) is a graph of the equation f(x,y) = c for some number c. The level curves for f(x,y) lie in the xy -plane. The points on the level curve are points where the function takes the value c.
A contour map of a function f(x,y) is a 2-dimensional graph showing several level curves corresponding to several values of c. (These are usually equally spaced values.) A contour map of f(x,y) lies in the xy -plane.
If the graph of a function is sliced by a horizontal plane and if the intersection curve is projected into the xy -plane, the result is a level curve. One way to view the graph of the function f(x,y) is by drawing many horizontal slices of the surface z=f(x,y) together. Maple's plot3d function will do exactly this if you specify the plot style contour .
If you haven't done so already, load the plots package, the function g(x,y) and plot it. Use the right mouse button to select contour style and ZHue color. You should see a graph like this:
Next rotate your plot so that you are viewing it from directly above. The image that you see looks like a contour map. The ZHUE shading helps you distinguish slices at different heights.
> plot3d(g(x,y), x=-2..2, y=-2..2, style=contour, axes=boxed, shading=ZHUE, orientation=[0,0]);
This is almost the same as what you get from the contourplot command we used earlier.
> contourplot(g(x,y), x=-1..1, y=-1..1, scaling=constrained);
To see the relationship between the level curves and the graph it is often helpful to plot the graph with the plot3d command using the PATCHCONTOUR style.
> plot3d(g(x,y), x=-2..2, y=-2..2, style=PATCHCONTOUR, axes=BOX, shading=ZHUE);
Open a Maple worksheet, a function h(x,y) = x^3 - 2*y^2 - x and load the plots package . Use the plot3d command to plot the graph of this function for yourself on the domain x=-1.5 .. 1.5, y=-1.5 .. 1.5 . Use the mouse to move the surface around and look at it from different angles. View it with ZHUE coloring and PATCHCONTOUR style. Use Maple's contourplot command to make a contour plot of the function h , then either print it out or sketch it. Shade in the regions on which h has the highest values (this is something like indicating the depth of an ocean on a map.)
Match each of the contour plots with its graph by writing the letter of the contour plot next to the name of the graph. HINTS: The graph of the function is steepest where its contours are closer together. Concentric rings in a contour map correspond to a peak or dimple in the graph of the function.
> plot3d(x/5 - y/4, x=-2..2, y=-2..2, shading=ZHUE, axes=BOX, title="plane");
> plot3d(x^2+y^2, x=-2..2, y=-2..2, axes=BOX, shading=ZHUE, title=parabaloid);
> plot3d(0.3*x^3-x+y^2, x=-2..2, y=-2..2, axes=box, color=0.3*x^3-x+y^2, title=cubic);
> plot3d(-1*sqrt(x^2+y^2), x=-2..2, y=-2..2, axes=box, color=-1*sqrt(x^2+y^2), title=cone);
Fun with plot3d
So far, we have looked at some fairly simple graphs. The graph of a function of two variables can be as complex and detailed as a map of the world. This plot is of the sine of the square of the distance of a point from the origin.
> plot3d(sin(x^2+y^2),x=-2.5..2.5,y=-2.5..2.5, axes=box, style=patchcontour);
The next plot was based on the Fourier series for a square wave. What do you predict will happen if you add more terms to the Fourier series? Try it!
> f1 := x-> Pi/4 + sin(x) + sin(3*x)/3 + sin(5*x)/5;
> plot3d(f1(x)*f1(y), x=-7..5, y=-7..5, grid=[33,33], axes=BOX, style=PATCHCONTOUR, shading=ZHUE);
Make up a few functions of your own or copy some from your textbook and see what their graphs look like!