Plotting 3D Surfaces
and Level Curves

We’ll use Mathematica to plot functions from (R^2) to R and (sorta) from (R^3) to R .

The general command is Plot3D[ f [x, y], {x, a, b}, {y, c, d}], where
f [x, y] is a function that uses two variables as inputs
{x, a, b} tells Mathematica the x domain
{y, c, d} tells Mathematica the y domain
 

Let’s get the party started!

Plot3D[Sin[x]+Sin[y],{x,-Pi,Pi}, {y, -Pi, Pi}]

Not that attractive or interesting? Let’s increase the domain.

Plot3D[Sin[x]+Sin[y],{x,-6*Pi,6*Pi}, {y, -6*Pi, 6*Pi}]

Four things of interest:
1. Notice that you may “click-and-grab” the graph and spin it around.
2. The ‘magic bar’ below the output offers other possibly interesting things to do to the output. Play with it!
3. Here’s a reference page that gives all sorts of optional commands for Plot3D:
https://reference.wolfram.com/language/tutorial/ThreeDimensionalSurfacePlots.html
4. We may also plot a finite number of functions at the same time over the same domain using this command (example below): 
Plot3D[ {f [x, y], g [x, y], ... , h [x, y] }, {x, a, b}, {y, c, d}]

Plot3D[{Sin[x]+Sin[y],Cos[x]+Cos[y]},{x,-6*Pi,6*Pi}, {y, -6*Pi, 6*Pi}]

We’ll also use Mathematica to find level curves and level surfaces. The command structure is similar: ContourPlot[ f [x, y], {x, a, b}, {y, c, d} ]

Below are the level curves the first function we did.

ContourPlot[Sin[x]+Sin[y],{x,-6*Pi, 6*Pi},{y,-6*Pi,6*Pi}]

If we want a specific level curve, we modify the command just a little bit. The general command is: ContourPlot[ f [x, y] == q, {x, a, b}, {y, c, d} ], where q is the value of the level curve we’d like to plot. I don’t know why Mathematica insists on the double-equal sign ‘ == ’ instead of ‘ = ’ but we must live with it.

Below is continued the example setting the level curve of 1/2; that is, what is plotted is every (x, y) pair in the square [ –6π, 6π ] x [ –6π, 6π ] that yields a true value for the equation: Sin[x] + Sin[y] = 1/2.

ContourPlot[Sin[x]+Sin[y]== 1/2,{x,-6*Pi, 6*Pi},{y,-6*Pi,6*Pi}]

It’s easy enough also to find the level curves for a bunch of values at the same time. Instead of “ == c ” simply throw in Mathematica’s beloved curly braces. It looks like this in the abstract: “ f [x, y] == {a, b, c, d, e, ... , n} ” as in the example below. There, we find level curves (places where the function is constant) simultaneously for five values.

ContourPlot[Sin[x]+Sin[y]== {-1,-1/2,0,1/2,1},{x,-6*Pi, 6*Pi},{y,-6*Pi,6*Pi}]

We can up this game to higher dimensions; we can plot level surfaces instead of level curves for functions of three variables. That is, if we have a function f(x, y, z) → R, we can’t plot it. (Why not?) But we can ask a similar question to that above: Which triplets of coordinates (x, y, z) pair in the cube [ –6π, 6π ] x [ –6π, 6π ] x [ –6π, 6π ] = 1/2 for the function Sin[x] + Sin[y] + Sin[z]. 

(By the way, there’s nothing special about this function; I just used it because it has lots of bumps and looks kinda dramatic.)

The command is, of course, similar: 

ContourPlot3D[ f [x, y, z] == q, { x, a, b }, { y, c, d }, { z, e, f } ]

We also must specify the domain of acceptable z values, { z, e, f }. Below is an example that produces a very complicated level surface for the function we’ve been playing with. My guess is that Mathematica is able to fairly quickly decide which (x, y, z) triplets are such that they sum to 1/2 ; I also think it’s a bear to plot. If you execute the command below, be forewarned: It probably takes a minute or more for the level surface to appear.

