Tutorial 07
3D Graphics:  Surface, Contour, and Density Plots

Copyright © 2003 by Bill Titus, Carleton College,
Department of Physics and Astronomy, Northfield, MN  55057-4025
March 1, 2003

Part 1 - Getting Started

Step 1 - Start up Mathematica and open the notebook  Tutorial07.nb.  Remember, this notebook is located in the workshop folder.

Step 2 - OPTIONAL.  Since you're going to edit this notebook, you need to make a copy of this notebook and work with that copy.  To do so, select  File / SaveAs  from the Mathematica menu and save the notebook in the workshop folder under the name  myTutorial07.nb.

Part 2 - Creating a Surface Plot

Topics and Skills

1.  The basic Mathematica command  Plot3D[]
2.  The options  PlotPoints,  PlotRange,  Mesh,  and  ViewPoint

Step 1.  In Tutorial 04, you defined a gravitational field function  gz[Δm,x,y,z,x0,y0,z0], which gives the value of  (Subscript[g, z])  at cartesian coordinate  {x,  y,  z},  corresponding to a spherical mass with density contrast  Δm,   located at  {(Subscript[x, 0]),  (Subscript[y, 0]), (Subscript[z, 0])}.   The origin of the cartesian coordinate system is on the surface earth, which is assumed to be level.  Here  Δm  is measured in units of  (10^6)kilograms and distance is measured in units of kilometers.  Activate the cell below which defines the function.

Clear[gz, Δm, x, y, z, x0, y0, 
      z0]
gz[Δm_, x_, y_, z_, x0_, y0_, 
        z0_] = -Δm*(z - 
              z0)/((((((x - x0))^2) + (((y - y0))^2) + (((z - z0))^2)))^(5/2))

Step 2.  Suppose the mass has  Δm = 0.001 and is located at  {0.00,  0.00,  -0.10}.  You want a visual representation of the field on the surface at  z = 0,  within the range  -0.20 ≤ x ≤ 0.20  and  -0.20 ≤ y ≤ 0.20.  So far, the only graphics tools you have are 2D plots, which you could use to show, for example, slices of  (Subscript[g, z])  at different values of  y.  But Mathematica allows you to create a surface plot of the field, using the function  Plot3D[],  where the plot's horizontal axes corresponds to the  x  and  y  positions on the earth's surface and the vertical axis gives the values for  (Subscript[g, z]).   Get extended help on the function  Plot3D[].

Comment 2

1.  Make sure you understand the usage statement for this function.  For example, to create a plot of the function  f[x ,y],  between  xMin ≤ x ≤ xMax  and  yMin ≤ y ≤ yMax,  you'd type in

				Plot3D[f[x,y],{x,xMin,xMax},{y,yMin,yMax}].
			
2.  Some of the options for  Plot3D[]  are the same as those for the 2D graphics functions, but there are some new ones that you'll find useful.  You'll explore several of those options in the comment section of the next step.

Step 3.  With the parameters given in Step 2, you'll now produce a basic surface plot of  (Subscript[g, z]).  To do so, look over the code in the cell below and make sure you understand what it's doing.  Then activate the cell.

Clear[Δm, x0, y0, z0, z, xMin, xMax, yMin, 
      yMax]
Δm = 0.001;
{x0, y0, z0} = {0.0, 0.0, -0.10};
z = 0;
{xMin, xMax} = {-0.2, 0.2}; {yMin, yMax} = {-0.2, 
        0.2};
Plot3D[
        gz[Δm, x, y, z, x0, y0, z0], {x, xMin, xMax}, {y, yMin, 
          yMax}];

Comment 3

1.  The reason the surface plot is not overly smooth is that Mathematica only calculated the values of  (Subscript[g, z])  on a 15 × 15 grid.  It then connected those grid points with localized plane surfaces.  You can control the size of the grid with the Mathematica option  PlotPoints.  The default setting for this option is 15.  Go back to the above code and see what happens if you insert the option  PlotPoints->30  in the code.  You should see a much finer grid and, consequently, a smother surface function.
2.  Some people don't like the grid explicitly showing.  You can eliminate the grid by invoking the option  Mesh->False.  Try this out in the code above.
3.  The axes can be labeled with the option  AxesLabel.  Go back to the code in the cell above, insert the Mathematica option  AxesLabel->{"x","y","gz"}  and see what happens.
4.  The next option you'll look at effects the view from which you observed the surface.  The view is controlled with the option  ViewPoint  that sets the viewing position relative to the center of the plotting box; the default setting for this option is  {1.3, -2.4, 2.0}.  Return to your code and insert the option  ViewPoint->{4.0, -2.0, 1.0} and watch what happens.
5.  Mathematica decided on its own how much of the range of  (Subscript[g, z])  to show.  If you want control over the range, use the option  PlotRange.  Try this out by inserting   PlotRange->{-8.0, 1.0}  into your code and then activate the cell.

