 |
In this section we will see some of the graphing capabilities of MATLAB
which go beyond simple graphs.
Please load the file graphics.m and run it
before going through this section.
Managing several windows
In the introductory class we learned how to graph more than one plot on
the same figure. Now what if we want to graph more than one plot on different
figures. The key command is figure. Here is a sequence of commands
that will illustrate this
plot(y1)
figure
plot(y2)
figure(1)
hold on
plot(y3)
figure(2)
plot(y4)
Notice how the hold command works for the current figure only. To
close one of the windows we use the close command. For example
close(1)
will close the first window, and
close all
will close all the windows.
Subplots
Subpolts are a compromise between different plots on one figure and different
plots on different figures. The subplot command treats one figure as an
array of subfigures. Namely, subplot(m,n,p) divides the current figure
into a mxn matrix and sets the current subplot to p. Example
close all
subplot(2,2,1)
plot(y1)
title('temp')
subplot(2,2,2)
bar(y2)
subplot(2,2,3)
plot(y3)
subplot(2,2,4)
plot(y4)
subplot(2,2,2)
title('histogram')
Contour plots and pcolor plots
Up to now we have plotted functions of one real variable. Functions of
two variables can be represented in a two dimensional throug a contour
(or level set) plot. Here is one nice example taken directly from the MATLAB
helpdesk.
[x,y,z] = peaks;
contour(x,y,z,20,'k')
hold on
pcolor(x,y,z)
shading interp
Mesh and surface plots
We now show how to plot functions of two variables as graphs in 3 dimensional
space. First we need to "prepare" two matrices X and Y (whose columns are
all equal):
[X,Y] = meshgrid(-pi:pi/20:pi);
Then we define the function of two variables
R = sqrt(X.^2+Y.^2)
Z = sin(R)*exp(-R.^2*1.5)+sqrt(25 - Y.^2)
and we finally perform the mesh plot
mesh(X,Y,Z)
Curves in 3 dimensional space
This is how we can plot a curve in the space (the logic is the same as
for curves in the plane).
z = 0:pi/200:2*pi;
r = exp(-z/3);
x = cos(z*10).*r;
y = sin(z*10).*r;
plot3(x,y,z);
Controlling the axes
Although MATLAB tries to optimize the scale on each axis, sometime it is
desirable to control directly these scales. For example, sticking to the
curve from the previous example enter the following
axis([-10 10 -10 10 -5 5])
axis equal
axis auto
axis off
axis on
grid on
grid off
Back to top of section.
Back to contents.
|