Mathematica can make very nice contour and surface
plots of three-dimensional functions such as z = z(x, y).
The commands that generate these plots are **ContourPlot**
and **Plot3D** for contour and surface plots,
respectively.

These commands have the same basic form, although there
are more "options" for **Plot3D**. To make a plot
of some function of x and y in which x ranges from xmin
to xmax and y ranges from ymin to ymax, enter

orContourPlot[ <function>, {x, <xmin>, <xmax>}, {y, <ymin>, <ymax> ]

For example, a contour plot of the function Exp[-(x^2 + 3 y^2)] near the origin can be generated as follows:Plot3D[ <function>, {x, <xmin>, <xmax>}, {y, <ymin>, <ymax> ]

A three-dimensional surface plot of the same function is generated with the commandContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]

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

If you try these commands, you might find that the
surface plot seems "chopped off" near the origin.
We can rectify this by including a larger range
of z coordinates in the plot, using the **PlotRange**
option that we first saw in two-dimensional plots:

This command tells Mathematica to include all parts of the function with z values between 0 and 2. We can also use thePlot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2},PlotRange -> {0, 2} ]

Plot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]Show[ %, PlotRange -> {{0, 1}, {0, 1}, {0, 2}} ]

You might notice that the resolution in this "zoomed" plot
is fairly poor. Mathematica creates surface plots by
scanning over a rectangular grid of points and calculating
the height of the surface at each point. When we zoom in,
Mathematica continues to use the original grid of points.
We can make the grid finer by using the **PlotPoints**
option in **Plot3D**. (Note that this option does **not**
work in the **Show** command!) Here is how we might
increase the resolution of this zoomed plot:

Plot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2}, PlotPoints-> 30 ]Show[ %, PlotRange -> {{0, 1}, {0, 1}, {0, 2}} ]

Of course, if you decided to zoom in after viewing a surface
plot, you can combine the **PlotPoints** and **PlotRange**
options in the **Plot3D** command to do everything in one
step:

Even this is wasteful in a sense; we are generating a 30-by-30 grid of points in which x and y both range from -2 to 2, and then throwing away all of the points except those where x and y are between 0 and 1. Instead, we might tryPlot3D[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2}, PlotPoints-> 30,PlotRange -> {{0, 1}, {0, 1}, {0, 2}} ]

which gives us higher resolution in the region of interest.Plot3D[ Exp[-(x^2 + 3 y^2)], {x, 0, 1}, {y, 0, 1}, PlotPoints-> 30,PlotRange -> {0, 2} ]

The **PlotPoints** option applies to contour plots as well.
Compare these two plots:

The plot with more points is much smoother, and represents the oval shape of the surface more faithfully. But how can we get rid of the shades of gray so that we can see the contours better? TryContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]ContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2},PlotPoints -> 30 ]

Nice, isn't it? The only problem is that we don't know what z values the contour lines correspond to. With theContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2},PlotPoints -> 30, ContourShading -> False ]

to see how you can control the placement of contour lines. Note that we need to includeContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2},PlotPoints -> 30, ContourShading -> False,Contours -> {0.2, 0.4, 0.6, 0.8},PlotRange -> {0, 2} ]

Finally, note that the variables x and y could be called anything we want. These two commands produce exactly the same results:

ContourPlot[ Exp[-(x^2 + 3 y^2)], {x, -2, 2}, {y, -2, 2} ]ContourPlot[ Exp[-(u^2 + 3 v^2)], {u, -2, 2}, {v, -2, 2} ]

Other parts of the Mathematica tutorial:

- Mathematica's user interface
- Simple calculations
- Plotting lists of points
- Plotting functions
- Combining two or more plots
- Fitting data to polynomials
- Generating and manipulating lists of numbers
- Complex numbers
- Derivatives and integrals
- Special functions