Signals and Systems
(EE-2008)
Spring 2025
LABORATORY MANUAL
Instructor: Dr. Adil Zulfiqar
INTRODUCTION TO MATLAB
(LAB # 01)
Student Name: ALI HASSAN
Roll No: 23F-6087 Section: 4A
Date performed: 23 ,01 , 2025 MARKS AWARDED: ________ / 10
____________________________________________________________________________________________________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES,
ISLAMABAD
Introduction to MATLAB LAB 1
Lab # 01: Introduction to MATLAB
Learning Objectives:
In this lab, you will learn:
⮚ The interface of MATLAB
⮚ Basic arithmetic operators
⮚ Matlab variables
⮚ Matrix manipulation
Software Used: MATLAB
Description:
Matlab (stands for Matrix Laboratory), it integrates computation, visualization, and programming in
an easy-to-use environment, where problems and solutions are expressed in familiar mathematical
notation.
Examples:
∙ Matrix computations and linear algebra
∙ Solving nonlinear equations
∙ Numerical solution of differential equations
∙ Mathematical optimization
∙ Statistics and data analysis
∙ Signal processing
∙ Modelling of dynamical systems
∙ Solving partial differential equations
∙ Simulation of engineering systems
Strengths of MATLAB
∙ MATLAB is relatively easy to learn.
∙ MATLAB code is optimized to be relatively quick when performing matrix operations.
∙ MATLAB may behave like a calculator or as a programming language.
∙ MATLAB is interpreted, errors are easier to fix.
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 2 of 19
Introduction to MATLAB LAB 1
“Fig 1.1”
Desktop Tools
1. Command Window
Use the Command Window to enter variables and run functions and M-files.
“Fig 1.2”
2. Command History:
Statements you enter in the Command Window are logged in the Command History. In the
Command History, you can view previously run statements, and copy and execute selected
statements.
Enter a command “commandhistory” in Command Window to see commands historY.
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 3 of 19
Introduction to MATLAB LAB 1
“Fig 1.3”
“Fig 1.4”
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 4 of 19
Introduction to MATLAB LAB 1
“Fig 1.5”
3. Workspace Browser:
The MATLAB workspace consists of the set of variables (named arrays) built up during a
MATLAB session and stored in memory.
“Fig 1.6”
4. Variable Tab:
∙ Double-click a variable in the Workspace browser to see it in the Variable Tab. ∙ Use the
Variable Tab to view and edit a visual representation of one- or two-dimensional numeric arrays,
strings, and cell arrays of strings that are in the workspace.
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 5 of 19
Introduction to MATLAB LAB 1
“Fig 1.7”
5. Current Folder:
It lists all the files that are available in current directory.
“Fig 1.8”
“Fig 1.9”
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 6 of 19
Introduction to MATLAB LAB 1
Reserved Words:
∙ Reserved Words List:
for end if while
function elseif case otherwise
switch continue else try
catch global persistent break
∙ This list is returned as an output of the ‘iskeyword’ function. For example,
>> iskeyword(‘case’)
ans =
1 (else 0 will be displayed)
∙ The function isvarname(‘MCS’) work in the same manner. ∙ Matlab
will report an error if you try to use a reserved word as variable.
Basic Arithmetic Operations:
Operations Symbol Example
Addition - 3 + 22
Subtraction Multiplication * 54.4 – 16.5 3.14 * 6
Division / or \ 10/2 or 2\10
+
∙ Expressions are evaluated from left to right, with precedence;
Exponentiation > Multiplication & Division > Addition & Subtraction.
Variables Naming Rule:
∙ The first character MUST be alphabetic followed by any number of letters, digits, or
underscore.
∙ The variable names are case sensitive.
∙ Blanks are NOT allowed in a variable name.
∙ Variable names can contain up to 63 characters.
∙ Punctuation characters are not allowed, because many of them have special meanings in
Matlab.
Matlab Special Variables:
ans Default variable name for results
pi Value of π
inf infinity
NaN Not a number e.g. 0/0
i and j i = j = -1
realmin The smallest usable positive real number
realmax The largest usable positive real number
Examples:
>>a = 2
a=
2
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 7 of 19
Introduction to MATLAB LAB 1
>> 2*pi
ans =
6.2832
Types of Variables:
Type Examples
Integer 1362,-5656
Real 12.33,-56.3
Complex X=12.2 – 3.2i (i = sqrt(-1))
Complex numbers in MATLAB are represented in rectangular form. To separate real & imaginary
part
∙ H = real(X)
∙ K = imag(X)
∙ Conversion between polar & rectangular
∙ C1 = 1-2i
∙ Magnitude: mag_c1 = abs(C1)
∙ Angle: angle_c1 = angle(C1)
∙ Note that angle is in radians
Useful Matlab Commands:
∙ who List known variables
∙ whos List known variables plus their size
∙ clear all Clear all variables from work space
∙ clear x y Clear variables x and y from work space
∙ clc Clear the command window only and not any variable ∙ close all
closes all open figures
∙ Extras:
Using up-arrow key allow user to recall most recently used commands
Another trick is to use a ‘letter’ prior using up-arrow
Other MATLAB symbols:
, :
separate statements and data
%;
(2) used as a row separator in a matrix
start comment which ends at end of line
specify range
(1) suppress output
MATLAB Matrices:
∙ MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an
array, in fact, that is how it is stored.
∙ Vectors are special forms of matrices and contain only one row OR one column.
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 8 of 19
Introduction to MATLAB LAB 1
∙ Scalars are matrices with only one row AND one column.
∙ A matrix can be created in MATLAB as follows (note the commas AND semicolons): »
matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
123
456
789
Row Vector:
∙ A matrix with only one row is called a row vector. A row vector can be created in MATLAB as
follows (note the commas):
» rowvec = [12 , 14 , 63]
rowvec =
12 14 63
∙ Row vector can also defined in a following way:
rowvec = 2 : 2 : 10; % start : step size : stop
∙ rowvec =
2 4 6 8 10
Column Vector:
∙ A matrix with only one column is called a column vector. A column vector can be created in
MATLAB as follows (note the semicolons):
» colvec = [13 ; 45 ; -2]
colvec =
13
45
-2
Extracting a Sub-Matrix:
∙ A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of
both matrices and the rows and columns to extract. The syntax is:
sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;
where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the
beginning and ending columns to be extracted to make the new matrix.
∙ A column vector can be extracted from a matrix. As an example we create a matrix below:
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 9 of 19
Introduction to MATLAB LAB 1
» matrix=[1,2,3;4,5,6;7,8,9] Here we extract the column 2 of matrix and make a
column vector:
matrix = » col_two =matrix( 1:3 ,2: 2)
1 2 3 col_two = 2
4565
7898
∙ A row vector can be extracted from a matrix. As an example we create a matrix below:
» matrix=[1,2,3;4,5,6;7,8,9] Here we extract row 2 of matrix and make a row vector.
Note that 2:2 specifies the 2nd row and matrix = 1:3 specifies columns of 2nd row.
1 2 3 » rowvec =matrix(2 : 2 , 1 : 3)
4 5 6 rowvec =
789456
Concatenation:
∙ New matrices may be formed out of old ones
∙ Suppose we have:
a = [1 2; 3 4]
a=12
34
Input output
[a , a, a] ans = 1 2 1 2 1 2
343434
[a ; a; a] ans =
12
34
12
34
12
34
[a, zeros(2); zeros(2), a'] ans = 1 2 0 0
3400
0013
0024
Scalar Matrix Addition & Subtraction:
» a=3;
» b=[1, 2, 3;4, 5, 6]
b=
123
456
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 10 of 19
Introduction to MATLAB LAB 1
» c= b+a % Add a to each element of b
c=
456
789
Scalar - Matrix Multiplication:
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
123
456
» c = a * b % Multiply each element of b by a
c=
369
12 15 18
Other matrices Operations:
Let a=[1 4 3;4 2 6 ;7 8 9]
∙ det(a) : Find the determinent of a matrix.
ans = 48
∙ inv(a) : Find the inverse of matrix.
ans =
-0.6250 -0.2500 0.3750
0.1250 -0.2500 0.1250
0.3750 0.4167 -0.2917
∙ a' : Find the transpose of a matrix.
ans =
147
428
369
∙ min(a) :Return a row vector containing the minimum element from each column.
ans = 1 2 3
∙ min(min(a)): Return the smallest element in matrix:
ans = 1
∙ max(a) : Return a row vector containing the maximum element from each column.
ans = 7 8 9
∙ max(max(a)): Return the max element from matrix:
ans = 9
∙ a.^2 :Bitwise calculate the square of each element of matrix:
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 11 of 19
Introduction to MATLAB LAB 1
ans =
1 16 9
16 4 36
49 64 81
∙ sum (a) : treats the columns of ‘a’ as vectors, returning a row vector of the sums of each column.
ans = 12 14 18
∙ sum(sum(a)): Calculate the sum of all the elements in the matrix.
ans = 44
∙ size (a) : gives the number or rows and the number of columns:
>> [r c] = size(a)
r=3c=3
∙ Let a =[ 4 5 6] , length(a) finds the number of elements in row vector.
Bitwise Multiplication of Two Vectors:
Let a=[1 2 3] ; b=[4 5 6];
∙ a.*b :Bitwise multiply the each element of vector ‘a’ and ‘b’:
ans =
4 10 18
Matrix Division:
∙ MATLAB has several options for matrix division. You can “right divide” and “left divide”.
Right Division: use the slash character
»A/B
This is equivalent to the MATLAB expression
» A*inv (B)
Left Division: use the backslash character
»A\B
This is equivalent to the MATLAB expression
» inv (A)*B
Matrix of Zeros:
∙ Syntax : zeros array
∙ Format : zeros(N), zeros(M,N)
∙ Description:
This function is used to produce an array of zeros, defined by the arguments.
(N) is an N-by-N matrix of array.
(M,N) is an M-by-N matrix of array.
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 12 of 19
Introduction to MATLAB LAB 1
∙ Example;
>> zeros(2) >> zeros(1,2)
ans = ans =
0000
00
Matrix of Ones:
∙ Syntax : ones array
∙ Format : ones(N), ones(M,N)
∙ Description:
This function is used to produce an array of ones, defined by the arguments.
(N) is an N-by-N matrix of array.
(M,N) is an M-by-N matrix of array.
∙ Example;
>> ones(2) >> ones(1,2)
ans = ans =
1111
11
Identity Matrix:
∙ Syntax : identity matrix
∙ Format : eye (N), eye (M,N)
∙ Description:
Create an NxN or MxN identity matrix (i.e., 1’s on the diagonal elements with all others
equal to zero). (Usually the identity matrix is represented by the letter “I”. Type
∙ Example;
>> I=eye(3)
I=
100
010
001
Book Examples:
1. Complex Numbers
%%
clear all;
z=8+j*3
v=9-j*2
a=real(z)+imag(v) % real and imaginary
b=abs(z+conj(v)) % absolute value, conjugate
c=abs(z*v)
d=angle(z)+angle(v) % angle
d1=d*180/pi % conversion to degrees
e=abs(v/z)
f=log(j*imag(z+v)) % natural log
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 13 of 19
Introduction to MATLAB LAB 1
2. Sound of a train
load train
whos
sound (y,Fs)
%%plot it
t=0:1/Fs:(length(y)-1)/Fs;
figure(2); plot(t,y'); grid
ylabel('y[n]'); xlabel('n')
3. Image Manipulation
clc
load clown
colormap ('bone')
imagesc (X)
Exercises:-
NOTE:
⮚ Solve these questions in MATLAB and write answers/code in the manual.
⮚ Submit your manual to lab instructor before leaving the lab.
Q.1 Run the MATLAB help desk by typing helpdesk. The help desk provides a hypertext interface
to the MATLAB documentation.
Q.2 Use MATLAB as a calculator. Try the following:
▪ pi*pi – 10
▪ sin(pi/4)
▪ ans ˆ 2 %<--- "ans" holds the last result
Q.3 Create a vector
a. ‘A’ of even whole numbers between 31 and 75.
b. ‘B’ of odd whole numbers between 75 and 131.
Q.4 Complex numbers are natural in MATLAB. The basic operations are supported. Try the
following:
▪ z = 3 + 4i, w = -3 + 4j
▪ real(z), imag(z)
▪ abs([z,w]) %<-- Vector constructor
▪ conj(z+w)
▪ angle(z)
▪ exp( j*pi )
▪ exp(j*[ pi/4, 0, -pi/4 ])
Q.5 Make sure that you understand the colon notation. In particular, explain in words what the
following MATLAB code will produce.
▪ jkl = 0 : 6
▪ jkl = 2 : 4 : 17
▪ jkl = 99 : -1 : 88
▪ ttt = 2 : (1/9) : 4
▪ tpi = pi * [ 0:0.1:2 ];
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 14 of 19
Introduction to MATLAB LAB 1
Q.6 Extracting and/or inserting numbers into a vector are very easy to do. Consider the following
definition of xx:
▪ xx = [ zeros(1,3), linspace(0,1,5), ones(1,4) ]
▪ xx(4:6)
▪ size(xx)
▪ length(xx)
▪ xx(2:2:length(xx)
Explain the results echoed from the last four lines of the above code.
Q.7 Enter the following in MATLAB.
A= 2364 ⎤⎥ ,B= ⎤⎥
⎦ ⎡⎢⎣ ⎦
⎡⎢⎣ 1662
Find AB, and A-1.
TASKS:
Matrix Manipulation:
A. Generate a 6x6 matrix A with magic command and replace first column with:
1
-2
-3
-4
-5
-6
`
ANSWER:
TASK NO 1:
A = magic(6);
A(:,1)= [1; -2; -3; -4; -5; -6];
disp(A);
TASK NO 1 AMSWER:
1 1 6 26 19 24
-2 32 7 21 23 25
-3 9 2 22 27 20
-4 28 33 17 10 15
-5 5 34 12 14 16
-6 36 29 13 18 11
B. Generate a 6x1 matrix z as given
1
2
3
4
5
6
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 15 of 19
Introduction to MATLAB LAB 1
ANSWER:
TASK NO 2:
z = [1; 2; 3; 4; 5; 6];
disp(z);
TASK NO 2 ANSWER:
1
2
3
4
5
6
C. Solve linear system of equations Ax=z
ANSWER:
TASK NO 3:
A = [3, 2, -1; 2, -2, 4; -1, 0.5, -1];
z = [1; -2];
x = A \ z;
disp(x);
TASK NO 3 ANSWER:
1.0000
-2.0000
-2.0000
D. Compute determinant of matrix A
ANSWER:
TASK NO 4:
A = [2, 4, 3; 1, 5, 7; 6, 8, 9];
determinant = det(A);
disp(determinant);
TASK NO 4 ANSWER:
%determinant
44
E. Extract a 4x4 matrix (first 4 rows and first 4 columns) from matrix A.
ANSWER:
TASK NO 5:
A = randi(10, 6, 6);
B = A(1:4, 1:4);
disp(A);
disp(B);
TASK NO 5 ANSWER:
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 16 of 19
Introduction to MATLAB LAB 1
%orignal matrix
9 3 10 8 7 8
10 6 5 10 8 1
2 10 9 7 8 3
10 10 2 1 4 1
7 2 5 9 7 1
1 10 10 10 2 9
%Extract matrix
9 3 10 8
10 6 5 10
2 10 9 7
10 10 2 1
F. Write commands to make a matrix of 2nd , 4th and 6th columns of the original matrix?
ANSWER:
TASK NO 6:
A = randi(10, 6, 6);
B = A(:, [2, 4, 6]);
disp(A);
disp(B);
TASK NO 6 ANSWER:
%ORIGNAL MATRIX
7 8 8 2 8 6
4 8 8 5 3 2
10 2 3 10 6 2
1 5 7 4 7 3
5 5 7 6 9 9
4 7 2 3 10 3
%EXTRACT MATRIX
8 2 6
8 5 2
2 10 2
5 4 3
5 6 9
7 3 3
Submission Declaration by the Student:
In submitting this lab write-up to the Lab Engineer/Instructor, I hereby declare that:
• I have performed all the practical work myself
• I have noted down actual measurements in this writeup from my own working •
I have written un-plagarised answers to various questions
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 17 of 19
Introduction to MATLAB LAB 1
• I have/have not obtained the desired objectives of the lab.
Reasons of not obtaining objectoves (if applicable): _________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________
Student’s signature and Date
Student Evaluation by the Lab Engineer:
The Lab Engineer can separate this page from the writeup and keep it for his/her own record. It
must be signed by the student with date on it.
• Lab Work: objectives achieved (correctness of measurements, calculations, answers to questions
posed, conclusion) ________/30
• Lab Writeup: Neatness, appropriateness, intime submission ________/10 • Troubleshooting:
Were the student able to troubleshoot his/her work when it was purposedly changed? ________/10
• TOTAL: ________/50
Feedback on student behaviour:
Encircle your choice. -2 means poorest/worst/extremely inadequate/irrevlevant, 0 gives an average
score, and +2 means best/most relevant/most adequate.
• Did the student join the lab at the start/remained in lab? -2 -1 0 1 2 • Did the student
remain focused on his/her work during lab? -2 -1 0 1 2 • Rate student's behaviour with
fellows/staff/Lab Engineer? -2 -1 0 1 2 • Did the student cause any distraction during the
Lab? -2 -1 0 1 2 • Was the student found in any sort of plagiarism? -2 -1 0 1 2 Additional
comments (if any) by the Lab Engineer:
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________.
___________________________
Lab Engineer’s signature and Date
Student's feedback: [Separate this page; fill it; drop in the Drop Box.] • Providing feedback
for every lab session is optional. No feedback means you are satisified • The Lab Committee will
consider only duly filled forms submitted within one week after the lab • This feedabck is for LAB
session: LAB Number: _____, Date: _____________________ • General (to provide feedback on a
persistent practice/ocurrence in LABs). • Your current CGPA is in the range 4.00 to 3.00/2.99 to
2.00/1.99 to 1.00/0.99 to 0.00 This feedback is:
• For a Particular
• Who conducted the LAB? __________________________________________________ •
Actual Start time: _______________ Total Duration of Lab: _______________________
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 18 of 19
Introduction to MATLAB LAB 1
• Instruction Duration: _________________ Practical Duration: _____________________ •
LAB writeup available before LAB? Yes/No with the Photocopier/in LAB/in SLATE • Had
the theory related to lab been covered in theory class? Yes/No
Encircle your choice. -2 means poorest/worst/extremely inadequate/irrevlevant, 0 gives an average
score, and +2 means best/most relevant/most adequate.
Instruction Session discussion?
Was duration of instruction -2 -1 0 +1 +2 -2 -1 0 +1 +2
session adequate? How much did
you understand about the
practical? How much content was
-2 -1 0 +1 +2 -2 -1 0 +1 +2
irrelevant to the practical? Did the
instructor allowed Q/A and
Practical Did you get sufficient time for practical? -2 -1 0 +1 +2
Presence in lab at all time?
Ability to convey?
-2 -1 0 +1 +2
-2 -1 0 +1 +2
Lab troubleshooting? How friendly
Engineer was the lab staff?
Presence of staff throughout the -2 -1 0 +1 +2
lab session? Impact of availability
of staff on your practical?
Staff Performance of Electronic
-2 -1 0 +1 +2
Instruments? Performance of
Breadboard/experiment kit?
Equipment Performance of circuit
Readiness to help during components esp. ICs? -2 -1 0 +1 +2
practical? -2 -1 0 +1 +2
Readiness to discuss theoretical
aspects? Helps in -2 -1 0 +1 +2
troubleshooting?
-2 -1 0 +1 +2
Guides hows & whys of
-2 -1 0 +1 +2
-2 -1 0 +1 +2
-2 -1 0 +1 +2
-2 -1 0 +1 +2
Overall Your overall rating for the whole lab -2 -1 0 +1 +2
session? Other comments:
_______________________________________________________________________________
__________________________________________________________________________.
SIGNALS AND SYSTEMS LAB NUCES FAST, CFD Page 19 of 19