You are on page 1of 11

SIGNAL AND SYSTEMS LAB 02

Pak-Austria Fachhochschule Institute of


Applied Sciences and Technology, Haripur, Pakistan

DEPARTMENT OF ELECTRICAL ENGINEERING


COURSE: SIGNAL AND SYSTEMS
COURSE CODE: ECE-251
Lab Report
LAB 02
Submitted by:
Name: Muhammad Usman
Reg# B20F0447EE031

Submitted to:
Name: Rafi Ullah
Designation: LAB Engineer

Date:
FEBRUARY 23, 2022
SIGNAL AND SYSTEMS LAB 02

Plotting of Sinusoids using MATLAB


OBJECTIVE:
Objective of this lab is to implement plotting graphs of sinoidal waves using function s in matlab.

SOFTWARE USED:
Matlab software

INTRODUCTION:
In this lab we will learn about plotting of graphs in MATLAB. With exception to it we will about
to know the different commands that include naming of the axis of graphs, characteristics of
graph members, multiple graphs in same window.

MATLAB:
MATLAB is a proprietary multi-paradigm programming language and numeric computing
environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of
functions and data, implementation of algorithms, creation of user interfaces, and interfacing
with programs written in other languages.

PLOTTING:
One of the most important functions in MATLAB is the plot function. The plot command also
happens to be one of the easiest functions to learn how to use. The basic syntax of the function
call is shown below. This code can be entered in the MATLAB command window or run from
an m-file.
Representation:
plot(x,y)

This command will plot the elements of vector y (on the vertical axis of a figure) versus the
elements of the vector x.

PLOT AESTHETICS:
The color, point marker, and line style can be changed on a plot by adding a third parameter (in
single quotes) to the plot command. For example, to plot the above function as a red, dotted
line, change the m-file will to generate the figure.
The third input consists of one to three characters which specify a color, point marker type,
and/or line style. The list of colors, point markers, and line styles are summarized below.

y yellow . point - solid

m magenta o circle : dotted


SIGNAL AND SYSTEMS LAB 02

c cyan x x-mark -. dashdot

r red + plus -- dashed

g green * star

b blue s square

w white d diamond

k black v triangle (down)

^ triangle (up)

< triangle (left)

> triangle (right)

p pentagram

h hexagram

SUBPLOTTING:
More than one plot can be put in the same figure on its own set of axes using
the subplot command. The subplot command allows you to separate the figure into as many
plots as desired, and put them all in one figure. To use this command, the following line of code
is entered into the MATLAB command window or run from an m-file.
Representation:

subplot(m,n,p)
This command splits the figure into a matrix of m rows and n columns, thereby
creating m*n plots on one figure.

CHANGING THE AXES:


Now that you have found different ways to plot functions, you can customize your plots. One of
the most important ways to do this is with the axis command. The axis command changes the
axis of the plot shown, so only the part of the axis that is desirable is displayed. The axis command
is used by entering the following code right after the plot command (or any command that has a
plot as an output).
Representation:
axis([xmin, xmax, ymin, ymax])
SIGNAL AND SYSTEMS LAB 02

ADDING TEXT:
Another thing that may be important for your plots is labeling. You can give your plot a title (with
the title command),
x-axis label (with the xlabel command), y-axis label (with the ylabel command), and put text on
the actual plot.
A title will be placed, centered, above the plot with the command: title('title string'). The x-axis
label is issued with the command xlabel('x-axis string'), while the y-axis label is issued with the
command ylabel('y-axis string').
Furthermore, text can be put on the plot itself in one of two ways: the text command and
the gtext command. The first command involves knowing the coordinates of where you want the
text string. The syntax of the command is text(xcor,ycor,'textstring').

LEGEND:
To use the other command, you do not need to know the exact coordinates. The syntax
is gtext('textstring') which provides a set of cross-hairs that you can move to the desired location
with your mouse and click on the position where you want the text to be placed.
Finally, a legend can be added to the plot to identify various curves. The command
is legend('string1','string2','string3'). You can specify as many text strings as there are curves in
the plot.

LOCATION:
The default location of the legend is in the top-right corner, but this can be modified with a
parameter called Location.

