Anda di halaman 1dari 8

 

The Islamia University of Bahawalpur 
University College of Engineering &Technology 
 
 
 
SIGNALS AND SYSTEMS LAB‐1 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
EXPERIMENT # 01: INTRODUNCTION TO MATLAB 
                         
 
Name of Student:  __________________________________ 
Roll No.: _________________________________________ 
DATE:  __________________________________________ 
 
 
Instructor’s Signature: ……………………………... 
 
Instructor : Engr. Abdul Rehman Chishti 
 
Department of Telecommunication Engineering 
 
Introduction to MATLAB 
 
1. Objectives 
 
ƒ Familiarities with MATLAB interface.  
ƒ Understanding basic features of MATLAB. 
ƒ Basic programming techniques. 
 
2. Overview 
MATLAB  developed  by  The  MathWorks  Inc.  stands  for  MATrix  LABoratory.  It  is  a 
software  package  used  to  perform  scientific  computations  and  visualization.  Its  capability  for 
analysis of various scientific problems, flexibility and powerful graphics makes it a very useful 
software package. It provides an Integrated Development Environment (IDE) for programming 
with  numerous  predefined  functions  for  technical  computations  and  visualization.  Besides 
available built‐in functions, user‐defined functions can also be included which can be used just 
like any other built‐n function. 
 
2.1 Getting Started 
 

 
 
If you don’t know anything about MATLAB, then MATLAB help is the best way to learn 
about something. In the command window of MATLAB simply write ‘help’; 
>> help ↵ 
It  will  display  list  of  all  toolboxes  included  in  MATLAB.  Then  by  investigating  the  name  of 
toolbox or the name of a function, which you would like to learn how to use, use the ‘help’ 
command: 
 
>> help functionname ↵ 

Signals and Systems Lab (EE-2103) 2


This  command  displays  a  description  of  the  function  and  generally  also  includes  a  list  of 
related  functions.  If  you  cannot  remember  the  name  of  the  function,  use  the  ‘lookfor’ 
command and the name of some keyword associated with the function: 
 
2.2 MATLAB variables; Scalars, Vectors and Matrices 
 
MATLAB stores variables in the form of matrices, which are M x N,  
where M is the number of rows and N the number of columns.  
A 1x1 matrix is a scalar; a 1xN matrix is a row vector, and Mx1 matrix is a column vector. 
 
>> x=[ 1 2 3 4]; 
>> y= 8; 
>> z=x*y 
 
z =    8    16    24    32 
To look at the transpose of the previous result, enter the following (‘ is the transpose 
operator): 
 z’   
  8 
  16 
  24 
  32 
 
 All elements of a matrix can be real or complex numbers; √‐1 can be written as either ‘i’ 
or ‘j’ provided the user does not redefine them. A matrix is written with a square bracket ‘[ ]’ 
with  spaces  separating  adjacent  columns  and  semicolons  separating  adjacent  rows.  For 
example, consider the following assignments of the variable x 
 
Row vector >> x = [1 2 3] (or x = [1, 2, 3]) 
Column vector >> x = [1; 2; 3] 
3 x 3matrix >> x = [1 2 3; 4 5 6; 7 8 9] 
 
 
2.3 Complex number operations 
 
Some of the important operations on complex numbers are illustrated below:  
 
x = 3+4j 
 
Real part of x >> real(x) = 3 
Imaginary part of x >> imag(x) = 4 
Magnitude of x >> abs(x) = 5 
Angle of x >> angle(x) = 0.9273 
Complex conjugate of x >> conj(x) = 3 ‐ 4i 

Signals and Systems Lab (EE-2103) 3


 
 
