Class 5
Class 5
Plotting graphs is a very common tool for illustrating results in science. MATLAB has many
commands that can be used for creating various kinds of plots.
Easy plots
ezplot ('x^2+2')
In this case, if we assign a value 2 to a variable first. says y=2, when executing
y 2;
U ezplot ( ' x*2+y')
MATLAB will interpret y as a variable (not a value of 2). Hence, it will ereate'a contour plot of
the function
S2 +y =0
1
CLASS:5
diamond
five-pointed star P
sixed-pointed star h
triangle (pointed left)
triangle (pointed right)
05 1 2253 3
The specifiers are typed inside the plot command as
strings. Within the string, the specifiers can be typed in any order and the specifiers are optional.
For example:
=
0:5; y =
2.*x; plot (x, y, '-or')
This command creates a plot with solid red line and the marker is a circle.
Properties are optionaland can be usedto specify the thickness of the line thesize of the marker.
andthe colors ofthe marker's edgelineand fill. The Property Name istyped as a string,
followed by a comma and a value for the property, all inside the plot command.
creates a plot with a blue dashed line and squares as markers. The linewidth is 2 points and the
size of the square markers is 8 points. The marker has green filling
Formatting a plot
commands. or
Plots can be formatted by using MATLAB command that follow the plot or fplot
the the first
interaciively by using the plot editor in the Figure Window. Here we'will éxplain time the
method which is more usefu! since a formatted plot can be created automatically every
program is executed.
>>xlabel('text as string') Fle Edit Vew Insert Toos Desktoo Vindow Hep
and:
0.6
5
CLASS.S
T h e legend command The legend command places a legend on the plot. The legend shows a
sample of the line type of each graph that is plotted, and places a label, specified by the user.
beside the line sampie. The usage is:
>>
legend ('string 1', 'string 2',...)
Fie Eew iet To
>t
1inspace (0, 10, 100); s =
t.*2/2
>
plot (t, s);
xlabel('Time (sec) ')
ylabel ('distance (m) ');
title ('Distance as a functi on of t')
legend("distá.cc')
ereates a plot shown on the right.
Formatting the texts
The texts in the xlabel, ylabel, title, text and Time is
legend comnands can be formatted to customize
the font, size, style (italic, bold, etc.), and color.
Formatting
can be done by adding
optional
PropertyName and Property Va!ue arguments following the string inside the command. For
example:
t e x t (x, y, 'text as string', ' PropertyNane', Pr opertyvalue)
Some formatting can also be done by adding modifiers inside the string. For example. adding lbf.
lit, or Im,will create a text in bold font. italicstyle, andwithnormal font, rexpectively. A single
character can be displayed as a subscript or a superscript by typing_(theunderscore character)
or in front ofthe character, respectively. A long superscript or subscript can be typed inside{}
following theor the ^. Forexample:
When the plot(x,y) command is executed, MATLAB creates axes with limits that are based on
the minimum and maximum values of the elements of x and y. The axis command can be used to
change the range of the axes. Here are some possible forms of the axis command:
Commands Description
axis([xmin, xmax, ymin, ymax]) sets the limits of both x and y
axis equal sets the same scale for both axes
axis square sets the axes region to be square
axis tight sets the axis limits to the range of the data
grid on adds grid lines tothe plot. grid off He Et rew nsert oois ird eC
removes grid lines from the plot.
For example:
to plot multiple graphs in one figure. One is by using the plot command, the other is by using the
hold on, hold off commands.
Two or more graphs can be created in the same plot by typing pairs of vectors inside the pBot
command. For example:
creates 3 graphs: y vs x, V vs u, and t vs s, all in the same plot. The vector of each pair must have
the same length. MATLAB automatically plots the graphs in different colors so that they can be
identified. It is also possible toadd line specifiers following each pair. For example:
plots y vs x with a solid blue line and circles, v vs u with a dashed red lines with cross signs. t vs
swith a dotied green line and square markers.
For example, we plot the function y=3a° -20r + 10, and its first derivative
y= 9r - 26and second derivatives = 187, for - 2 4 , all in the samt plot. A
=
linspace (-2, 4, 20);
3*x.^3-26*x+10;
>> dy = 9*x.^2-26;
>ddy = 18*x;
>ddy = 18*x;
plot (x, y, x, dy, x, ddy) ;
legend ( "y', 'first derivative', 'second derivative');
The defauit colors of multiple graphs in MATLAB start from blue, red, and green, respectively.
20
8
CLASS:5
process. It returns MATLAB to the default mode in which the plot command erases the previous
p l o t and resets the axis properties.
Ea Jset Tods D s t Whoo Hep
qLogarithmic Scales
The semilogy function plots x data on linear axes and y data on logarith1nic axes. The semilogx
function plots x data on logarithmic axes and y data on linear axes. The loglog function plots
both x and y data on logarithmic axes.
30
10
Polar plots 120
240 J00
9
CLASSS
the plot command. To plot a function Oina certain domain, vector for values of Gis a
created first, and then a vector rwith the corresponding values of J (0)is created using
wise calculation. element
For example, a plot of the function
Pi, 1o)
r =
3 cos(0/2) +0. for
theto Itnspace (o,
00 2T
is done by:
T9x cos (theta/2),"2 +helo-
>pola (AkeBo, , - 0 )
>thetalinspace (0, 2*pi, 200)
>>r 3'cos(theta/2). *2+theta;
>>polar(theta, r)
>>
subplot (m, n,p)
0Fgure1
Elle Eit Ylew Insert Iools Qesktop Window Help
The command divides the Figure
Wirndow into mxn rectangular
Edlt Plot subplots where plots will be created.
sin) exp- The subplots are arranged like
elements in a mxn matrix where
200
each element is a subplot. The
subplots are numbered from
100
through mn. The number increases
from left to right within a row, from
the first row to the last. The
5 642 0 24
X
command subplot(m.n.p) makes the
subplot p current. This means that
sin/ the next plot command will create a
40
plot jn this sybpl 2 )
20
0.5
e(o,
plot , y )
>aitle C'sina)); subplot 3,3,5)
>Subplo (3,3,2)5 y1dafx);
=coS M);
Plot ,yD
Forexample, we create a plot that has 2 rows and 2 columns:2P10 +ie ('t on))
l e eo 9')
>subplot (2,2, 1);ezplot ( 'sin (x) ");
subplot (2,2,2);ezplot ('exp(-x) ")
(5,5,1); fplot ('sin(t), to, 2*pi3);
sub plot
10 Pij); to,2 *
creates a
plot of y=x, with figure window number as 10. Then
gude )
pot (
close(10) Atgue()
command closes the figure window. >fplot (x^);
Stguoers)
Pplot C'xs');
fig uoe C6)
fplot x°6');
EXERCISE-CLASS 5