Learning Mathematica Basics—Graphics III

Solve each of the following problems using Mathematica exclusively.

1.  So far we have concentrated on plotting functions of a single variable which only requires the use of two dimensions to make a graphical representation.  Many functions, however, depend on two or more independent variables.  We'll concentrate on just two for now!

A simple example of this kind of function would be z = x^2 + y^2.  There are several ways in which we could have Mathematica plot this function for us.

To start with we might use the method used by cartographers (map-makers) in representing the height of the landscape—curves on the map along which all of the terrain is of equal altitude.  Such representations are usually referred to as topographical maps, but in mathematics we refer to such graphs as contourplots.  Mathematica's built in command for this is ContourPlot.  Ask for its syntax.

2.   Well, the command looks quite straightforward.  Let's make a plot of the example we gave above, using a domain of -2≤x≤2, and -2≤y≤2.  Issue the command:

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

3.   You're probably unimpressed with your results, but that's an inherant problem with this kind of representation.  You have to use your imagination to even visualize what this plot is telling you about the function.  Contourplots obey the convention that all points along one of the curves (contours) are of the same height (on a map) or have the same function value (in mathematics.)  Another convention is that the contours are equally spaced throughout the range of the function.  (e.g. On a map there may be a contour curve for every 200 ft of vertical elevation.)

As a result of these conventions we can make the following observations about the plot in the last example.  

a)  The function is symmetric about the origin.
b)  The contours get closer together as you move away from the origin, implying that the surface is getting steeper the further you move out.

You may also have noticed that I included an unexpected option in the last problem, namely ContourShading->False.  The reason I did this was to make our picture mimic a topographical map as closely as possible.  Now re-issue the command in problem 2 with this option removed.

4.   So what does the shading mean?  Well basically it means height!  Lighter means higher, darker means lower.  This element adds a lot to our ability to visualize the surface, but it's still not ideal.

Another possible representation is to use Mathematica's DensityPlot.  Ask for its syntax.

5.   Try redoing the problem 3, but replace the ContourPlot command with DensityPlot.

6.   Well, that's no more impressive than the contourplot we did earlier!  What we'd really like is to see a nice three-dimensional representaion of our function.

Mathematica can handle three-dimensional plots quite easily.  The command we need is Plot3D.  Ask for its syntax.

7.   The command is almost identical in syntax to both ContourPlot and DensityPlot.  Use it to represent our original example again.

8.   At last!  A surface we can easily visualize.  (Does it look like you imagined it would?)

Now let's look at the ways in which we could tweak this picture.  Issue the command Options[Plot3D] to find out the ways in which we can change the look of our plot.

9.   Woah!  That's a lot of buttons we can push!  Let's see!  What should we play with first?

Well, I'm not sure I like the scales that Mathematica chose when it made the three-dimensional graph. The horizontal scales are OK, but the vertical scale looks kind of squished.  The option which controls this is BoxRatios.  Notice that the default settings are{1, 1, 0.4}.  There's no wonder the graph looks squished.  Redo problem 7, but set the BoxRatios to {1, 1, 1}.

10.   That looks better, but now I'd like to look at the surface from a different angle, maybe a bit lower.  This is controlled by the ViewPoint option.  The default appears to be {1.3, -2.4, 2.}.  (These numbers represent the x, y, and z coordinates of the viewer's eye in space.)  Judging where you'd like to view a surface from can be difficult.  For this reason, Mathematica provides a "3D-ViewPoint Selector."  It can be found in the Action menu under Prepare Input.  Before you choose it, however, first retype the last command, and click your cursor to the left of the closing bracket, and type a comma.  (This is right where our new option will need to be inserted.)  Then coose the 3D-ViewPoint Selector, rotate the box to a viewpoint you like, and click on the Paste button.

11.  Well now it's your turn.  Create all three kinds of plots (contour, density, and 3D) for the functions given below on the domains specified.  Adjust the options freely to make your pictures look nice.  (We've only scratched the surface of the Options available—try some others too.  Hint: One option that helps when a surface looks really rough is PlotPoints.)

a)   z = sin(xy)  on -π/2 ≤ x ≤ π/2,   -π/2 ≤ y ≤ π/2

b)   z = sin x sin y  on -π ≤ x ≤ π,   -π ≤ y ≤ π

c)   z = e^-Sqrt(x^2 + y^2) cos(x^2 + y^2)  on -π ≤ x ≤ π,   -π ≤ y ≤ π