0% found this document useful (0 votes)
128 views12 pages

LAB-01 EE-311 Signal and Systems PDF

The document is a lab manual for an introduction to MATLAB programming course. It outlines the objectives of getting students familiar with basic MATLAB functionality and how to define and manipulate vectors and matrices. It covers the MATLAB workspace environment, important commands, how to define and manipulate scalars, vectors, matrices, and built-in matrix functions. Examples are provided for defining vectors and matrices, indexing into them, and performing operations like transposition and summation.

Uploaded by

Awais Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views12 pages

LAB-01 EE-311 Signal and Systems PDF

The document is a lab manual for an introduction to MATLAB programming course. It outlines the objectives of getting students familiar with basic MATLAB functionality and how to define and manipulate vectors and matrices. It covers the MATLAB workspace environment, important commands, how to define and manipulate scalars, vectors, matrices, and built-in matrix functions. Examples are provided for defining vectors and matrices, indexing into them, and performing operations like transposition and summation.

Uploaded by

Awais Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

The Islamia University of Bahawalpur

University College of Engineering &Technology


Department of Electronic Engineering
LAB MANUAL SIGNALS AND SYSTEMS EE-311 5thSemester

LAB EXPERIMENT # 01
Introduction to Matlab Programming

Student Name: Abdul Rehman Roll No: 16ES08

Lab Instructor Signatures: Date:

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.

1. The MATLAB Working Environment:

1.1The MATLAB Desktop:

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.3 MATLAB BASICS:

1.31 Getting Help:


MATLAB provides an online help system accessible by using the help command. For example,
to get information about the function size, enter the following:
>> help help
There also is a help desk (formatted in HTML for viewing from a Web browser) with useful
introductory material. It is accessible from the Help menu. If you have no prior experience with
MATLAB, see the topic “Getting Started” in the help desk. Spend some time with this. You
can find in the help desk all the information you need to carry out the exercises.

1.3.2 Important Commands:

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

Matlab replies with


x=
2
Type 2^4 at the command prompt. Output will be

ans =
16
where ans is also a MATLAB variable.

2.1 MATLAB as Calculator


P=3*(sqrt(5)-1)/(sqrt(5)+1)^2-1
Q=log(exp(3))
R=log10(exp(3))
S=(sin(pi/6))^2+(cos(pi/3))^2
T=(1+3i)/(1-3i)

2.2 Techniques for rounding off variables:


A=[-2.33 2.66] ;
fix (A) ; floor (A) ; ceil (A) ; round(A)
B = [1.52 1.67]
C= rem (A,B)

2.3 Suppressing output:


When we type a command, function, or variable name, its value appears on the screen.
Sometimes it is inconvenient if you do not want to display the output at every step of your
program. The output display of the value can be suppressed by putting the semicolon at the end
of command or variable name. If the semicolon is not present at the end of a command line,
MATLAB outputs the contents of the operation on the screen.

2.4 Clearing the variables:


The command
>> clear x

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.

If M= N= 1, the variable is a scalar.


If M= 1and N>1, then the variable is a row vector.
If M>1 and N= 1, then the variable is a column vector.
If both M and N are greater than one, then the variable is a matrix.
If M = N, then the variable is a square matrix.

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.

To see the dimension of x and y variables, type


>>whos
The whos command provides information on existing variables. Try whos command.

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.

Question 2: What will happen if the following command is entered?


>>y(0)

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]

What will be the output of the above command?

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 start , step , stop

What will be the output after executing the above command?

n1 = 0:2:10

1-5
n1 =

0 2 4 6 8 10

4. Matrices:

4.1 Defining Matrices


Matrices are represented in MATLAB by writing individual rows, separated by semicolons,
e.g.

>> 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.

What will be the outputs?

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)

The output will be.

F= A (1:2,1:3)

F=

1 2 3
4 5 6

4.2 Built-in Matrices Functions


MATLAB provides four functions that generate the following basic matrices:
>>E=zeros(M,N); generates an M x N matrix of zeros.

>>F=ones(M,N); generates a M x N matrix of ones.

>>G=eye(M); generates a diagonal matrix

>>H=zeros(M,N)+5; generates M x N matrix with all elements are 5.

>>I=ones(M,N)*5
; generates M x N matrix with all elements are 5.

>>rand(M,N); generates a M x N matrix whose entries are uniformly


distributed random numbers in the interval [0 to 1].

>>randn(M,N); generates a M x N matrix whose numbers are normally distributed numbers


with mean 0 and variance 1.

>>L =diag(A); extracts the diagonal elements of a matrix M

>>M=diag(A,2); extracts the 2nd bi-diagonal of a matrix D

>>N=diag(k); if k is vector then can generate matrix with elements of k as


diagonal of new matrix

>>Q=sum(J); add the columns of any already defined matrix

>>R=transpose(Q)or Q’ ; generates the transpose of a matrix

4.3 Matrices Concatenation:

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

>> B=[1 2 4; 3 4 6; 5 6 8];

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

5 MATLAB Script files:


Using command prompt is recommended for temporary calculations. However, it is mostly
desirable to save code so that we can run it again in future Matlab sessions. The saved Matlab
files are called m-files and must have an extension .m. There are two types of m-files i.e.scripts
and functions.

 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

You might also like