Lab Exp 1
Lab Exp 1
Lab Exp 1
Objectives
To familiarize the students with MatLab and the basic concept of signals in the MatLab. Following are the
main features of the experiment.
Introduction to MATLAB
Plotting of Continuous and Discrete time signals on MATLAB.
Equipment Required
Software: MATLAB
Hardware: Computer
Lab Instructions
This lab activity comprises of three parts: Pre-lab, Lab Exercises, and Post-Lab Viva session.
The students should perform and demonstrate each lab task separately for step-wise evaluation.
Only those tasks that are completed during the allocated lab time will be credited to the students.
Students are however encouraged to practice on their own in spare time for enhancing their skills.
Lab objectives
MATLAB codes
Results (graphs/tables) duly commented and discussed
Conclusion
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.
Solution
% Program to understand the use of script file
% Muhammmad Usman Iqbal
% EEE223 Signals and Systems
clc
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 acount
t=-5:0.1:5;
f=t.*cos(2*pi*t);
plot(t,f);
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.
Solution
function [sm,pro]=oper(A,B)
% Program to understand the use of a function file
% Muhammmad Usman Iqbal
% EEE223 Signals and Systems
% 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
% Muhammmad Usman Iqbal
% EEE223 Signals and Systems
clc
close all
clear all
% to test the function oper
% Eneter the matric A
A= [ 2 3; 4 5]
% Enter Matrix B
B= [4 5; 5 6]
[sm,pro]=oper(A,B)
A =
2
4
3
5
4
5
5
6
B =
sm =
6
9
8
11
23
41
28
50
pro =
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 value, while the
command median returns the median value. All these commands are suitable also for matrices by slightly
changing their syntax.
Commands
a = [4
s = sum(a)
c = cumsum(a)
6]
Results/Comments
a = 4 2 7 0 6
%Definition of vector .
s = 19
%Sum the elements of .
c = 4 6 13 13 19
%Cumilative sum.the result is obtained as[4,4+2,4+2+7,
4+2+7+0, 4+2+7+0+6]
p = prod(a)
p = 0
%Product of all elements.
d = diff(a)
d = -2 5 -7 6
%Difference between two consecutive elements i.e.,
d(1)= (2)- (1),etc.
[m,i] = max(a)
[m,i] = min(a)
max(a)
m = 7
i = 4
%The largest value is assigned to variable
index is aasigned to variable .
m = 0
i = 3
%The smaller value is assigned to variable
index is aasigned to variable .
,and its
,and its
ans = 7
%If no output variable is specified,only the largest value
is returned.
mean(a)
ans = 3.8000
%Mean value of the elements.
median(a)
ans = 4
%Median value of the vector.
sort(a)
ans = 0 2 4 6 7
%Sorting in ascending order.
sort(a,descend)
ans = 7 6 4 2 0
%Sorting in descending order.
Commands
Results/Comments
ones(2,3)
ans = 1
1
1
1
1
1
%Matrix of size 2x3 with ones.
zeros(1,4)
ans = 0 0 0 0
%Matrix of size 1x4(or vector of length)with zeros.
rand(3)
eye(4,2)
eye(3)
A = magic(3)
ans = 0.8600
0.9000
0.4600
0.6000
0.1000
-0.3000
0.4540
0.6023
0.2700
%Matrix of size 3x3 with random elements. If there is one
input argument to the command, the obtained matrix is
square.
ans = 1
0
0
1
0
0
0
0
%Magic of size 4x2 with ones in the main diagonal and
zeros elsewhere.
Ans = 1
0
0
0
1
0
0
0
1
%The identity matrix 1 of size 3x3.
A = 8
1
3
5
4
9
%Magic matrix
6
7
2
Symbolic Variables
In MATLAB, a variable type is the symbolic variable (or object). A symbolic variable is defined by the command
sym and syms. The use of symbolic variables allows the computation of limits, integrals, derivatives etc.
, 2
Solution
% Program to understand the two dimension plot
% Muhammmad Usman Iqbal
% EEE223 Signals and Systems
clc
close all
clear all
x=-2:2 % independent variable , length of the plot
length (x)
y=x.^2 % Function
length (y)
plot(x,y)
x=
-2 -1
ans =
5
y=
4
ans =
cos( ) , ( ) =
( ),
( ) = 2 sin( ) , 0
Solution
% Program to understand the two dimension plot
% Muhammmad Usman Iqbal
% EEE223 Signals and Systems
clc
clear all
close all
x = linspace(0,2*pi,100) % linespace could be used to create a vector.
x=0:pi/50:2*pi
% this is same value as above. Both method are correct
y = (x.^2).*cos(x);
g = x.*cos(x);
f = (2.^x).*sin(x);
plot(x,y,x,g,x,f)
In the previous examples, the functions were plotted with predefined colors and line type (solid). 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.
Symbols
B
G
R
C
M
Y
K
W
Color
Blue
Green
Red
Cyan
Magenta
Yellow
Black
White
Symbol
.
o
x
+
*
s
d
<,>
P
h
Point Type
Point
Circle
x-mark
Plus
Star
Square
Diamond
Triangle
Pentagram
Hexagram
Symbol
:
-.
_
Line Type
Solid
Dotted
Dashdot
Dashed
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
% Muhammmad Usman Iqbal
% EEE223 Signals and Systems
clc
clear all
close all
x = linspace(0,2*pi,150);
plot(x,cos(x),'r*',x,sin(x),'k')
grid
xlabel('Time Axis')
ylabel('Amplitude')
2.
Solution
% Program to understand the plotting of discrete time signal
% Muhammmad Usman Iqbal
% EEE223 Signals and Systems
clc
clear all
close all
n = -3:3
f= n.^2
stem(n,f)
xlabel('Time Axis')
ylabel('Amplitude')
title('Graph of f(n)')
. In this
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
sin(4 ) 5 8
Solution
% Program to understand piecewise functions plotting
% Muhammmad Usman Iqbal
% EEE223 Signals and Systems
clc
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')
( )=
Plot ( ) for x from
3. Define and plot the real and the imaginary part of the signal
( )=
,
( )=
)/
( ) through your
Performance
Viva
(10 Marks)
(5 Marks )
Total/15
Performance
/4
Results
/3
Critical Analysis
/1
/2
Comments