0% found this document useful (0 votes)
88 views8 pages

Experiment: # 3 Title: Equipment Required: Theory

This document provides instructions for creating two-dimensional plots in MATLAB using the plot and fplot commands. It discusses plotting single graphs and multiple graphs in the same figure window. Formatting options like titles, labels, and text annotations are also covered using commands like title, xlabel, ylabel, and text. Key aspects of the plot and fplot commands include specifying line styles, colors, markers, and axis limits. The hold on and hold off commands allow adding multiple graphs to a single figure.

Uploaded by

Ahad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views8 pages

Experiment: # 3 Title: Equipment Required: Theory

This document provides instructions for creating two-dimensional plots in MATLAB using the plot and fplot commands. It discusses plotting single graphs and multiple graphs in the same figure window. Formatting options like titles, labels, and text annotations are also covered using commands like title, xlabel, ylabel, and text. Key aspects of the plot and fplot commands include specifying line styles, colors, markers, and axis limits. The hold on and hold off commands allow adding multiple graphs to a single figure.

Uploaded by

Ahad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Digital Signal Processing Lab Manual

Faculty of Engineering
Experiment # 3
Title: Two Dimensional Plots.

Equipment Required: Personal computer (PC) with windows operating system


and MATLAB software
Theory:-
1) The plot Command:-

The plot command is used to create two-dimensional plots. The simplest form of
the command is:
plot(x,y)

Vector Vector

The arguments x and y are each are vector (one-dimensional array). Both vectors must
have the same number of elements.
The plot command has additional optional 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,’ line specifiers ' ,'PropertyName' ,PropertyValue)

(Optional) Properties with


Vector Vector (Optional) Specifiers
that define the type values that can be used to
and color of the line specify the line width, and
and markers. marker's size and edge, and
fill colors.
1.1) Line Specifiers:-
The line style specifiers are:
Line Style Specifier
solid (default) -
Dashed --
Dotted :
dash-dot -.
19

 The line color specifiers are:


Page
Digital Signal Processing Lab Manual

Line Color Specifier Line Color Specifier


red r magenta m
green g yellow y
blue b black k
cyan c white w
 The marker type specifiers are:
Marker Type Specifier Marker Type Specifier
plus sign + square s
circle o diamond d
asterisk * five-pointed star p
point . six-pointed star h
Notes about using the specifiers:
 The specifiers are typed inside the plotcommand as strings.
 Within the string the specifiers can be typed in any order.
 The specifiers are optional. This means that none, one, two, or all the three can
be included in a command.
LEGEND
PLOT TITLE light Intensity as a Function of Distance
1200

Theory
Experiment
1000

800
INTENSITY (lux)

Comparison between theory and experiment

Y AXIS LABEL 600 TEXT


LABEL

400
MARKER

200

0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
20

X AXIS LABEL
2) THE fplot COMMAND
Page
Digital Signal Processing Lab Manual

The fplot command plots a function with the form y = f(x) between specified limits. The
command has the form:

fplot('function' ,limits,line specifiers)

The function The domain of x, Specifiers that define


to be plotted. and optionally, the type and color of
the limits of the y the line and markers
axis. (optional).

2.1) function: The function can be typed directly as a string inside the command. For
example, if the function that is being plotted is f(x) = 8x2+ 5cos(x), it is (optional).
and markers typed as:
'8*x^2+5*cos(x) '.

2.2) limits: The limits is a vector with two elements that specify the domain of x [xmin,
xmax], or a vector with four elements that specifies the domain of x and the limits of the
y-axis [xmin, xmax, ymin, ymax].

3) Plotting Multiple Graphs in the Same Plot:-

In many situations there is a need to make several graphs in the same plot. There are
three methods to plot multiple graphs in one figure.
 By using the plot command
 By using the hold on, hold off commands
 By using the line command.
3.1) Using plot command:-
Two or more graphs can be created in the same plot by typing pairs of vectors
inside the plot command. The command:
plot(x,y,u,v,t,h)
creates three graphs: y vs. x, v vs. u, and h vs. t, all in the same plot. The vectors
of each pair must be of 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 the command:
plot(x,y,'-b',u,v,'--r',t,h,'g:' )

plots y vs. x with a solid blue line, v vs u with a dashed red line, and h vs. t with a
21

dotted green line.


Page
Digital Signal Processing Lab Manual

3.2) Using the hold on, hold off Commands:-

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 formatting if any was
done. Additional graphs can be added with plot commands that are typed next. Each
plot command creates a graph that is added to that figure. 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.

3.3) Using the line Command

With the line command additional graphs (lines) can be added to a plot that already
exists. The form of the line command is:

line(x,y, 'PropertyName' ,PropertyValue)

(Optional) Properties with values that can be


used to specify the line style, color, and width,
marker type, size, and edge and fill colors.

The format of the linecommand is almost the same as the plot command

4) Formatting a Plot Using Commands

The formatting commands are entered after the plot or the fplot commands. The
various formatting commands are:

4.1) The xlabel and ylabel commands:-

Labels can be placed next to the axes with the xlabel and ylabel commands which
have the form:

xlabel('text as string')

ylabel ('text as string')

4.2) The title command:-


A title can be added to the plot with the command:
22

title ('text as string')


Page

The text is placed at the top of the figure as a title.


Digital Signal Processing Lab Manual

