Department of Electrical Engineering
Riphah College of Science & Technology
Faculty of Engineering & Applied Sciences
Riphah International University, Lahore
Program: [Link]. Electrical engineering Semester: V
Subject EEL-311 Signal and System Date: ……………….
Experiment 1: Introduction to MATLAB and Signals in MATLAB
OBJECTIVES:
(i) Introduction to MATLAB
(ii) Plotting of Continuous and Discrete time signals on MATLAB
Name & SAP: ….…………………………………………………………
No. Lab Evaluation Scheme Obtained
Marks
1 Understanding and Ability to Conduct Experiment. (0-5)
2 Implementation and Results (0-5)
Total
No. Lab Report Scheme Obtained
Marks
1 Report Content (0-5)
2 Code/Output Presentation and Conclusion (0-5)
Total
Remarks (if any): ………………………………….
Name & Signature of faculty: …………………………………
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
What is MATLAB?
MATLAB is a high-level technical computing language equipped with a user-friendly interface. Its name
stems
from the words MATrix and LABoratory as it is based on the use of matrices. MATLAB is an extremely
powerful tool useful for scientists and engineers from various disciplines. For example, MATLAB can be
used
in a wide range of applications, such as telecommunications, signal and image processing, control,
mathematics, financial modelling, bioengineering, aeronautics, and many more.
M-Files
In order to write many commands that are executed all together, the program must be written in
a text editor. In this editor, one can type all the needed commands to form a program, save the
program, and execute it any time he or she wants. The text files are called M-files due to their
suffice *.m.
There are two categories of M-files: the Scripts and the Functions.
Scripts
Sripts are the M-files with MATLAB commands. Their name must have a .m suffix. Scripts are suitable for
solving the problems that require many commands. The advantage of the scripts is that they are
implemented very easily.
Lab Task
Write a script file and execute.
% Program to understand the use of script file
clc Draw its output here
close all
clear all
% All commands that are
% needed are typed in the
% .m File. Everything written
% at the right of symbol %
% is a comment and is not
% taken into account
t=-5:0.1:5;
f=cos(2*pi*t);
plot(t,f);
Signal and System Lab 5th Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Functions
Function are also M-files, That is, are files with extension .m and must be saved in the current
Directory of MATLAB. The difference between functions and scripts is that a function accepts one
or more input arguments and returns one or more output arguments. To declare that an M-file is
a function the first line of the m file must contain the syntax definition. More specifically, the first
line of the M-file must be of the form function[y1,y2,y3,…yn]=name{x1,x2,x3… xm}. The variable
y1,y2,…yn are the outputs of the function while x1,x2,…xm are the input arguments. In case there
is only one output, the square brackets are not necessary. The “name” specifies the name of the
function. In order to execute a function, first the M-File is saved in Current Directory.
Lab Task
Write a function file and execute. Function should accepts as input two matrices and returns their sum
and product.
function [sm,pro]=oper(A,B)
% Program to understand the use of a function file
% This function computes the
% sum and the product of 2 matrices
sm=A+B;
pro=A*B;
% Program to understand the use of a function file
clc Write its output here
close all
clear all
% to test the function operation
% Enter the matric A
A= [ 2 3; 4 5]
% Enter Matrix B
B= [4 5; 5 6]
[sm,pro]=oper(A,B)
Useful Commands
Here we will learn and practice useful (when workig with vectors and matries) commands. As already
discussed, the command sum returns the sum of the elements of a vector. The command cumsum
returns a vector whose elements are the comulative sum of the previous elements, the command prod
is the product of the vector elements, while the command diff returns a vector in which each element
is given by its subtraction with the previous element. The command max and min return the largest
and smallest elements of the vector,respectively, as well as their index. The command sort sorts the
vector elements in ascending(by default) or descending order. The command mean computes the mean
Signal and System Lab 5th Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
value, while the command median returns the median value. All these commands are suitable also for
matrices by slightly changing their syntax.
Signal and System Lab 5th Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Special Forms of Matrices
The command ones(M,N) creates a matrix of size MxN with ones. Typing ones(M) returns a square
matrix with ones. Similarly, the command zeros(M,N) creates a matrix of size MxN with zeros. The
command rand(M,N) returns an MxN matrix with random elements. The command eye(M,N)
defines an MxN matrix with ones in the main diagonal zeros everywhere. The command magic(M)
creates a square matrix with the property that the sum of each row or column is equal.
Commands Write output of each command
ones(2,3)
zeros(1,4)
rand(3)
eye(4,2)
eye(3)
A = magic(3)
Plotting Signals in MATLAB
MATLAB is a very reliable and power full tool for plotting. A graph is constructed as a set of
points in two or three dimensions. These points are typically connected with a solid line.
Commonly a graph of a function is required. However in MATLAB a plot is done using the
vectors and matrices not functions.
Lab Task
Plot the function
𝒚(𝒙) = 𝒙𝟐 , −𝟐 ≤ 𝒙 ≤ 𝟐
Signal and System Lab 5th Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
% Program to understand the two dimension plot
clc Draw the figure here
close all
clear all
x=-2:1:2 % independent variable , length of the plot
length (x)
y=x.^2 % Function
length (y)
plot(x,y)
The figure is given; Write matlab code to get this figure
It is possible to create a graph using colors, symbols used to draw the points, and type of line that
connects the points of your choice. This is achieved by applying one more input argument to the
command Plot The new argument is a series of special character given in single quotes. The available
special characters are presented in the table below.
Signal and System Lab 5th Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Formatting a Figure
The command grid on adds lines to the graph, while the command grid off removes the
grid lines. Simply typing grid is switch between the two modes. Text besides the x-axis and y-
axis can be added using the commands xlabel and ylabel, respectively. A graph title is
inserted by the Command title.
% Program to understand formatting a plot: axis labeling, title and legend
Clc Plot the diagram
clear all
close all
x = linspace(0,2*pi,150); %same as x=0:0.04:2*pi
plot(x,cos(x), 'r*' ,x,sin(x), 'k' )
grid
xlabel('Time Axis' )
ylabel('Amplitude' )
title('Graph of cos(x) & sin(x)' )
legend('cos' , 'sine' )
Plotting in Different Figures
Up to this point, all plots were made in single figure named Figure 1. By typing at the command
prompt figure, a new figure with name Figure 2 appears without closing the old figures. The
command subplot(m,n,p) or subplot(mnp) splits the figure window into small
subfigures and makes the pth subfigure active.
Lab Task
Plot the functions that were plotted in last lab task in the following two ways.
1. In two different figures
2. In a figure but separately using subplot.
Plotting the Discrete Time Functions
A discrete time function is a function of the form [ ] In this
case the appropriate command for plotting the function is ‘stem(n,f)’
Lab Task
Plot the discrete function
𝒚(𝒙) = 𝒙𝟐 , −𝟐 ≤ 𝒙 ≤ 𝟐
Signal and System Lab 5th Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
% Program to understand the plotting of discrete time signal
Clc Plot the signal
clear all
close all
n = -3:3
f= n.^2
stem(n,f)
xlabel('Time Axis' )
ylabel('Amplitude' )
title('Graph of f(n)' )
Write MATLAB code for the figure given
Piecewise Functions
Now we will discuss the way of defining and plotting functions with more than one part.
Lab Task
Plot the following function
1, −2 ≤ 𝑡 ≤ −2
𝑓(𝑡) = { 0, 2<𝑡<5
t ∗ sin(4 ∗ 𝑝𝑖 ∗ 𝑡) 5 ≤ 𝑡 ≤ 8
Signal and System Lab 5th Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
% Program to understand piecewise functions plotting
clc Plot the diagram
clear all
close all
t1=-2:.1:2;
t2=2.1:.1:4.9;
t3=5:.1:8;
f1=ones(size(t1));
f2=zeros(size(t2));
f3=t3.*sin(4*pi*t3);
t=[t1 t2 t3];
f=[f1 f2 f3];
plot(t,f)
title('Multi-part function f(t)' )
xlabel( 'time' )
ylabel( 'Amplitude' )
%Combine Plots in Same Axes
clear all
close all
x = linspace(0,10,50);
y1 = sin(x);
plot(x,y1)
title('Combine Plots')
hold on
y2 = sin(x/2);
plot(x,y2,'red')
y3 = 2*sin(x);
scatter(x,y3,'k')
Signal and System Lab 5th Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
%Sub Plots in MATLAB
clear all
close all
x = linspace(0,10,50);
y1 = sin(x);
subplot(2,2,1)
plot(x,y1)
title('Combine Plots')
hold on
subplot(2,2,2)
y2 = sin(x/2);
plot(x,y2,'red')
subplot(2,2,3)
y3 = 2*sin(x);
scatter(x,y3,'k')
Signal and System Lab 5th Semester-EE RCST Lahore