SUBPLOTTING:
Sub-plotting is a very powerful feature in MATLAB. They allow users to very quickly create
customized data visualizations and displays. The subplot() function in MATLAB/Octave allows you
to insert multiple plots on a grid within a single figure. The basic form of the subplot() command
takes in three inputs: nRows, nCols, linearIndex.

GRIDS:
Grid on displays the major grid lines for the current axes.
SIGNAL AND SYSTEMS LAB 02

Task :

Draw graphs of the functions


i.y = sin(x)/x
ii. u= (1/(x-1)2 )+x

iii. v= (x2+1)/(x2 -4)


iv. ((10-x)1/3 -1)/(4 - x 2 ) 1/2

ANSWER

CODE:
x = 0:0.1:10;
y = sin(x)./x;
subplot (221), plot (x, y), title (’(i)’)

u = 1./(x-1).^2 + x;
subplot (222), plot (x, u), title(’(ii)’)

v = (x.^2+1)./(x.^2 -4);
subplot(223), plot(x, v), title(’(iii)’)
w = ((10-x).^(1/3)- 1)./sqrt(4-x.^2);

subplot (224), plot (x, w), title(’(iv)’)

GRAPH AND OUTPUT:


SIGNAL AND SYSTEMS LAB 02

Lab Task 01:

Question#1:
Draw graphs of the functions y = cos(x) z = x for 0 ≤ x ≤ 2 in the same window. Use the zoom
facility to determine the point of intersection of the two curves (and, hence, the root of x = cos(x))
to two significant figures.

ANSWER:

CODE:
N=10;
d=1/N;
x= 0:d:2;
y=cos(x);
z=x;
plot(x,y,x,z);
legend('Cos x', 'x');
grid

OUTPUT AND GRAPH:


SIGNAL AND SYSTEMS LAB 02

ZOOM:

Explanation:
In this task we made graphs of cos x and x=z by using plot command and give names to them by
legend command so that they can have identities of cos x and x.

***********************************************************

Question#2:
Draw graphs of exp(nx) on the interval -1 ≤ x ≤ 1

for n = 1, 2, . ., 8.

Use Subplots!

ANSWER:

CODE:
x =-1:0.05:1;
for n =1:8
subplot (4,2,n), plot (x, exp (n*x))
end
SIGNAL AND SYSTEMS LAB 02

OUTPUT AND GRAPH:

Question#3:
Make a function to generate: y = t sin (3t) where t is an input by the user to the function .

ANSWER:

CODE:
Function code:
function y=time(mini,maxa)
N=10;
d=1/N;
t=mini:d:maxa;
y=t.*sin(3*t);
plot(y,t);
end
Main:
SIGNAL AND SYSTEMS LAB 02

time(1,3)

OUTPUT AND GRAPH:


SIGNAL AND SYSTEMS LAB 02

Explanation:
In this task we first made function time and then saved it by having two arguments of t. Then
by calling through other script by same name of function we made graph.

Question#5:
Draw following signal in MATLAB in one window using subplot command and also give legend
to them for differentiated.
i) Sin(2πt)
ii) Cos(3/2 πt)
iii) Sin(2πt)+ Cos(3/2 πt)
Where 0≤ t ≤5

ANSWER:

CODE:
N=10;
d=1/N;
t= 0:d:5;
y=sin(2.*pi.*t);
y1=cos((3./2).*pi.*t);
y2=sin(2.*pi.*t)+cos((3./2).*pi.*t);
subplot(221), plot(t,y,'r');
legend('Sin(2pit)');
subplot(222),plot(t,y1,'b');
legend('Cos(3/2 pit)');
subplot(223),plot(t,y2,'g');
legend('Sin(2pit)+ Cos(3/2pi t)');

EXPLANATION:
In this task we made three graphs by using subplotting.

• First graph was named y.


• 2nd graph was named y1.
• 3rd graph was named y2.
• They are differentiated by colours by using commands in subplotting.
• Legend command is used to give them identical names.
SIGNAL AND SYSTEMS LAB 02

GRAPH AND OUTPUT:

CONCLUSION:
In this lab graphs are made by using matlab and they are easily made as they have very wide
range of applications in life. By giving names to graphs and plotting them in the same window
enables us to understand other people about the graphs very easily. Subplotting na mes and
legends features are very essential that we done today in lab. As well as continuous also
discrete signals graph are made by using command stem. These are all the useful features to
make graph.

You might also like