Next: 2.1.2 Log plots
Up: 2.1 Two-dimensional plots
Previous: 2.1 Two-dimensional plots
  Contents
Functions of a single variable, e.g., y(x), are normally plotted in a
conventional 2-D plot. Do the following to make a plot of the sine curve
between zero and
. (Remember, at this stage, you should really enter
the commands into an M-file and then execute (shortcut: hit F5) the M-file
rather than entering all the commands directly at the command line. You
don't need to enter any of the comments, which are marked by %.)
% Our first plot
x = linspace(0,2*pi,100); % get 100 evenly spaced values between 0 and 2 pi
y = sin(x); % Compute the sine at these points and store in variable y
plot(x,y) % plot the results
The linspace command creates 100 evenly
spaced points starting at zero and ending at
. This is similar to the
thing you tried in Sec. 1.4.1 (e.g., x=[0:2*pi/100:2*pi])
except that instead of choosing the step size explicitly, linspace
allows you to choose the number of points instead and makes MATLAB
figure out the step size. The second statement takes the array of 100
points and calculates the sine at each point and stores the results in the
array. The plot command then plots y vs. x.
(See Figure 2.1.) The plot is displayed in a separate window
called Figure No. 1.
Figure 2.1:
Simple plot of a sine curve.
|
|
You could have also used the plot command this way plot(x,sin(x))
without explicitly storing the function values in y. Try it.
By default, MATLAB plots the data by connecting the individual data
points. The plot command can be changed so that only the points are drawn
instead of lines. Try plot(x,y,'o') (the letter ``oh'' between
single quotes). Now try plot(x,y,'-*'). This tells MATLAB to
connect the points with a dotted line and mark each point with a
star. Try it. You can also change the color--try plot(x,y,'r-*'). A
list of all the style options that can be used with plot are summarized in
Table 2.1. You can also see them by typing
help plot. Experiment with three or four different plot styles of
your own design.
Table 2.1:
Style options for the plot command.
| Color-style options |
Marker-style
options |
Line-style options |
b |
blue |
. |
point |
- |
solid |
g |
green |
o |
circle |
: |
dotted |
r |
red |
x |
cross |
-. |
dashdot |
c |
cyan |
+ |
plus |
- |
dashed |
m |
magenta |
* |
asterisk |
|
|
y |
yellow |
s |
square |
|
|
k |
black |
d |
diamond |
|
|
| |
|
v |
triangle (down) |
|
|
| |
|
^ |
triangle (up) |
|
|
| |
|
< |
triangle (left) |
|
|
| |
|
> |
triangle (right) |
|
|
| |
|
p |
5-pointed star |
|
|
| |
|
h |
6-pointed star |
|
|
We can also add labels to the axes, control the plotting range, change the aspect
ratio of the plot, etc. For example, try the following:
% Making our first plot fancier
x = linspace(0,2*pi,100); % get 100 evenly spaced values between 0 and 2 pi
y = sin(x); % Compute the sine at these points and store in variable y
plot(x,y,'r-',x,y,'x') % plot the results with a red line and blue crosses
axis square % Make the plot square
xlabel('angle \theta') % Label the x axis
ylabel('sin(\theta)') % Label the y axis
title('Pretty plot of a sine curve') % Add a title label to the plot
axis([0 2*pi -1.1 1.1]) % Set the x and y ranges of the plot
Figure 2.2:
Fancier plot of a sine curve.
|
|
If it's unclear what any of the commands do, try looking them using the
help command.
Next: 2.1.2 Log plots
Up: 2.1 Two-dimensional plots
Previous: 2.1 Two-dimensional plots
  Contents
Gus Hart
2005-01-28