There  are  a  few  notes  of  caution.  Complex  elements  of  a  matrix  should  not  be  typed  with 
spaces,  i.e.,  ‘‐1+2j’  is  fine  as  a  matrix  element,  ‘‐1  +  2j’  is  not.  Also,  ‘‐1+2j’  is  interpreted 
correctly whereas ‘‐1+j2’ is not (MATLAB interprets the ‘j2’ as the name of a variable. You can 
always write  
‘‐1+j*2’. 
 
2.4 Generating Vectors: 
 
Vectors  can  be  generated  using  the  ‘:’  command.  For  example,  to  generate  a  vector  x  that 
takes on the values 0 to 10 in increments of 0.5, type the following, which generates a 1 x 21 
matrix? 
>> x = [0:0.5:10]; 
 
x=0:12 
x =     0     1     2     3     4     5     6     7     8     9    10    11    12 
 
Other  ways  to  generate  vectors  include  the  commands:  ‘linspace’  which  generates  a 
vector  by  specifying  the  first  and  last  number  and  the  number  of  equally  spaced  entries 
between the first and last number, 
 
 LINSPACE Linearly spaced vector. 
    LINSPACE(X1,  X2)  generates  a  row  vector  of  100  linearly  equally  spaced  points 
between X1 and X2. 
     LINSPACE(X1, X2, N) generates N points between X1 and X2. 
     
linspace (1,10, 10) 
ans =    1     2     3     4     5     6     7     8     9    10 
  
 
2.5 Accessing Vector Elements 
 
Specifying the row and column accesses elements of a matrix. For example, in the matrix 
specified  by  A  =  [1  2  3;  4  5  6;  7  8  9],  the  element  in  the  first  row  and third column can be 
accessed by writing  
>> x = A(1,3) which yields 3 
 
The entire second row can be accessed with 
>> y = A(2,:) which yields [4 5 6] 
Where the ‘:’ here means “take all the entries in the column”. A sub‐matrix of A consisting of 
rows 1 and 2 and all three columns is specified by 
>> z = A(1:2,1:3) which yields [1 2 3; 4 5 6] 
 

Signals and Systems Lab (EE-2103) 4


A= [ 1 2 3 4 5 6] 
>>A(1) 
ans =     1 
>> A(1,1) 
ans =     1 
A(1,5) 
ans =     5 
 
A = [ 1 2 3; 4 5 6; 7 8 9]; 
 
     1     2     3 
     4     5     6 
     7     8     9 
 
>> A(2,2) 
 
ans =     5 
 
>> A(3,2) 
 
ans =     8 
 
a=[1 2 ; 3 4] 
 
a =1     2 
     3     4 
 
a(1,:) 
ans =     1     2 
 
a(2,:) 
ans =     3     4 
 
 
2.6 Matrix Operations 
 
MATLAB contains a number of arithmetic, relational, and logical operations on matrices. 
 
2.6.1 Arithmetic Operations 
 
The basic arithmetic operations on matrices (and of course scalars, which are special cases 
of matrices) are: 
 
 

Signals and Systems Lab (EE-2103) 5


 
+  Addition 
‐  Subtraction 
*  Multiplication 
/  Right division 
\  Left division 
Exponentiation 

(power) 
‘  Conjugate transpose
 
 
>> x=[ 1 2 3 4]; 
>> y= 8; 
>> z=x*y 
 
z =     8    16    24    32 
 
>> whos 
  Name      Size                    Bytes  Class 
 
  x         1x4                        32  double array 
  y         1x1                         8  double array 
  z         1x4                        32  double array 
 
Grand total is 9 elements using 72 bytes 
 
We often want to take the sum or product of a sequence of numbers. 
We use MATLAB's sum command: 
>>X=1:7 
X = 
1 2 3 4 5 6 7 
>>sum(X) 
ans =28 
 
>>prod(X) 
ans =5040 
The difference between matrix multiplication and element‐by‐element multiplication is seen in 
the following example 
 
>>A = [1 2; 3 4] 

Signals and Systems Lab (EE-2103) 6


A = 
1 2 
3 4 
 
>>B=A*A 
B = 
7   10 
15 22 
 
>>C=A.*A 
C = 
1 4 
9 16 
 
PRACTICE PROBLEMS 
 
Problem.1   The variable x, y, z have values equal to 2.5, 0.5 and 2 respectively. 
Evaluate the following MATLAB expressions.. 
 
 (i). x + y + z   (ii). x * y* z   (iii). x/y   (iv). xy     (v). xz 
 
Problem.2  (a). Suppose vector is defined as A= [5 6 9 2 14 3]. Explain what happens 
if we write: 
 
A (3) , A (1:3) , A(3:5)    
(b). v=[1:2:7], find v(1:3), v(2,4), v(1:3)‐ v(2,4) 
 
Problem.3  Show the output of the following commands 
 
x = 0 : 6 
y = 2 : 4 : 17 
z = 99 : ‐1 : 88 
w= 2 : (1/9) : 4 
v = pi * [ 0:0.1:2 ]; 
 
Problem.4  The complex variables are x=3 ‐4i and y= 1 + 2i. Evaluate the following 
expressions. 
 
 (i). z= x + y (ii). x‐y (iii) x*z (iv) x/y (v) xy 
       (vi) Magnitude, Phase, real, imaginary part and complex conjugate of z 
    
 
Problem.5    Evaluate the following   
 

Signals and Systems Lab (EE-2103) 7


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 ]) 
 
  Problem.6  Extracting  and/or  inserting  numbers  into  a  vector  is  very  easy  to  do. 
Consider the following definition of k: 
 
ƒ k = [ zeros(1,3), linspace(0,1,5), ones(1,4) ] 
ƒ k(4:6) 
ƒ size(k) 
ƒ length(k) 
ƒ k(2:2:length(k)) 
   
Problem .7  Enter the following in MATLAB. 
 
⎡ 2 3⎤ ⎡1 6 ⎤
A =  ⎢ ⎥ , B =  ⎢ ⎥ 
⎣6 4⎦ ⎣6 2⎦
Find AB, and A‐1.n 
 
 

Signals and Systems Lab (EE-2103) 8

Anda mungkin juga menyukai