转自:http://msemac.redwoods.edu/~darnold/math50c/matlab/contours/index.xhtml
Contour Maps in Matlab
In this activity we will introduce
Matlab's contour command,
which is used to plot the level curves of a multivariable function.
Let's begin with a short discussion of the level curve concept.
Level Curves
Hikers and backpackers are likely to take along a copy of a
topographical map when verturing into the wilderness (see Figure
1).
A topographical map has lines of constant height.
If you walk along one of the contours shown in Figure 1, you will
neither gain nor lose elevation. You're walking along a curve of
constant elevation. If you walk directly perpendicular to a
contour, then you are either walking directly downhill or uphill.
When the contours are far apart, the gain or loss in elevation is
gradual. When the contours are close together, the gain or loss in
elevation is quite rapid.
The level curves of a multivariate function are analogous to the
contours in the topographical map. They are curves of constant
elevation. Let's look at an example.
Sketch several
level curves of the function f(x,y)=x2+y2.
Solution:We are interested in
finding points of constant elevation, that is, solutions of the
equation
f(x,y)=c,
where c is
a constant. Equivalently, we wish to sketch solutions of
x2+y2=c,
where c is
a constant. Of course, these "level curves" are circles, centered
at the origin, with radius c. These level
curves are drawn in Figure 2 for
constants c=0, 1, 2, 3, and
4.
Level curves of f(x,y)=x2+y2 lie
in the xy-plane.
Matlab:It's a simple task to
draw the level curves of Figure 2 using
Matlab's contour command.
We begin as if we were going to draw a surface, creating a grid
of (x,y) pairs
with the meshgridcommand.
x=linspace(-3,3,40);
y=linspace(-3,3,40);
[x,y]=meshgrid(x,y);
We then use the function f(x,y)=x2+y2, or
equivalently, z=x2+y2, to
calculate the z-values.
z=x.^2+y.^2;
Where we would normally use
the mesh command
to draw the surface, instead we