Level sets and contour plots on Mathematica
D. McClendon

Plotting a single level curve of a function (ℝ^2) -> ℝ

Recall that a level curve of a function f : (ℝ^2) -> ℝ is a set of the form (f^(-1))(c), where c is a constant.  To sketch such a set in Mathematica, use the ContourPlot command.  For example, to sketch the level curve to f(x,y) = (x^2)+ xy - sin(x-2y) at height 2 (in the viewing window [-4,4] × [-2,6]), use this syntax:

f[x_,y_] = x^2 + x y - Sin[x - 2y];
ContourPlot[f[x,y] == 2, {x, -4,4},{y,-2,6}]

Notice that there are two equals signs in the equation inside the ContourPlot command.

ContourStyle

You can make the contour appear differently using the ContourStyle command:

f[x_,y_] = x^2 + x y - Sin[x - 2y];
ContourPlot[f[x,y] == 2, {x, -4,4},{y,-2,6},
ContourStyle -> {Red, Thick}]

Contour plots of functions (ℝ^2) -> ℝ

A contour plot of a function is a collection of level sets to that function, all drawn on the same axes.  The ContourPlot command can be used to produce these pictures; for example, here is a contour plot of  f(x,y) = (x^2)+ xy - sin(x-2y):

f[x_,y_] = x^2 + x y - Sin[x - 2y];
ContourPlot[f[x,y], {x, -4,4},{y,-2,6}]

The darker regions correspond to larger values of f; the lighter regions are smaller values.  To get a picture which is more useful, I like to adjust the ContourPlot command in several ways.  First, I get rid of the shading with ContourShading -> False.  Then, I insert labels for each level set with ContourLabels -> True:

f[x_,y_] = x^2 + x y - Sin[x - 2y];
ContourPlot[f[x,y], {x, -4,4},{y,-2,6},
ContourShading -> False,
ContourLabels -> True]

Each of the numbers seen in this picture indicates the height of the level curve (i.e. the value of c).  In general, Mathematica just picks these values, but if you want to specify which heights you want to use, use the Contours command and list the heights you want:

f[x_,y_] = x^2 + x y - Sin[x - 2y];
ContourPlot[f[x,y], {x, -4,4},{y,-2,6},
Contours -> {-1,0,3,8,12,16,20,30},
ContourShading -> False,
ContourLabels -> True]

If you want to change the colors of the contours, you can use ContourStyle.  To change the way the labels look, use ContourLabelsStyle.

Plotting a single level surface of a function (ℝ^3) -> ℝ

A level surface of a function f : (ℝ^3) -> ℝ is a set of the form (f^(-1))(c), where c is a constant; this is a subset of (ℝ^3).  To sketch such a set in Mathematica, use the ContourPlot3D command.  For example, to sketch the level surface to f(x,y,z) = (x^2)+ xyz at height 1 (in the viewing window [-3,3] × [-1,5] × [-3,3]), use this syntax:

f[x_,y_,z_] = x^2 + x y z;
ContourPlot3D[f[x,y,z]==1, {x, -3,3},{y,-1,5}, {z,-3,3}]

AxesLabel can be used to label the axes.  Use ContourStyle to adjust how the level surface looks (the Opacity command, introduced in the file on surfaces, is particularly useful here).  Mesh also works:

f[x_,y_,z_] = x^2 + x y z;
ContourPlot3D[f[x,y,z]==1, {x, -3,3},{y,-1,5}, {z,-3,3},
AxesLabel -> {"x","y","z"},
AxesStyle -> Larger,
ContourStyle -> {Blue, Opacity[.5]},
Mesh -> 6]