Step 4.  Suppose there are two masses present, one with  (Subscript[Δm, 1]) = 0.001  located at  {0.0,  0.0,  -1.0},  and the other with  (Subscript[Δm, 2])= 0.003   located at  {0.20,  0.00,  -0.15}.  Create a surface plot for the field  (Subscript[g, z])  produced by the superposition of these two masses by activating the cell below.  Play around with the values for  ViewPoint  and see what happens.

Clear[Λm1, x1, y1, z1, Λm2, x2, y2, z2, 
      z, xMin, xMax, yMin, yMax]
Δm1 = 
      0.001; {x1, y1, z1} = {0.00, 
        0.00, -0.10};
Δm2 = 
      0.003; {x2, y2, z2} = {0.20, 
        0.00, -0.15};
z = 0;
{xMin, xMax} = {-0.2, 0.4}; {yMin, yMax} = {-0.2, 
        0.2};
Plot3D[
        gz[Δm1, x, y, z, x1, y1, z1] + 
          gz[Δm2, x, y, z, x2, y2, z2], {x, xMin, xMax}, {y, 
          yMin, yMax}, PlotPoints -> 30, Mesh -> False, 
        AxesLabel -> {"x", "y", "gz"}, 
        ViewPoint -> {2.0, 3.0, 0.0}, 
        PlotRange -> {-10.0, 1.0}];

Part 3 - Creating a Contour Plot

Topics and Skills

1.  The basic Mathematica command   ContourPlot[]
2.  The options  PlotPoints,  ContourShading,  Contours,  and  FrameLabel

Step 1.  Being a geologist, you know all about the relationship between 2D surfaces and contour plots.   Mathematica allows you to create contour plots by using the function  ContourPlot[].  Get extended help on this function.

Comment 1

1.  Make sure you understand the usage statement for this function.  For example, to create a basic contour plot for the function   f[x, y],  between  xMin ≤ x ≤ xMax  and  yMin ≤ y ≤ yMax,  you'd type in

				ContourPlot[f[x,y],{x,xMin,xMax},{y,yMin,yMax}] .
					
2.  Some of the options for  ContourPlot[]  are the same as those for the 2D graphics functions and that makes sense since a contour plot is, indeed, a 2D plot.  However there are some that are unique to  ContourPlot[]  and you'll explore several of those options in the comment section of the next step.

Step 2.  With the parameters given in Step 2 of Part 2, you'll now create a basic contour plot for  (Subscript[g, z]).  To do so, look over the code in the cell below and make sure you understand what it's doing.  Then activate the cell.

Clear[Λm, x0, y0, z0, z, xMin, xMax, yMin, 
      yMax]
Δm = 0.001;
{x0, y0, z0} = {0.0, 0.0, -0.10};
z = 0;
{xMin, xMax} = {-0.2, 0.2}; {yMin, yMax} = {-0.2, 
        0.2};
ContourPlot[
        gz[Δm, x, y, z, x0, y0, z0], {x, xMin, xMax}, {y, yMin, 
          yMax}];

Comment 2

1.  You can make the contours smoother by using the option  PlotPoints.  Try out this option in the above code by inserting   PlotPoints->30.
2.  If you don't like the shading in the contour plot, you can eliminate it by invoking  ContourShading->False.  Try this out in the cell above.
3.  The default number of contours that Mathematica shows is  10.  You can change this number to  20,  for example, by inserting   Contours->20 in the function.  Do this and see what happens.
4.  Mathematica has decided what values of  (Subscript[g, z])  to pick for the contours.  You can override this decision by using the option  Contours.  See what happens to the plot when you type in  Contours->{-0.5,-1.0,-1.5,-2.0,-2.5,-3.0}.
5.  The final option you'll look is  FrameLabel,  which allows you to label the horizontal and vertical axes.  Verify that this assertion is true by inserting the option  FrameLabel->{"x","y"}  into the code above.

Step 3.  Suppose there are two masses present, one with  (Subscript[Δm, 1]) = 0.001  located at  {0.0,  0.0,  -1.0},  and the other with  (Subscript[Δm, 2])= 0.003   located at  {0.20,  0.00,  -0.15}.  Create a contour plot for the field  (Subscript[g, z])  produced by the superposition of these two masses by activating the cell below.

