Tải bản đầy đủ (.pdf) (24 trang)

MATLAB Graphics

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (406.98 KB, 24 trang )

Chapter 5
MATLAB Graphics
In this chapter we describe more of MATLAB’s graphics commands and the
most common ways of manipulating and customizing them. You can get a
list of MATLAB graphics commands by typing help graphics (for general
graphics commands), help graph2d (for two-dimensional graphing), help
graph3d (for three-dimensional graphing), or help specgraph (for special-
ized graphing commands).
We have already discussed the commands plot and ezplot in Chapter 2.
We will begin this chapter by discussing more uses of these commands, as well
as the other most commonly used plotting commands in two and three dimen-
sions. Then we will discuss some techniques for customizing and manipulating
graphics.
Two-Dimensional Plots
Often one wants to draw a curve in the x-y plane, but with y not given explicitly
as a function of x. There are two main techniques for plotting such curves:
parametric plotting and contour or implicit plotting. We discuss these in turn
in the next two subsections.
ParametricPlots
Sometimes x and y are bothgiven as functions of some parameter. For example,
the circle of radius 1 centered at (0,0) can be expressed in parametric form as
x = cos(2πt), y = sin(2πt) where t runs from 0 to 1. Though y is not expressed
as a function of x, you can easily graphthis curve with plot, as follows:
>> T = 0:0.01:1;
67
68
Chapter 5: MATLAB Graphics
>> plot(cos(2*pi*T), sin(2*pi*T))
>> axis square
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
-1


-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Figure 5-1
The output is shown in Figure 5.1. If you had used an increment of only 0.1in
the T vector, the result would have been a polygon with clearly visible corners,
an indication that you should repeat the process with a smaller increment
until you get a graph that looks smooth.
If you have version 2.1 or higher of the Symbolic Math Toolbox (cor-
responding to MATLAB version 5.3 or higher), then parametric plotting is also
possible with ezplot. Thus one can obtain almost the same picture as Figure
5-1 withthe command
>> ezplot(’cos(t)’, ’sin(t)’, [0 2*pi]); axis square
Two-Dimensional Plots
69
Contour Plots and Implicit Plots
A contour plot of a function of two variables is a plot of the level curves of the
function, that is, sets of points in the x-y plane where the function assumes
a constant value. For example, the level curves of x
2
+ y
2
are circles centered

at the origin, and the levels are the squares of the radii of the circles. Contour
plots are produced in MATLAB with meshgrid and contour. The command
meshgrid produces a grid of points in a specified rectangular region, witha
specified spacing. This grid is used by contour to produce a contour plot in
the specified region.
We can make a contour plot of x
2
+ y
2
as follows:
>> [X Y] = meshgrid(-3:0.1:3, -3:0.1:3);
>> contour(X, Y, X.ˆ2 + Y.ˆ2)
>> axis square
The plot is shown in Figure 5-2. We have used MATLAB’s vector notation to
-3 -2 -1 0 1 2 3
-3
-2
-1
0
1
2
3
Figure 5-2
70
Chapter 5: MATLAB Graphics
produce a grid withspacing 0.1 in bothdirections. We have also used axis
square to force the same scale on both axes.
You can specify particular level sets by including an additional vector ar-
gument to contour. For example, to plot the circles of radii 1,


2, and

3,
type
>> contour(X, Y, X.ˆ2 + Y.ˆ2, [1 2 3])
The vector argument must contain at least two elements, so if you want
to plot a single level set, you must specify the same level twice. This is quite
useful for implicit plotting of a curve given by an equation in x and y.For
example, to plot the circle of radius 1 about the origin, type
>> contour(X, Y, X.ˆ2 + Y.ˆ2, [1 1])
Or to plot the lemniscate x
2
− y
2
= (x
2
+ y
2
)
2
, rewrite the equation as
(x
2
+ y
2
)
2
− x
2
+ y

2
= 0
and type
>> [X Y] = meshgrid(-1.1:0.01:1.1, -1.1:0.01:1.1);
>> contour(X, Y, (X.ˆ2 + Y.ˆ2).ˆ2 - X.ˆ2 + Y.ˆ2, [0 0])
>> axis square
>> title(’The lemniscate xˆ2-yˆ2=(xˆ2+yˆ2)ˆ2’)
The command title labels the plot with the indicated string. (In the default
string interpreter, ˆ is used for inserting an exponent and
is used for sub-
scripts.) The result is shown in Figure 5-3.
If you have the Symbolic Math Toolbox, contour plotting can also be
done withthe command ezcontour, and implicit plotting of a curve f (x, y)= 0
can also be done with ezplot. One can obtain almost the same picture as
Figure 5-2 withthe command
>> ezcontour(’xˆ2 + yˆ2’, [-3, 3], [-3, 3]); axis square
and almost the same picture as Figure 5-3 with the command
>> ezplot(’(xˆ2 + yˆ2)ˆ2 - xˆ2 + yˆ2’, ...
[-1.1, 1.1], [-1.1, 1.1]); axis square
Two-Dimensional Plots
71
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4

