Ma204 Contour and Cross-Section Demo

I'm always naming things similar names, and get tired of Mathematica asking if I've made a spelling error, and so turn off spelling messages before starting:

Off[General::spell]

This is the function that we'll be working with for the duration of this notebook

f[x_, y_] := - .1 (x^3) y;

In the following, the sole objective is to generate a bunch of cool 3D pictures to demonstrate contour plots and cross-sections, and how they are related.  If you want to jump right to the cool conclusory pictures, see the contour plot graphics, or the cross-section graphics.  Otherwise, read along for the development of the pictures.

A plot of the function with the "conventional" orientation is the following—here "conventional" means that x goes "out of the page", y  goes "left to right" and z  goes vertically.

splot1  =  
      Plot3D[f[x, y], {x, -3, 3}, {y, -3, 3}, 
        ViewPoint -> {2.816,  1.166,  1.470}, 
        AxesLabel -> {"x", "y", "z"}];

However, I want to turn it so that it compares nicely with generated contour plots, and so will graph it in the following orientation for the remainder of the notebook.

splot2  =  
      Plot3D[f[x, y], {x, -3, 3}, {y, -3, 3}, 
        ViewPoint -> {-1.349,  -2.628,  1.650}, 
        AxesLabel -> {"x", "y", "z"}];

Ok, now for some fun stuff !  Mathematica has a neat-o cool shadowplot function, which I want to use to demonstrate the relationship between the graph and the contours.  This is in a separate package, like the "Colors," etc., and so we need a "Needs" command to get access to it.

Needs["Graphics`Graphics3D`"]

Then the shadow plot is

splotShadow  =  
      ShadowPlot3D[f[x, y], {x, -3, 3}, {y, -3, 3}, 
        ViewPoint -> {-1.349,  -2.628,  1.650}];

and I want to compare this with the contour plot.  Because the shadowplot doesn't have contour lines, let's first plot without contour lines.

cplot1  =  
      ContourPlot[f[x, y], {x, -3, 3}, {y, -3, 3}, 
        ContourLines -> False];

Contour lines are in general nice, though, so it's worth plotting it all with this as well.

cplot2  =  
      ContourPlot[f[x, y], {x, -3, 3}, {y, -3, 3}, 
        ContourLines -> True];

So far, so good.  Now let's graph some planes to slice the 3D surfaces with to further demonstrate where the contours come from.  I'm choosing the plane at z = -1 here so that we can see most of the surface above the plane when we combine the graphs—this is the inside-most dark (downward) contour in the contour plot above.

splot2b  =  
      Plot3D[-1, {x, -3, 3}, {y, -3, 3}, 
        ViewPoint -> {-1.349,  -2.628,  1.650}, 
        AxesLabel -> {"x", "y", "z"}];

If we show this with the surface plot, we see how the plane slices the surface to give the contour.

splotSlice = 
      Show[splot2, splot2b, PlotRange -> All, AspectRatio -> 1];

and, as it turns out, this is even better seen with the shadow graphic.  I'm not sure why Mathematica selected black for the color of the plane here, but it makes things very clear.

splotShadSlice  =  Show[splotShadow, splot2b, AspectRatio -> 1];

Contour Plot Graphics

And, finally, let's show all of these different views all together in one array.  The top two pictures are the surface plots of f(x, y) and the surface plot with the height of the surface "shadowed" on the bottom plane.  Then the middle two graphics show this surface sliced by a plane at z = -1, which corresponds to the innermost dark (downwards, or below z = 0) contour in the contour plots shown in the bottom row.

contourdemo = 
      Show[GraphicsArray[{{splot2, splotShadow}, {splotSlice, 
              splotShadSlice}, {cplot1, cplot2}}]];

Next, let's look at cross-sections for f(x, y) .   These are generated by slicing the function with vertical planes—we'll look at x  =  -2 and y = -2.  The first of these is given in the following.  In this case we have to use ParametricPlot3D rather than Plot3D because the figure that we are generating is not technically a function!

vplot1 = 
      ParametricPlot3D[{-2, y, z}, {y, -3, 3}, {z, -7, 7}, 
        ViewPoint -> {-1.349,  -2.628,  1.650}, 
        AxesLabel -> {"x", "y", "z"}, AspectRatio -> 1];

Showing this together with the surface, we see the slice that is the cross-section nicely.

vslice1 = Show[vplot1, splot2];

We can plot this slice in two dimensions, too.  Here I've played around with the argument of f(x, y) and the axis labels so that I could pretend to plot the horizontal variable (y  in this case) from 3 to -3 rather than the more conventional -3 to 3.  This all goes back to having rotated the 3D figure around to be able to compare it with the contour plots earlier.

csxn1  =  
      Plot[f[-1, -y], {y, -3, 3}, AxesLabel -> {y, z}, 
        AspectRatio -> 1, 
        Ticks -> {{{3, "-3"}, {2, "-2"}, {1, "-1"}, {-1, 
                "1"}, {-2, "2"}, {-3, "3"}}, Automatic}];

Next, we can generate a plane at y = -2,

vplot2 = 
      ParametricPlot3D[{x, -2, z}, {x, -3, 3}, {z, -7, 7}, 
        ViewPoint -> {-1.349,  -2.628,  1.650}, 
        AxesLabel -> {"x", "y", "z"}, AspectRatio -> 1];

and combine it with the 3D surface.

vslice2 = Show[vplot2, splot2];

In this case the cross-section is easy to see, and in 2D is

csxn2 = 
      Plot[f[x, -2], {x, -3, 3},  PlotRange -> All, 
        AspectRatio -> 1, AxesLabel -> {x, z}];

Cross Section Graphics

Putting all of these together, we have the following.  The two graphs in the left column show a slice along x = -2, with the corresponding cross-section shown in the bottom row.  The scale on the y (horizontal) axis for that graph has been made to correspond to the orientation of the 3D figure.  The right column shows the cross section with y fixed at y = -2.

csxndemo  =  
      Show[GraphicsArray[{{vslice1, vslice2}, {csxn1, csxn2}}]];