4.3) 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. When
the command is executed, the Figure Window opens and the user specifies the Position
with the mouse.
4.4) 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 form of the command is:
legend('string1', 'string1', ………….. ,pos )
The strings are the labels that are placed next to the line sample

4.5) The axis command:-


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 and the appearance of the axes. In many
situations a graph looks better if the range of the axes extend beyond the range of the
data. The following are some of the possible forms of the axis command:

axis( [xmin, xmax] ) Sets the Iimits of the x axis (xmin and xmax are numbers).

axis([xmin,xmax,ymin,ymax]) Sets the limits of both the x and y axes.

5) Plotting multiple plots on the same page:-


Multiple plots on the same page can be created with the subplot command.
subplot(m,n,p)
The command divides the Figure Window (page when printed) into m x n
rectangular subplots where plots will be created. The subplots are arranged like
elements in a m X n matrix where each element is a subplot. The subplots are numbered
from 1 through m. n. The upper left is 1 and the lower right is the number m x n. The
23

numbers increase from left to right within a row, from the first row to the last. The
Page

command subplot(m, n, p) makes the subplot P current.


Digital Signal Processing Lab Manual

Procedure:-
Execute following example on MATLAB Command Window.

1. The plot Command:-


>> x = [1 2 3 5 7 7.5 8 10]
>> y = [2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)

The following table contains sales data of a company from 1988 to 1994.

YEAR 1988 1989 1990 1991 1992 1993 1994


SALES 8 12 20 22 18 24 27
(millions)

To plot this data, the list of years is assigned to one vector (named yr), and the
corresponding sale data is assigned to a second vector (named sle). The Command
Window where the vectors are created and the plot command is used is shown below:

>> yr = [1988:1:1994]
>> sle=[8 12 20 22 18 24 27]
>> plot(yr,sle,'--r*','linewidth',2,'markersize',12)

Line Specifiers: Property Name and Property Value:


dashed red line and the line width is 2 points and the
asterisk marker. markers size is 12 point.

2. THE fplot COMMAND:-

A plot of the function y = x2+ 4sin(2x) - 1 for -3 ≤ x ≤ 3 can be created with the fplot
command by typing:

>> fplot('x^2+4*sin(2*x)-1',[-3 3])

The plot of this function can be created with plot command as follows:

x = [-3:.1: 3]; Create vector x with the domain of the function.


24

y = x.^2+4.*sin(2.*x)-1;
Create vector y with the function value at each x.
Page

plot(x,y)
Plot y as a function of x.
Digital Signal Processing Lab Manual

3. Plotting Multiple Graphs in the Same Plot:-


Plot the function y = 3x3- 26x+ 10, and its first and second derivatives, for –2 ≤x≤4, all
in the same plot.
Solution
The first derivative of the function is: y' = 9x2- 26.
The second derivative of the function is: y" = 18x.
3.1 Using the plot command:-
A script file that creates a vector x, and calculates the values of y, y' , and y" is:
x =[-2:0.01:4]; Create vector x with the domain of the function.

y =3*x.^3 -26*x+ 6; Create vector y with the function value at each x.

yd = 9*x.^2-26; Create vector yd with values of the first derivative.

ydd =18*x; Create vector ydd with values of the second derivative.
plot(x,y,'-b',x,yd,'--r',x,ydd,':k')
Create three graphs, y vs. x, yd vs x and
ydd vs. x in the same figure.

3.2 Using the hold on, hold off Commands:-


x =[-2:0.01:4];
y =3*x.^3-26*x+6;
yd = 9*x.^2-26;
ydd =18*x;
plot(x,y,'-b') The first graph is created.
hold on
plot(x,yd,'--r') Two more graphs are added to the figure
plot(x,ydd,':k')
hold off
xd = [10:2:22];
yd = [950 640 460 340 250 180 140];
4. Formatting a Plot Using Commands
x = [10:0.1 :22];
y = 95000./x.^2;
25

xd =[10:2:22];
Page

yd =[950 640 460 340 250 180 140];


Digital Signal Processing Lab Manual

plot(x,y,'-','LineWidth', 1.0)
xlabel('DISTANCE (cm)')
ylabel('INTENSITY (lux)')
title('light Intensity as a Function of Distance')
axis([8 24 0 1200])
text(14,700,'Comparison between theory and experiment')
hold on
plot(xd,yd,'ro-- ','LineWidth',1.0,'markersize',10)
legend('Theory','Experiment',0);
hold off

Problems:-
Solve the following problems in MATLAB Command Window.

1) Make two separate plots of the function f ( x)  0.6 x  5 x  9 x  2


5 3

one plot for -4≤x≤4, and one for -2.7≤x≤2.7.

x  x  1 for -10 ≤ x ≤ 10
2

2) Plot the function f ( x) 


x  x 1
2

3) Use the fplot command to plot the


function: f ( x)  0.01x  0.03x  0.4 x  2 x  6 x  5
5 4 3 2

In the domain -4 ≤ x ≤ 6
4) Plot the following data in MATLAB
Year 1990 1991 1992 1993 1994 1995 1996
Minimum Temperature 10C 11C 9C 12C 8C 11C 6C
Maximum Temperature 45C 49C 46C 50C 48C 44C 47C
a) Label x Axis as year and y axis as temperature
b) The color of the Minimum temp graph should be blue and maximum temp
graph should be red.
26

c) The range of x Axis should be between 1990 to 1996 and y axis 4 to 50


d) The title of the graph should be “seven years temperature record”.
Page

You might also like