Bahria University Karachi Campus: Matrix Generation & Matrix Operations
Bahria University Karachi Campus: Matrix Generation & Matrix Operations
LAB EXPERIMENT # 2
OBJECTIVE:-
The purpose of this lab is to familiarize you with basic MATLAB functionality. The tasks include
TOOLS:-
THEORY:-
The name MATLAB is short for MATrix LABoratory. It is a commercial software package whose
main function is to perform calculations on matrices, row vectors and column vectors. It is widely
used in both industry and academic institutions. It possesses many of the features of a high level,
numerically oriented programming language, and it has a large collection of built-in functions for
performing matrix and vector operations in a way that is very simple for the user. For example, to
find the determinant of a matrix A one needs only enter: det(A)
6
LAB-2
>> v = [2 4 7 5]
This creates a variable v whose current value is a row vector with four elements as shown. After
pressing “return” the value of v will be echoed back to you. To suppress the echo, one uses a semi-
colon following the command line. Thus
>> w = [1 3 8 9];
Creates another row vector w, but does not echo. To check that w has appeared in the MATLAB
workspace, type, >> who
Which will display all the variables in the MATLAB workspace, and to check the value of w simply
type w. Operations on row vectors can be best illustrated by some simple exercises.
One way to generate a column vector is to take the transpose of a row vector. Column vectors can be
typed in directly in one of two ways. For example, to enter the command vector
1
1
z
0
0
One way of creating matrices is by multiplying row and column vectors as seen in the above
exercises. There are various ways of entering matrices. For example, to enter the matrix
1 2
M
3 4
The most obvious ways are to type
>> M = [1 2; 3 4]; or >> M = [[1 3]’[2 4]’];
Built-in Functions
You have already encountered some built-in functions. In addition to standard arithmetic operators,
such as “+” and “*”, you have inv(A) that generates the inverse of matrix A, and eye(n) that
7
LAB-2
generates the n-by-n identity matrix. MATLAB has a very large number of built-in functions. To see
the full list of those variables on your machine type help, to get information on a specific function
(“inv” for example) type: help inv. The help command is very useful, most of the MATLAB built-in
functions have very descriptive names and are easily remembered or guessed.
Graphs
The results of signal processing computations in MATLAB are huge matrices containing, for
example, frequency responses, which one would like to display in graphical form. We will go
through a simple example of generating data, plotting the graph, putting labels on the axes etc.
We will plot the graph of y = sin(t), for t going from 0 to 20 seconds. First we need to generate a
vector t containing the values of time that we want to use. Type:
>> t=linspace(0,20,100);
It makes t into a row vector of 100 values from 0 to 20 inclusive, then type:
>> ys = sin(t);
To plot the set of points type:
>> Plot(t,ys)
The basic graph can be prettied up using the following self explanatory sequence of commands:
More than one set of points can be plotted on the same axes, and more than one graph can be
displayed on the screen at once. The following exercise leads you though this:
8
LAB-2
QUESTIONS:-
OBSERVATION:- Attach the prints of the plots you obtain in the exercises above.