Plotting in Matlab

advertisement
Plotting in Matlab
The main function is plot (x, y) that plots vector y versus vector x.
Example:
x=-3*pi:pi/20:3*pi;
y=x.*sin(x);
plot(x,y);
plot(x,y) causes Matlab to open a Figure Window and display the plot in that window:
Various line types, plot symbols and colors may be obtained with plot (x, y, s) where s is
a character string made from one element from any or all the following 3 columns:
b
g
r
c
m
y
k
Color
blue
green
red
cyan
magenta
yellow
black
Marker Style
Line Style
.
point
solid
o
circle
:
dotted
x
x-mark
-.
dashdot
+
plus
-dashed
*
star
s
square
d
diamond
v
triangle (down)
^
triangle (up)
<
triangle (left)
>
triangle (right)
p
pentagram
h
hexagram
As seen from the first figure, the default color is blue (for a single graph), the default
marker style is no marker, and the default line style is no line!
For instance, here follows the plot when the command plot (x, y, 'rs:'); is executed:
You can either use the print command or the File menu, export command in the figure
window in order to convert (save as) the plot to a graphical image. For instance, here
follows the tif format of the first plot:
Titles, axis labels, and legend(for figures containing more than one plot) can be added to
a plot using title, xlabel, ylabel, and legend commands. The command grid on/off can be
used to turn on/off the grid lines. For instance, lets issue the following commands:
yy=sin(x)+x.*cos(x);
plot(x,y,x,yy);
legend('xsin(x)','sin(x)+xcos(x)',-1);
title('Lab Assignment 4 Question 1');
xlabel('x')
grid on
Then, we end up with the following plot:
A new figure window will be opened with the command figure (a) where a corresponds
to the figure number. We can also use the subplot (m, n, p) command to plot multiple
plots separately on a m x n table figure area on the same figure. The axis command can
be used to set the x and y limits of the plot.
Example:
The following commands will yield the next figure:
>> figure(3)
>> subplot(1,2,1)
>> plot(x,y)
>> title y=xsin(x)
>> miny=min(y)
miny =
-4.8095
>> maxy=max(y)
maxy =
7.9124
>> xmin=min(x)
xmin =
-9.4248
>> xmax=max(x)
xmax =
9.4248
>> axis ( [xmin xmax miny maxy]);
>> subplot(1,2,2)
>> maxyy=max(yy)
maxyy =
9.4248
>> minyy=min(yy)
minyy =
-9.4248
>> plot(x,yy,'r+-');
>> axis ( [xmin xmax minyy maxyy]);
>> title yy=sin(x)+xcos(x)
>> grid on
Note that one can also use the hold on/off command to plot multiple graphs on the same
axes. Thus, we could execute plot (x, y); hold on; plot (x, yy, ’g’) instead of
plot (x, y, x, yy). (Compare the result to first figure).
The semilogx, semilogy, and loglog functions can be used to plot x data, y data, both data
on logarithmic axes, respectively.
It is possible to enhance the properties of a line by specifying the Width,
MarkerEdgeColor, MarkerFaceColor, and MarkerSize properties.
Example:
plot(x,y,'o-','LineWidth',2.0,'MarkerEdgeColor','r', …
'MarkerFaceColor','g','MarkerSize',6.0);
generates the following figure:
The text strings 8titles, axis labels, etc.) can be enhanced by the use of stream modifiers.
Common modifiers are:
\bf
Bold face
\it
Italics
\rm
Restore normal font
\fontname{fontname}
Specify font name
\fontsize {fontsize}
Specify font size
_{something}
something is typed as subscript
^{something}
something is typed as superscript
Moreover, it is possible to use some Greek and Mathematical symbols by embedding
escape sequences.
Example:
>> r=1:0.1:3;
>> area=pi.*r.^2;
>> plot(r,area);
>> title \itarea\rm=\fontname{tahoma}\fontsize{14}\pir^{2}
generates the following figure (see title):
You can also use the polar (theta, r) command to display in polar coordinates. For
instance, the following commands generate the next figure:
>> theta=0:pi/10:pi;
>> rho=1:0.1:2;
>> rr=1.5*ones(1,11)
rr =
Columns 1 through 4
1.5000 1.5000 1.5000 1.5000
Columns 5 through 8
1.5000 1.5000 1.5000 1.5000
Columns 9 through 11
1.5000 1.5000 1.5000
>> polar(theta,rho)
>> hold on
>> polar(theta,rr,'r')
Download