LAB-01 EE-311 Signal and Systems PDF
LAB-01 EE-311 Signal and Systems PDF
LAB EXPERIMENT # 01
Introduction to Matlab Programming
OBJECTIVES:
The purpose of this lab is to familiarize with basic MATLAB functionality. The students will
also learn how vectors and matrices are defined and manipulated in MATLAB.
The MATLAB desktop is the main MATLAB application window. In Figure 1, the desktop
contains five sub-windows: the Command Window, the Workspace Browser, the Current
Directory Window, the Command History Window, and one or more Figure Windows, which
are shown only when the user displays graphics or plots.
The Command Window is, where the user types MATLAB commands and expressions at the
prompt (>>) and where the outputs of those commands are displayed.
MATLAB defines the workspace as the set of variables that the user creates in work session.
The Workspace Browser shows those variables and some information about them.
The Current Directory tab above the Workspace tab shows the contents of the current
directory, whose path is shown in the Current Directory Window. By Clicking on the button to
the right of the window allows the user to change the current directory. MATLAB uses a search
path to find M-files and other MATLAB. Any file run in MATLAB must reside in the current
directory or in the directory that is on the search path. The easiest way to see which directories
are on the search path, or to add or modify a search path is to select Set Path from the File
menu on the desktop, and then use the Set Path dialog box.
The Command History Window contains a record of the commands a user has entered in the
Command Window, including both current and previous MATLAB sessions. Previously
entered MATLAB commands can be selected and re-executed from the Command History
Window by right clicking on a command. This action launches a menu from which to select
various options in addition to executing the commands. This is a useful feature when
experimenting with various commands in a work session.
1-1
1.2 Getting Started:
To run MATLAB simply double-click the MATLAB icon on the desktop or find the MATLAB
command in the start menu. This will open a MATLAB command window, which displays a
prompt “>>”. The commands are typed at this prompt. The “>>” is called the MATLAB
prompt. There is no need to type that part. Explore the built-in demos by typing demo.
1-2
Pwd
Cd
Lookfor
help
2. Variables:
MATLAB does not require the variables to be declared before their use. To assign value to a
variable equal “=” sign is used. To set x equal to 2 write at command prompt.
>>x = 2
ans =
16
where ans is also a MATLAB variable.
The clear x command clears the variable x, from the workspace. This is useful when we want
to reclaim system memory. Similarly, “clear all” and “close all” commands are used to clear
all previous variables in memory and close all figure windows opened in MATLAB desktop.
2.5 Vectors:
1-3
MATLAB simplest mathematical objects are scalars, vectors, and matrices. An array has
dimensions M× N, where M and N are positive integers. M is the number of rows and N is the
number of columns.
The coefficients of an array are real or complex numbers. There are two types of vectors
supported in MATLAB i.e., row vectors and column vectors. The simplest way to create a row
vector is by using concatenation brackets or square brackets. Various values in a row vector
are separated by space or comma whereas in column vector various values are separated by
semicolon.
>> x = [1 2 3 4]
>> y = [1; 2; 3; 4]
This will create a row vector x and column vector y.
Question 1: Initialize a row vector x1 and column vector y1 with 5 elements each.
>> X=[1 2 3 4 5]
X=
1 2 3 4 5
>> Y=[1;2;3;4;5]
Y=
1
2
3
4
5
To convert a row vector into column vector and vice versa transpose operator’ is used.
>> x1 = x’
>> y1 = y’
This will create a column vector x and row vector y.
1-4
MATLAB does not allow arguments of vectors or matrices to be zero or negative. For instance,
if we want the first entry of the vector y we need to type:
>>y(1)
It will return the first element of vector y.
ANS:
??? Subscript indices must either be real positive integers or logicals.
To create a vector corresponding to a sequence of numbers (in this case integers) there are
different approaches, as follows:
Method 1:
>>n = 0:10
It will create a row vector with entries 0 to 10 increased by 1. Write down the output.
n=
Columns 1 through 10
0123456789
Column 11
10
Method 2:
>>[0:1:10]
n = 0:10
n=
0 1 2 3 4 5 6 7 8 9 10
Method 3:
If we wish the increment different from 1 (default value), then we indicate it as in the following:
n1 = 0:2:10
1-5
n1 =
0 2 4 6 8 10
4. Matrices:
>> A = [1 2 3; 4 5 6; 7 8 9];
>> B = [1:10 ; 11:20]
It will create a 3 x 3 matrix in MATLAB in which the first the workspace row has elements 1,
2, 3, the second row has elements 4, 5, 6, and so on (note the use of the brackets [ ]).
>> C = A(2,3)
>> D = A(:,3)
>> E = A(2, :)
One of MATLAB most powerful index operators is the colon operator “:”. For example, when
used in matrix indexing, the colon operator selects parts of a matrix. For example, the following
statement extracts the third column of A.
C = A(2,3)
C=
>> D = A(:,3)
D=
3
6
9
>> E = A(2, :)
E=
4 5 6
1-6
Finally, the following statement extracts the top two rows:
>> F= A (1:2,1:3)
F= A (1:2,1:3)
F=
1 2 3
4 5 6
>>I=ones(M,N)*5
; generates M x N matrix with all elements are 5.
1-7
Matrix concatenation is the process of joining small matrices to create larger matrices. The
concatenation operator is the pair of square brackets, [ ]. Earlier in this section,
>> A = [1 2 3; 4 5 6; 7 8 9]
command creates a matrix of size 3 x 3. Note that individual elements are separated by a space.
Separating the elements by commas would yield the same result. Note also the order in which
the elements were concatenated. The group of three elements before the first semicolon formed
the firstrow, the next group formed the second row, and so on. Clearly, the number of elements
between semicolons has to be equal. This concept is also applicable when the elements
themselves are matrices. For example, consider the 2 x 2 matrix
The statement
>> C = [B B;B+4 B-1]
in which B + 4 and B - 1 indicate adding 4 and subtracting 1 from all elements of B,
respectively.
>> O=[A,B+30 ;A-20,B*2]; generates a matrix by using already defined matrices.
>>P= repmat (O,M,N); repeats already defined matrix by provided number of rows
and columns repetition numbers M and N respectively.
Output:
O=[A,B+30 ;A-20,B*2]
O=
1 2 3 31 32 34
4 5 6 33 34 36
7 8 9 35 36 38
-19 -18 -17 2 4 8
-16 -15 -14 6 8 12
-13 -12 -11 10 12 16
Scripts do not accept input arguments or return output arguments. They operate on data
in the workspace.
1-8
Functions accept input arguments and return output arguments. They operate on data
in the workspace.
6 Logical Operator:
We can perform logical operations on variables, elements of arrays by logical operators,
e.g. x>=y, x<=y, x==y, and x-=y
Question
For x=[1,3,5,7] and y=[3,2,5,9]
Write down the output of the following logical operators.
>> x >= y
> x >= y
ans =
0 1 1 0
>> x <= y
x <= y
ans =
1 0 1 1
>> x == y
x == y
ans =
0 0 1 0
>> x =- y
> x =- y
x=
-3 -2 -5 -9
1-9
7. Comments:
Starting a line with percent “%” sign tells MATLAB that line is a comment. If comments are
placed in very beginning of m-file, then by typing help filename, MATLAB prints the
comments that appear at the beginning of file. This will allow the user to expand MATLAB
help facility for user-defined functions.
1-10
Homework Problems:
Question 1: Write MATLAB commands to initialize a column vector whose entries will start
from 5 and end with +5 with an increment of 2.
Ans;
T=-5:2:5
T=
-5 -3 -1 1 3 5
E=T'
E=
-5
-3
-1
1
3
5
Question 2:(a) Write MATLAB commands to access all entries except the last one, of a vector
initialized in Question 1. (b) Repeat the same problem to access only the last entry in the already
stored vector.
Ans:
a) T(1:5)
ans =
-5 -3 -1 1 3
b) T(6)
ans =
Question 3: Write down a single MATLAB command to initialize a 3 x 3 matrix, whose first
rows contains zeros, second row contains ones and third row contains 2. The matrix should be
0 0 0
𝐴 = [1 1 1]
2 2 2
Ans:
T=[0 : 2; 0 : 2; 0 : 2]
T=
0 1 2
0 1 2
0 1 2
1-11
T=T'
T=
0 0 0
1 1 1
2 2 2
2nd method:
E=[0 0 0;1 1 1;2 2 2]
E=
0 0 0
1 1 1
2 2 2
1 2 3
Question 4:Write MATLAB commands to initialize a matrix 𝐵 = [4 5 6] and access the
7 8 9
elements from first to third rows and columns of B.
Ans: B=[1:3;4:6;7:9]
B=
1 2 3
4 5 6
7 8 9
>> B=B'
B=
1 4 7
2 5 8
3 6 9
Question 5: Write MATLAB command to replace the second row with elements 1, 2, and 3
for the matrix initialized in Question 4.
Ans:
B(2,:)=[1 2 3]
B=
1 2 3
1 2 3
7 8 9
Note: Create a single script file for the whole lab session. Name the file as LABxx_16TCyy.m,
where xx is the Lab No. (01 for this lab) and yy is the student Roll No.
It is important to note that file without following the mentioned naming convention will be
considered as “No Submission.”
1-12