Clear[Λm1, x1, y1, z1, Λm2, x2, y2, z2, 
      z, xMin, xMax, yMin, yMax]
Δm1 = 
      0.001; {x1, y1, z1} = {0.00, 
        0.00, -0.10};
Δm2 = 
      0.003; {x2, y2, z2} = {0.20, 
        0.00, -0.15};
z = 0;
{xMin, xMax} = {-0.2, 0.4}; {yMin, yMax} = {-0.2, 
        0.2};
ContourPlot[
        gz[Δm1, x, y, z, x1, y1, z1] + 
          gz[Δm2, x, y, z, x2, y2, z2], {x, xMin, xMax}, {y, 
          yMin, yMax}, PlotPoints -> 100, ContourShading -> False, 
        FrameLabel -> {"x", "y"}];

Comment 3

1.  I had to boost up the number for  PlotPoints  up to  100  so as to make the contours smooth.

Part 4 - Creating a Density Plot

Topics and Skills

1.  The basic Mathematica command   DensityPlot[]
2.  The options  PlotPoints,   Mesh,   ColorFunction,  and   FrameLabel
3.  The graphics directive   Hue

Step 1.  The last graphics function you'll investigate is  DensityPlot[].  Get extended help on this function.

Comment 1

1.  Make sure you understand the usage statement for this function.  For example, to create a basic density plot for the function   f[x, y],  between  xMin ≤ x ≤ xMax  and  yMin ≤ y ≤ yMax,  you'd type in

				DensityPlot[f[x,y],{x,xMin,xMax},{y,yMin,yMax}] .
			
Note that this format is identical to that used in   Plot3D[]  and  ContourPlot[].
2.  Again, some of the options for  ContourPlot[]  are the same as those for the 2D graphics functions.  However there some that are unique to this command and you'll explore several of those options in the comment section in the next step.

Step 2.  With the parameters given in Step 2 of Part 2, you'll now create a basic density plot for  (Subscript[g, z]).  To do so, look over the code in the cell below and make sure you understand what it's doing.  Then activate the cell.

Clear[Δm, x0, y0, z0, z, xMin, xMax, yMax, 
      yMin]
Δm = 0.001;
{x0, y0, z0} = {0.0, 0.0, -0.10};
z = 0;
{xMin, xMax} = {-0.2, 0.2}; {yMin, yMax} = {-0.2, 
        0.2};
DensityPlot[
        gz[Δm, x, y, z, x0, y0, z0], {x, xMin, xMax}, {y, yMin, 
          yMax}];

Comment 2

1.  You can increase the resolution by using the option  PlotPoints.  Apply this option to the code above to increase the grid size to  30 × 30.
2.  You can eliminate the grid by typing  Mesh->False.  Do so.
3.  Black and white is such a drag.  See what happens when you invoke the option  ColorFunction->Hue.
4.  Finally, use  FrameLabel  to label your axes  as  x  and  y.   Also boost up the number of   PlotPoints  so the density plot looks smoother.

Step 3.  Suppose there are two masses present, one with  (Subscript[Δm, 1]) = 0.001  located at  {0.0,  0.0,  -1.0},  and the other with  (Subscript[Δm, 2])= 0.003  located at  {0.20,  0.00,  -0.15}.  Create a density plot for the field  (Subscript[g, z])  produced by the superposition of these two masses by activating the cell below.

Clear[Λm1, x1, y1, z1, Λm2, x2, y2, z2, 
      z, xMin, xMax]
Δm1 = 
      0.001; {x1, y1, z1} = {0.00, 
        0.00, -0.10};
Δm2 = 
      0.003; {x2, y2, z2} = {0.20, 
        0.00, -0.15};
z = 0;
{xMin, xMax} = {-0.2, 0.4}; {yMin, yMax} = {-0.2, 
        0.2};
DensityPlot[
        gz[Δm1, x, y, z, x1, y1, z1] + 
          gz[Δm2, x, y, z, x2, y2, z2], {x, xMin, xMax}, {y, 
          yMin, yMax}, PlotPoints -> 100, Mesh -> False, 
        FrameLabel -> {"x", "y"}, 
        ColorFunction -> Hue];

Comment 3

1.  I had to boost up the number of   PlotPoints to  100  so as to make the density plot look smooth.

Part 5 - Save Your Notebook (OPTIONAL)

Step 1.  Remove all output cells from this notebook, and to do one final save of your notebook before you quit Mathematica.