Lab Manual 1
Lab Manual 1
Lab Manual 1
Lab 01
Lab Title: INTRODUCTION TO MATLAB.
Objective:
Tool Used:
Matlab.
Description:
Strengths of MATLAB
MATLAB Desktop
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.
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.3
4. Array Editor:
Fig 1.4
5. Current Directory Browser:
It lists all the files that are available in current directory.
Fig 1.5
Reserved Words:
end
else if
else
persistent
if
case
try
break
while
otherwise
Symbol
+
*
/ or \
Example
3 + 22
54.4 16.5
3.14 * 6
10/2 or 2\10
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.
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
Extras:
Using up-arrow key allow user to recall most recently used commands
Another trick is to use a letter prior using up-arrow
:
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.
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 =
1 2 3
4 5 6
7 8 9
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
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:
1
4
7
2
5
8
3
6
9
col_two = 2
5
8
matrix=[1,2,3;4,5,6;7,8,9]
matrix =
1
4
7
2
5
8
3
6
9
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
3 4 3 4 3 4
[a ; a; a]
ans = 1 2
3 4
1 2
3 4
1 2
3 4
ans = 1 2 0 0
3 4 0 0
0 0 1 3
0 0 2 4
4 5 6
c= b+a
c=
4 5 6
7 8 9
6 9
15 18
min(a) :Return a row vector containing the minimum element from each column.
ans =1 2 3
max(a) : Return a row vector containing the maximum element from each
column.
ans =7 8 9
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=3 c=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];
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:
Example;
>> zeros(2)
ans =
0
0
>> zeros(1,2)
ans =
0
0
Matrix of Ones:
EXERCISE:
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.6 Extracting and/or inserting numbers into a vector is 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
Now write a statement that will take the vector xx defined in part (b) and replace the even
indexed elements (i.e., xx(2), xx(4), etc) with the constant . Use a vector replacement,
not a loop.
A=
6
3
,B=
4
1
6
6
2