Tutorial 9:  3-D Graphs

by R. Dover, IMSA

Basic graphing

The command to plot a 3-dimensional function uses familiar coding, with the exception that it is now necessary to give domain values for both x and y.  Try the following examples.

Plot3D[
      Sin[x y],  {x,  -3,  3},  {y,  -3,  3}]

Plot3D[
      Exp[1  -  (x^2) + (y^2)],  {x,  -7,  7},  {y,  -3,  
        3}]

Plot3D[
      Cos[(x^2) + (y^2)],  {x,  -3,  3},  {y,  -3,  
        3}]

This last one in particular isn't very smooth.  Ask Mathematica for the options for "Plot3D."  Specifically, note the default value for "PlotPoints."  Add this command to the last graph above, increasing the number of points used.  Note that there are trade-offs:  The graph may look much better, but the computer may need significantly more time and memory to draw the graph, depending upon the number you have chosen.

Use the "Help" system to look under "Plot3D."  Check the examples to see how other options may be used.  Try some options on the functions above - or create your own. (Remember the appropriate space when writing xy.)

Related graphs

We'll go back to  z = Cos((x^2) + (y^2)) for a moment.  To see a contour graph with shading, execute the following.

ContourPlot[
      Cos[(x^2) + (y^2)],  {x,  -3,  3},  {y,  -3,  3},  
      PlotPoints  ->  25]

To see the same graph without the shading, add the option "ContourShading -> False," giving what normally comes to mind for a contour map or a set of level curves.  Then check the density plot below for another approach to understanding the function.

DensityPlot[
      Cos[(x^2) + (y^2)],  {x,  -3,  3},  {y,  -3,  3},  
      PlotPoints  ->  25]

More examples

Below are commands to graph some functions that are more interesting, particularly around the origin.  (Hopefully, the reasons why should be evident!)  For some, Mathematica will give error messages, but it should still draw a graph.  You may find it useful to use a higher value for PlotPoints or to play with the domains a bit.

Plot3D[((x y)/((x^2) + (y^2))),  {x,  -1,  
        1},  {y,  -1,  1}]

Plot3D[y/(x^3),   {x,  -1,  1},  {y,  -1,  1}]

Plot3D[
      Sin[1/(x y)],   {x,  -1,  1},  {y,  -1,  
        1}]

Plot3D[
      Log[x y],   {x,  -1,  1},  {y,  -1,  1}]

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

On any of these graphs, it is also possible to change the point of view.  You may have noticed in the options that the default "ViewPoint" is (1.3, -2.4, 2).  Adding the option in the command line with alternate numbers is one way to change this, but there's another method which is usually more fun.
	In any of the inputs above, place a comma inside the final closing bracket and leave the cursor there.  From the "Input" menu, choose "3D ViewPoint Selector..."    Put the cursor on the box, click, and drag it as desired.  When you get to a position you like, release the mouse, and click on "Paste."  The ViewPoint option will be entered automatically. Then execute the cell to see what you did!  (Of course, if you prefer, you are welcome to use the drag bars or enter specific numbers in either spherical or cartesian form instead.)  Try a few.

3D Parametric plots

As you might have guessed, Mathematica can also do 3D parametric curves.  One must give expressions for x, y, and z in terms of t as well as the domain values for t.  Check out this one.

ParametricPlot3D[{Cos[2  t],  Cos[t],  Sin[3  t]},  {t,  0 ,  
        2  π}]

Or would you like to see 2 plots?

ParametricPlot3D[{{t,  Sin[t],  Cos[t]},  {Cos[t],  t,  
          Sin[t]}},  {t,  0,  2  π}]

Lots of RAM?

We're serious here about the RAM for this next cute trick.  (It may be helpful to cut the graphics cells from the previous examples in this notebook.)  If you're confident, then the first thing to do is to load a special package.  Under "Help," choose "Add-Ons."  Then choose "Standard Packages,"  "Graphics" and "Animation."  Execute the appropriate cell to load the package.  After doing this, execute the following:

SpinShow[
      Plot3D[Sin[x + Sin[y]],  {x,  -3,  3},  {y,  -3,  3}],  
      Frame  ->  12]

After all the graphs are created, click on the cell bar to select all 12 graphs. From the "Cell" menu, choose "Animate Seclected Graphics."  Enjoy.