Class:5 – Plotting
Graphs
______________________________________________________
Easy Plots
i. ezplot(f)
ezplot(f) plots a symbolic expression, equation, or function f. By default, ezplot plots a univariate
expression or function over the range [–2π, 2π] or over a subinterval of this range. If f is an equation or
function of two variables, the default range for both variables is [–2π, 2π] or over a subinterval of this
range.
Example:
• The function definition involves some constant parameter, write it explicitly:
>> ezplot('x^3+1')
In this case, if we assign a value 2 to a variable first, says y=1, when executing:
>> y = 1;
>> ezplot('x^3+y')
MATLAB will interpret y as a variable (not a value of 1). Hence, it will create a contour plot
of the function
𝑥 3 + 𝑦 = 0.
• The following example plots the implicitly defined function, x2 - y4 = 0 over
the domain [-2 , 2 ]:
>> ezplot('x^2-y^4')
ii. ezplot(f,[min,max])
ezplot(f,[min,max]) plots f over the specified range. If f is a univariate expression or function,
then [min,max] specifies the range for that variable. This is the range along the abscissa (horizontal
axis). If f is an equation or function of two variables, then [min,max] specifies the range for both
variables, that is the ranges along both the abscissa and the ordinate.
Example:
The simplest buit-in function for plotting an explicit function is ezplot command. For
example, we want to plot a parabola on the interval [-1,1]
𝑓(𝑥) = 2𝑥 2 , −1 ≤ 𝑥 ≤ 1
We can simply use the following command:
>> ezplot('2*x^2',[-1,1])
The first input of ezplot command is a string describing the function. The second input
(which is optional) is the interval of interest in the graph. Note that the function
description is not necessarily a function in x variable. We can use any variable we like:
Plot sin(x) over [−π/2, π/2] by specifying the plotting interval as the second input to ezplot.
>>ezplot(‘sin(x)’,[-pi/2,pi/2])
Another related command is fplot. It is used to plot a function with the form 𝑦 =
𝑓(𝑥) between specified limits.
For example
>>fplot('2*x^2',[-1,1])
iii. ezplot(f,[xmin,xmax,ymin,ymax])
ezplot(f,[xmin,xmax,ymin,ymax]) plots f over the specified ranges along the abscissa and the
ordinate.
Example:
>> ezplot(‘sin(x)/(1+x^2)’, [-4, 4,-0.5,0.5])
Parametric function
Example:
𝑥 = 𝑠𝑖𝑛(𝑡)
𝑦 = 𝑐𝑜𝑠(𝑡)
𝑥 2 + 𝑦 2 = 𝑠𝑖𝑛2 𝑡 + 𝑐𝑜𝑠 2 𝑡 = 1
iv. ezplot(x,y)
ezplot(x,y) plots the parametrically defined planar curve x = x(t) and y = y(t) over the default
range 0 <= t <= 2π or over a subinterval of this range.
Example:
>> ezplot(‘sin(t)‘,’cos(t)’)
Or
>> 𝑥 = 𝑠𝑖𝑛(𝑡);
>> 𝑦 = 𝑐𝑜𝑠(𝑡);
>> 𝑒𝑧𝑝𝑙𝑜𝑡(𝑥, 𝑦)
v. ezplot(x,y,[tmin,tmax])
ezplot(x,y,[tmin,tmax]) plots x = x(t) and y = y(t) over the specified range tmin <= t <= tmax.
Example:
>>ezplot('sin(3*t)*cos(t)','sin(3*t)*sin(t)',[0,pi])
Plot
The plot command is used to create a two-dimensional plot. The simplest form of the
command is:
plot(x,y)
where x and y are each a vector. Both vector must have
the same number of elements.
For example:
>> x = 0:0.1:5;
>> y = exp(-x);
>> plot(x,y)
Once the plot command is executed, the figure Window
opens and the plot is displayed
The plot appears on the screen in blue which is the
default line color.
The plot command has additional arguments that can be used to specify the color and style
of the line and the color and type of markers, if any are desired. With these options the
command has the form:
plot(x,y,'linespec','PropertyName',PropertyValue)
Line Specifiers (linespec)
Line specifiers are optional and can be used to define the style and color of the line and the
type of markers (if markers are desired).
The line style specifiers are: The line color specifiers are:
Line Style Specifier
solid (default) - Line Color Specifier
dashed -- red r
dotted : green g
dash-dot -. blue b
cyan c
The marker type specifiers are: magenta m
Marker Type Specifier yellow y
plus sign + black k
circle o white w
asterisk *
point .
cross x
triangle (pointed up) ^
triangle (pointed down) v
square s
diamond d
five-pointed star p
sixed-pointed star h
triangle (pointed left) <
triangle (pointed right) >
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:
>> x = 0:5; y = 2.^x; plot(x,y,'-or')
This command creates a plot with solid red line and the marker is a circle.
Property Name and Property Value
Properties are optional and can be used to specify the thickness of the line, the size of the
marker, and the colors of the marker’s edge line and fill. The Property Name is typed as a
string, followed by a comma and a value for the property, all inside the plot command.
Property Name Description
linewidth the width of the line (default 0.5, possible values are 1,2,3,...)
markersize the size of the marker (e.g., 5,6,... )
markeredgecolor the color of the edge line for filled marker (e.g., r, b)
markerfacecolor the color of the filling for filled markers (e.g, r, b)
For example, the command:
>>x = linspace(0,pi,50); y = exp(-x).*sin(8*x);
>>plot(x,y,'--sb','linewidth',2,'markersize',8,'markerfacecolor','g')
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
Plots can be formatted by using MATLAB command that
follow the plot or fplot commands, or interactively by
using the plot editor in the Figure Window. Here we will
explain the the first method which is more useful since a
formatted plot can be created automatically every time the program is executed.
The xlabel and ylabel command
xlabel and ylabel create description on the x- and y-axes, respectively after we have
plotted a graph. The usages are:
>> xlabel('text as string')
and:
>> ylabel('text as string')
The title command
A title can be added to the plot with the command:
>> title('text as string')
The text will appear on the top of the figure as a title.
The text command
A text label can be placed in the plot with the text or gtext commands:
>> text(x,y,'text as string')
>> gtext('text as string')
The text command places the text in the figure such that the first character is positioned at
the point with the coordinates x,y (according to the axes of the figure). The gtext command
places the text at a position specified by the user (with the mouse).
The 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
sample. The usage is:
>> legend('string 1','string 2',... )
For example, the command:
>> t = linspace(0,10,100); s = t.^2/2;
>> plot(t,s);
>> xlabel('Time (sec)');
>> ylabel('distance (m)');
>> title('Distance as a function of t');
>> legend('distance')
creates a plot shown on the right.
Formatting the texts
The texts in the xlabel, ylabel, title, text and legend commands can be formatted
to customize the font, size, style (italic, bold, etc.), and color. Formatting can be done by
adding optional PropertyName and PropertyValue arguments following the string inside
the command. For example:
>> text(x,y,'text as string','PropertyName',PropertyValue)
Some of the PropertyName are:
Property Name Description
Rotation the orientation of the text (in degree)
FontSize the size of the font (in points)
FontWeight the weight of the characters (light, normal, bold)
Color the color of the text (e.g., r, b, etc.)
Some formatting can also be done by adding modifiers inside the string. For example,
adding \bf, \it, or \rm, will create a text in bold font, italic style, and with normal font,
rexpectively. A single character can be displayed as a subscript or a superscript by
typing _ (the underscore character) or ^ in front of the character, respectively. A long
superscript or subscript can be typed inside { } following the _ or the ^. For example:
>> title('\bf The values of X_{ij}', 'FontSize',18)
Greek characters
Character Letter
Greek characters can be included in the text by typing \ (back slash) \alpha
before the name of the letter.
\beta
\gamma
The axis command
\theta
When the plot(x,y) command is executed, MATLAB creates axes \pi
with limits that are based on the minimum and maximum values of the \sigma
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
The grid command
grid on adds grid lines to the plot. grid off removes grid lines from the plot.
For example:
>> fplot('x^2+4*sin(2*x)-1',[-3 3])
>> grid on
>> axis([-2 2 0 5])
Plotting multiple graphs
In may situations there is a need to make several graphs in the same plot. There are two
methods 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.
1. Using the plot command
Two or more graphs can be created in the same plot by typing pairs of vectors inside
the plot command. For example:
>> plot(x,y,u,v,s,t)
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 to add line specifiers following each pair.
For example:
>> plot(x,y,'-bo',u,v,'--rx',s,t,'g:s')
plots y vs x with a solid blue line and circles, v vs u with a dashed red lines with cross
signs, t vs s with a dotted green line and square markers.
For example, we plot the function , and its first
derivative and second derivatives , for , all in
the same plot. A script file that creates these graphs
can be written as:
>> x = linspace(-2,4,20);
>> y = 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 default colors of multiple graphs in MATLAB start
from blue, red, and green, respectively.
2. Using the hold on, hold off command
To plot several graphs using the hold on, hold off commands, one graph is plotted first
with the plot command. Then the hold on command is typed. This keeps the Figure
Window with the first plot open, including the axis properties and the formatting.
Additional graphs can be added with plot commands that are typed next.
The hold off command stops this process. It returns MATLAB to the default mode in
which the plot command erases the previous plot and resets the axis properties.
With the data from the previous example, we can plot y and its derivatives by typing
commands shown in the script:
>> plot(x,y,'-b');
>> hold on
>> plot(x,dy,'--r');
>> plot(x,ddy,':k');
>> hold off
Logarithmic Scales
The semilogy function plots x data on linear axes and y data on logarithmic 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.
For example, notice the difference between the two commands:
>> x = logspace(0,3,100);
>> y = x.^2;
>> semilogy(x,y)
>> loglog(x,y)
(The logspace creates a vector of 100 elements in log scale with the first
element 10^0 and the last element 10^(3).)
Polar plots
The polar command is used to plot functions in polar coordinates. The command has the
form:
>> polor(theta,radius,'linespec')
where theta and radius are vectors whose elements define
the coordinates of the points to be plotted. The line
specifiers are the same as in the plot command. To plot a
function in a certain domain, a vector for values
of is created first, and then a vector with the
corresponding values of is created using element-
wise calculation.
For example, a plot of the function
is done by:
>> theta = linspace(0,2*pi,200);
>> r = 3*cos(theta/2).^2+theta;
>> polar(theta,r)
Multiple plots on the same window
Multiple plots on the same page can be created with the subplot command, which has
the form:
>> subplot(m,n,p)
The command divides the Figure Window
into mxn rectangular subplots where plots
will be created. The subplots are
arranged like elements in a mxn matrix
where each element is a subplot. The
subplots are numbered from 1
through mn. The number increases from
left to right within a row, from the first
row to the last. The
command subplot(m,n,p) makes the
subplot p current. This means that the
next plot command will create a plot in
this subplot.
For example, we create a plot that has 2 rows and 2 columns:
>> subplot(2,2,1);ezplot('sin(x)');
>> subplot(2,2,2);ezplot('exp(-x)');
>> subplot(2,2,3);ezplot('x^2');
>> subplot(2,2,4);ezplot('sin(x)/x');
Multiple Figure Windows
When the plot or any other commands that generates a plot is executed, the Figure
Window opens and displays the plot. If a Figure Window is already open and
the plot command is executed, a new plot will replace the existing plot. It is also possible
to open additional Figure Window. This is done by typing the command figure. Every
time the command figure is entered, MATLAB opens a new Figure Window and after than
we can enter the plot command to generate a plot in the last active Figure Window.
The figure command can also have an input argument that is an integer figure(n). The
number corresponds to the number of a corresponding Figure Window. Figure Windows
can be closed with the close command. Several forms of the command are:
• close closes the active Figure Window
• close(n) closes the nth Figure Window
• close all closes all Figure Windows that are open
For example
𝑓𝑖𝑔𝑢𝑟𝑒(10);
𝑥 = 0: 0.01: 5;
𝑦 = 𝑥 ∗ 𝑥;
𝑝𝑙𝑜𝑡(𝑥, 𝑦)
EXERCISE-CLASS-5
1. Plot the graph of 𝑦 = 𝑠𝑖𝑛𝑥, by taking 𝑥 = [0,2π] with step length π/20.
2. Plot the graph of 𝑦 = 𝑠𝑖𝑛𝑥, by taking 𝑥 = [0,2𝜋] with step length 𝜋/10.
3. Plot the graph of 𝑦 = 𝑠𝑖𝑛𝑥, by taking 𝑥 = [0, 𝜋] with step length 𝜋/20.
4. Plot the graph of 𝑦 = 𝑠𝑖𝑛𝑥, by taking 𝑥 = [0, 𝜋] with step length 𝜋/10.
5. Plot the graph of 𝑦 = 𝑐𝑜𝑠𝑥, by taking 𝑥 = [0,2𝜋] with step length 𝜋/20.
6. Plot the graph of 𝑦 = 𝑐𝑜𝑠𝑥, by taking 𝑥 = [0,2𝜋] with step length 𝜋/10.
7. Plot the graph of 𝑦 = 𝑐𝑜𝑠𝑥, by taking 𝑥 = [0, 𝜋] with step length 𝜋/20.
8. Plot the graph of 𝑦 = 𝑐𝑜𝑠𝑥, by taking 𝑥 = [0, 𝜋] with step length 𝜋/10.
9. Plot the graph of 𝑦 = 𝑐𝑜𝑠2𝑥, by taking 𝑥 = [0,2𝜋] with step length 𝜋/5.
10. Plot the graph of 𝑦 = 𝑠𝑖𝑛(𝑥/2), by taking 𝑥 = [0,2𝜋] with step length 𝜋/5.
11. Plot the graph of 𝑦 = 𝑠𝑖𝑛2𝑥, by taking 𝑥 = [0,2𝜋] with step length 𝜋/5.
12. Plot the graph of 𝑦 = 𝑐𝑜𝑠(𝑥/2), by taking 𝑥 = [0,2𝜋] with step length 𝜋/5.
13. Plot the graph of 𝑦 = 𝑠𝑖𝑛𝑥, by taking 𝑥 = [0,2𝜋] with step length 𝜋/10, by
making use of the commands like line specifier, line color, line maker.
14. Plot the graph of 𝑦 = 𝑠𝑖𝑛2𝑥, by taking 𝑥 = [0,2𝜋] with step length 𝜋/10, by
making use of the commands like line specifier, line color, line maker.
15. Plot the graph of 𝑦 = 𝑐𝑜𝑠𝑥, by taking 𝑥 = [0,2𝜋] with step length 𝜋/10, by
making use of the commands like line specifier, line color, line maker.
16. Plot the graph of 𝑦 = 𝑐𝑜𝑠(2𝑥/3), by taking 𝑥 = [0,2𝜋] with step length 𝜋/10,
by making use of the commands like line specifier, line color, line maker.
17. Plot the graph of 𝑦 = 𝑒 𝑥 𝑠𝑖𝑛𝑥, by taking 𝑥 = [−2,2] and making use of the
commands like fplot, title, x label, y label, line color, line marker, line
width.
18. Plot the graph of 𝑦 = 𝑐𝑜𝑠𝑥. 𝑠𝑖𝑛𝑥, by taking 𝑥 = [−1,1] and making use of the
commands like fplot, title, x label, y label, line color, line marker, line
width.
19. Plot the graph of 𝑦 = 𝑒 3𝑥 𝑐𝑜𝑠𝑥 2 , by taking 𝑥 = [−3,3] and making use of the
commands like fplot, title, x label, y label, line color, line marker, line
width.
20. Create the graphs of 𝑥, 𝑥 + 1, 2𝑥, 2𝑥 + 1 in a single plot by making use of
the commands like title, x label, y label, line color, line marker, line width.
21. Create the graphs of 𝑥, 𝑥 + 1, 2𝑥, 2𝑥 + 1 in a single plot by making use of
the commands like title, x label, y label, line color, line marker, line width.
22. Create the graphs of 𝑥, 𝑥 2 + 1, 2𝑥 3 , 2𝑥 + 1 in a single plot by making use of
the commands like title, x label, y label, line color, line marker, line width.
23. Create the graphs of 𝑠𝑖𝑛𝑥, 𝑐𝑜𝑠𝑥 𝑖𝑛 a single plot by making use of the
commands like title, x label, y label, line color, line marker, line width.
24. Plot the graph of 𝑦 = 𝑠𝑖𝑛(𝑥/2), by taking 𝑥 = [−𝜋, 𝜋] with step length 𝜋/5, by
making use of the commands like line specifier, line color, line marker.
25. Plot the graph of 𝑦 = 𝑐𝑜𝑠2𝑥, by taking 𝑥 = [−𝜋, 𝜋] with step length 𝜋/5, by
making use of the commands like line specifier, line color, line marker.
26. Plot the graph of 𝑦 = 𝑐𝑜𝑠𝑥. 𝑠𝑖𝑛𝑥, by taking 𝑥 = [−𝜋, 𝜋] with step length 𝜋/5,
by making use of the commands like line specifier, line color, line marker.
27. Plot the polar graph of 𝑦 = 𝑒 𝑥 𝑠𝑖𝑛2𝑥, by taking 𝑥 = [−2𝜋, 2π] with step
length 𝜋/10, by making use of the commands like line specifier, line color,
line marker.
28. Plot the polar graph of 𝑦 = 𝑒 2𝑥 𝑐𝑜𝑠2𝑥, by taking 𝑥 = [−2𝜋, 2𝜋] with step
length 𝜋/10, by making use of the commands like line specifier, line color,
line marker.
29. Plot the polar graph of 𝑦 = 𝑥 2 𝑠𝑖𝑛2𝑥, by taking 𝑥 = [−2𝜋, 2𝜋] with step
length 𝜋/10, by making use of the commands like line specifier, line color,
line marker.
30. Create the graphs of 𝑥, 𝑥 + 1, 2𝑥, 2𝑥 + 1 in multiple plots in a single window
by making use of the commands like title, x label, y label, line color, line
marker, line width.
31. Create the graphs of 𝑠𝑖𝑛𝑥, 𝑠𝑖𝑛2𝑥, 𝑐𝑜𝑠𝑥, 𝑐𝑜𝑠2𝑥 in multiple plots in a single
window by making use of the commands like title, x label, y label, line
color, line marker, line width.
32. Create the graphs of 𝑥, 𝑥 + 10, 𝑥 2 , 2𝑥 + 3 in multiple plots in a single window
by making use of the commands like title, x label, y label, line color, line
marker, line width.