ContourPlot3D[Sin[x]+Sin[y]+Sin[z]== 1/2,{x,-6*Pi, 6*Pi},{y,-6*Pi,6*Pi}, {z,-6*Pi,6*Pi}]

As promised, if you clicked the command, the above graphic took a lot of time to render. At the top bar for Mathematica, it included the phrase “Running...” to let the viewer know it was hard at work.

I’m going to try one more example to see if it’s any happier; the case where they sum to 2. I’m also shrinking the domain, hoping it will produce the graphic faster and happier.

ContourPlot3D[Sin[x]+Sin[y]+Sin[z]== 2,{x,-2*Pi, 2*Pi},{y,-2*Pi,2*Pi}, {z,-2*Pi,2*Pi}]

Here’s an example I lifted from Wolfram. Because it’s cool.

ContourPlot3D[x^4+y^4+z^4-(x^2+y^2+z^2)^2+3 (x^2+y^2+z^2)==3,{x,-2,2},{y,-2,2},{z,-2,2},Mesh->None,ContourStyle->Directive[Orange,Opacity[0.8],Specularity[White,30]]]

If anyone wants to get superduper cool, and plot both the surface and the level curves at the same time, there are evidently ways to do it. Here’s a webpage with examples. (They are, to my mind, more complicated than I need you to learn. However, if it excites you to be able to visualize functions—and scientists and economists, it should exciteyou to learn how to visualize data—then you may want to play around and figure out all of this.)

http://mathematica.stackexchange.com/questions/14863/placing-a-contourplot-under-a-plot3d

If I had time to play around with these things, I would. But I don’t, so you should.

contourPotentialPlot1=ContourPlot[-3600. h^2+0.02974 h^4-5391.90 s^2+0.275 h^2 s^2+0.125 s^4,{h,-400,400},{s,-300,300},PlotRange->{-1.4*10^8,2*10^7},Contours->15,Axes->False,PlotPoints->30,PlotRangePadding->0,Frame->False,ColorFunction->"DarkRainbow"];

potential1=Plot3D[-3600. h^2+0.02974 h^4-5391.90 s^2+0.275 h^2 s^2+0.125 s^4,{h,-400,400},{s,-300,300},PlotRange->{-1.4*10^8,2*10^7},ClippingStyle->None,MeshFunctions->{#3&},Mesh->15,MeshStyle->Opacity[.5],MeshShading->{{Opacity[.3],Blue},{Opacity[.8],Orange}},Lighting->"Neutral"];

level=-1.2 10^8;gr=Graphics3D[{Texture[contourPotentialPlot1],EdgeForm[],Polygon[{{-400,-300,level},{400,-300,level},{400,300,level},{-400,300,level}},VertexTextureCoordinates->{{0,0},{1,0},{1,1},{0,1}}]},Lighting->"Neutral"];

Show[potential1,gr,PlotRange->All,BoxRatios->{1,1,.6},FaceGrids->{Back,Left}]

More examples from the above link from other people WHO HAVE TOO MUCH TIME ON THEIR HANDS.

level=-1.2 10^8;
pts=Append[#,level]&/@contourPotentialPlot1[[1,1]];
cts=Cases[contourPotentialPlot1,Line[l_],Infinity];
cts3D=Graphics3D[GraphicsComplex[pts,{Opacity[.5],cts}]];

Show[potential1,cts3D,PlotRange->All,BoxRatios->{1,1,.6},FaceGrids->{Bottom,Back,Left}]

One last one from that page. Note that this person figured out how to project the surface to different planes. Amazing.

potential1/.Graphics3D[gr_,opts___]:>Graphics3D[{gr,Scale[gr,{1,1,1/100},{0,0,-2 10^8}],Scale[gr,{1/100,1,1},{-400,0,0}]},PlotRange->All,opts]