0.6
0.8
1
The lemniscate x
2
-y
2
=(x
2
+y
2
)
2
Figure 5-3
Field Plots
The MATLAB routine quiver is used to plot vector fields or arrays of arrows.
The arrows can be located at equally spaced points in the plane (if x and y
coordinates are not given explicitly), or they can be placed at specified loca-
tions. Sometimes some fiddling is required to scale the arrows so that they
don’t come out looking too big or too small. For this purpose, quiver takes an
optional scale factor argument. The following code, for example, plots a vector
field witha “saddle point,” corresponding to a combination of an attractive
force pointing toward the x axis and a repulsive force pointing away from the
y axis:
>> [x, y] = meshgrid(-1.1:.2:1.1, -1.1:.2:1.1);
>> quiver(x, -y); axis equal; axis off
The output is shown in Figure 5-4.
72
Chapter 5: MATLAB Graphics
Figure 5-4

Three-Dimensional Plots
MATLAB has several routines for producing three-dimensional plots.
Curves in Three-Dimensional Space
For plotting curves in 3-space, the basic command is plot3, and it works like
plot, except that it takes three vectors instead of two, one for the x coordi-
nates, one for the y coordinates, and one for the z coordinates. For example,
we can plot a helix (see Figure 5-5) with
>> T = -2:0.01:2;
>> plot3(cos(2*pi*T), sin(2*pi*T), T)
Again, if you have the Symbolic Math Toolbox, there is a shortcut
using ezplot3; you can instead plot the helix with
>> ezplot3(’cos(2*pi*t)’, ’sin(2*pi*t)’, ’t’, [-2, 2])
Three-Dimensional Plots
73
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1.5
-1
-0.5
0
0.5

1
1.5
2
Figure 5-5
Surfaces in Three-Dimensional Space
There are two basic commands for plotting surfaces in 3-space: mesh and
surf. The former produces a transparent “mesh” surface; the latter produces
an opaque shaded one. There are two different ways of using each command,
one for plotting surfaces in which the z coordinate is given as a function of x
and y, and one for parametric surfaces in which x, y, and z are all given as
functions of two other parameters. Let us illustrate the former with mesh and
the latter with surf.
To plot z = f (x, y), one begins witha meshgrid command as in the case of
contour. For example, the “saddle surface” z = x
2
− y
2
can be plotted with
>> [X,Y] = meshgrid(-2:.1:2, -2:.1:2);
>> Z = X.ˆ2 - Y.ˆ2;
>> mesh(X, Y, Z)
The result is shown in Figure 5-6, although it looks much better on the screen
since MATLAB shades the surface with a color scheme depending on the z
coordinate. We could have gotten an opaque surface instead by replacing mesh
with surf.
74
Chapter 5: MATLAB Graphics
-2
-1
0

1
2
-2
-1
0
1
2
-4
-3
-2
-1
0
1
2
3
4
Figure 5-6
With the Symbolic Math Toolbox, there is a shortcut command ezmesh,
and you can obtain a result very similar to Figure 5-6 with
>> ezmesh(’xˆ2 - yˆ2’, [-2, 2], [-2, 2])
If one wants to plot a surface that cannot be represented by an equation
of the form z = f (x, y), for example the sphere x
2
+ y
2
+ z
2
= 1, then it is bet-
ter to parameterize the surface using a suitable coordinate system, in this
case cylindrical or spherical coordinates. For example, we can take as param-

eters the vertical coordinate z and the polar coordinate θ in the x-y plane. If
r denotes the distance to the z axis, then the equation of the sphere becomes
r
2
+ z
2
= 1, or r =

1 − z
2
, and so x =

1 − z
2
cos θ , y =

1 − z
2
sin θ . Thus
we can produce our plot with
>> [theta, Z] = meshgrid((0:0.1:2)*pi, (-1:0.1:1));
>> X = sqrt(1 - Z.ˆ2).*cos(theta);
Special Effects
75
-1
-0.5
0
0.5
1
-1

-0.5
0
0.5
1
-1
-0.5
0
0.5
1
Figure 5-7
>> Y = sqrt(1 - Z.ˆ2).*sin(theta);
>> surf(X, Y, Z); axis square
The result is shown in Figure 5-7.
With the Symbolic Math Toolbox, parametric plotting of surfaces has
been greatly simplified withthe commands ezsurf and ezmesh, and you can
obtain a result very similar to Figure 5-7 with
>> ezsurf(’sqrt(1-sˆ2)*cos(t)’, ’sqrt(1-sˆ2)*sin(t)’, ...
’s’, [-1, 1, 0, 2*pi]); axis equal
Special Effects
So far we have only discussed graphics commands that produce or modify a
single static figure window. But MATLAB is also capable of combining several

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×