You are on page 1of 50

MATLAB

MATLAB
Creating a vector
v = ( 2, 3, 4) € R3
>> v = [ 2, 3, 4]
To generate a vector
v = [ 2, 3, 4, 5, 6, 7]
>> v = 2 : 1 : 7
>> v = 2 : 2 : 7
Addition / subtraction of two vectors
>> v1 = [ 1, 3, 4 ]
>> v2 = [ 2, 5, 3 ]
>> v1 + v2
>> v1 – v2
Scalar multiplication of a vector
>> v = [ 2, 3, 4]
>> k = 10
>> k * v
ans =
20 30 40
Vector cross product. w = u x v
>> u = [2 4 7]
>> v = [10 20 30]
>>w = cross(u, v)

NOTE: u and v must be


3 element vectors
Try for
>> help dot
ML/244
Let p(t ) = 2 t3 + 5 t2 + t – 2
q(t ) = t3 + 3 t + 5

Find 1. p(t) + q(t)


2. 5 p(t)
3. 3 p(t) – 4 q(t)
>> p = [ 2, 5, 1, -2]
>> q = [ 1, 0, 3, 5]
>> p + q
ans = 3 5 4 3

>> 5 * p
>> 3 * p - 4 * q
randn : To generate a random scalar
>> randn
>> randn
>> randn
randn(m x n) :
To generate m x n matrix
with random entries
>> randn(4, 4)
>> randn(4)
fix( x ) : To round the element x to
the nearest integer towards 0
>> fix( 7.1)
ans =
7
>> fix( 7.9)
ans =
7
Ex: Let V = R3
Let W = { (2, a , b ) | a, b € R }
Is W a subspace of V ?

Sol:
>> a1 = randn
>> b1 = randn
>> x = [ 2, a1, b1 ]
>> a2 = randn
>> b2 = randn
>> y = [ 2, a1, b1 ]
>> x + y
>> 5 * x
Creating a Matrix

>>A=[11, 12, 13 ;
21, 22, 23 ;
31, 32, 33 ]
Let A be Matrix, Calculate A3
>> A^3

Let A = (aij)
Find matrix B = (bij) such that bij=(aij2)

>>B = A . ^ 2
Dot
Appending a Row or Column

>> [ A v ]
append column vector v to
columns of A
>> [ A ; u ]
append row vector u to
rows of A
Ex:
1 2 3

A= 4 5 6,
 
 7 8 9
11 
u = [10 20 
30 ], v = 22 
 
 33 
>>A = [1 2 3; 4 5 6; 7 8 9]
>>u = [ 10 20 30]
>>v = [11 22 33]’
>> [ A v ]

ans 1 2 3 11
4 5 6 22
7 8 9 33
>> [ A ; u ]
ans
1 2 3
4 5 6
7 8 9
10 20 30
Extract diagonal of matrix A
>>A=[ 1 2; 3 4]
>> diag(A)
ans
1
4
Generate a diagonal matrix with
v as diagonal
>> v = [1, 2]
>> diag(v)
ans
1 0
0 2
Addition of two matrices
>> A + B
Scalar multiplication of a matrix
>> 5 * A

rref(A) : To find Row Reduced


Echelon form of matrix A
>> rref(A)
ML/252: Let
S = { (1, 0, 0, 1), (0, 1, 1, 0), (1, 1,1,1)}
Check (0, 1, 1, 1) € LC(S)

SOL:
>> v1 = [1, 0, 0, 1]
>> v2 = [0, 1, 1, 0]
>> v3 = [1, 1, 1, 1]
>> v = [0, 1, 1, 1]
>> Ab = [v1’ , v2’, v3’, v’ ]
>> rref(Ab)
ML6/252: Let
S = {(1, 1, 0, 1), (0, -1, 0, 1), (0, 1,2,1)}
Check v = (2, 3, 2, 3) € Span S = [ S ]
>> v1 = [1, 1, 0, 1]
>> v2 = [1, -1, 0, 1]
>> v3 = [0, 1, 2, 1]
>> v = [2, 3, 2, 3]
>> Ab = [v1’ , v2’, v3’, v’ ]
>> rref(Ab)
If System is consistent, then v € [ S ]
Creating ZERO matrix
>>zeros(3, 2)
>>zeros(3)
Creating IDENTITY matrix
>>eye(3)
>>eye(3,2) !!!!!
ML1/262:
Let
S = {(1, 0, 0, 1), (0,1, 1, 0),(1, 1, 1, 1) }
Check S is LI or LD
>> v1 = [ 1, 0, 0, 1]
>> v2 = [ 0, 1, 1, 0]
>> v3 = [ 1, 1, 1, 1]
>> b = zeros(4, 1)
>> A = [ v1’, v2’, v3’]
>> Ab = [ A, b]
>> rref(Ab) OR >>rref([A, b])
If Rank(A) = Rank(Ab), then LI
ML7/274
Let S = { (1, 1, 0, 0), (-2, -2, 0, 0)
(1, 0, 2, 1), ( 2, 1, 2, 1)
(0, 1, 1, 1) }
Find dim (Span S )
>> v1 = [ 1, 1, 0, 0]’
>> v2 = [ -2, -2, 0, 0]’
>> v3 = [ 1, 0, 2, 1]’
>> v4 = [ 2, 1, 2, 1]’
>> v5 = [ 0, 1, 1, 1]’
>> b = zero(4, 1)
>> rref( [ v1 , v2, v3, v4 , v5, b] )
Vectors corresponding to leading 1’s
will be basis of [ S ]
ML5/294
Check the consistency for

 x1 
1 2 4 − 1 x   21 
0 1 2 0    =  8
2
   x3   
 3 1 1 − 2     16 
x
 4
>>A = [ 1 2 4 -1;
0 1 2 0;
3 1 1 -2 ] ;
>> b = [ 21, 8, 16 ]
>> rank (A) - rank( [A , b] )

If output is 0
Then system is consistent
ML6/306: Let V = R4
S={(1, 2, 3, 0), (0, 1, 2, 3),
(3, 0, 1, 2), (2, 3, 0, 1) }
T={ e1 , e2 , e3 , e4 }

Find Transition matrix PS T


>>A = [1, 2, 3, 0; 0, 1, 2, 3;
3, 0, 1, 2; 2, 3, 0, 1 ]’
>> B = eye(4) (WHY)
>> rref( [A, B] )
>>P = ans(: , 5 : 8)
Transpose
ML2/467
Let L : R3 R4  1
2
2 0 
1 −1
L( v ) = C v, C = 
3 1 0 
 
−1 0 2 

Find matrix of L with respect to bases


 1  2 0 
      
S =s1 = 0 , s 2 = 0 , s 3 = 1 
     
 1  1  2 
      
and

 1  1  0   0 
 1  1  1   0 
 
T =  w1 =   , w 2  
= , w3 =   ,w4 =   
 1  1  1  1  
        
 2  0  − 1  0 
>>C=[ 1 2 0; 2 1 -1; 3 1 0; -1 0
2]
>>v1=[1 0 1]’
>>v2=[2 0 1]’
>>v3=[0 1 2]’
>>w1 = [1 1 1 2]’
>>w2 = [1 1 1 0]’
>>w3 = [0 1 1 -1]’
>>Lv1 = C * v1
>>Lv2 = C * v2
>>Lv3 = C * v3
>>M=[ w1 w2 w3 w4
Lv1 Lv2 Lv3 ]
>>rref( M )
>>A = ans(: , 5 : 7 )
ML1/452: L(x) = A x be a LT
Find basis of Ker L , Range L

 1 2 5 5
A=  
− 2 − 3 − 8 − 7
>> A = [1, 2, 5, 5; -2, -3, -8, -7]
>>rref( A ) further simplify to get
basis of ker L
>>rref ( A’ )’

Each non-zero column of


this matrix is a basis of Range L
Definition:
Let A = (aij)n x n
Then Characteristic Polynomial of
matrix A is
det( A – λ I ) = 0
ML1/355
Find Characteristic polynomial of A
1 2
A=  
2 − 1
>>poly(A)
>>roots( poly(A) )
verify
>>sqrt(5)
Ex:Find eigen values and eigen vectors
for the matrix
1 1 3

A = 1 5 1 
 3 1 1 

Sol: Characteristic equation


| A - λ I | =0
(λ+2)(λ-3)(λ-6)=0
Eigen values λ = -2, λ = 3, λ = 6
Let X = [ x, y, z ] be eigen vector
corresponding to λ
Then ( A – λ I ) X = 0

1 − λ 1 3  x 
1 5−λ 1    y = 0
  
3 1 1 − λ   z 
For λ = -2
We have
3 x + y + 3z = 0
x + 7y + z = 0
3x + y + 3z =0
Hence x = -1, y = 0, z = 1
X1 = ( -1, 0, 1 ) is eigen vector
corresponding to λ = -2
Eigen vector corresponding to λ = 3 is
(1, -1, 1)

Eigen vector corresponding to λ = 6 is


(1, 2, 1)
NOTE:
All non-zero multiple of X1 is an eigen
vector corresponding to λ = -2
Ex:
Find eigen values and eigen vectors
for the matrix
− 9 4 4

A = − 8 3 4 
 − 16 8 7 
Sol: Characteristic equation
| A - λ I | =0
(λ+1)(λ+1)(λ-3)=0
Eigen values λ = -1, λ = -1, λ = 3
Let X = [ x, y, z ] be eigen vector
corresponding to λ
Then ( A – λ I ) X = 0

− 9 − λ 4 4 x 
− 8 3−λ 4  y = 0
  
− 16 8 7 − λ   z 
For λ = 3
We have
12 x + 4y + 4z = 0
-8x + 0 y + 4z = 0
-16 x + 8y + 4 z = 0
Hence -x + y = 0
-2y + z = 0
Hence x = y = z / 2
Let z = 2
X1 = ( 1, 1, 2 ) is eigen vector
corresponding to λ =3
For λ = -1
We have
-8 x + 4y + 4z = 0
-8x + 4 y + 4z = 0
-16 x + 8y + 8 z = 0
Hence -2x + y + z = 0
Hence Eigen space of λ = -1 is
{ (x, y, z) | -2x + y + z = 0 }
= { (x, y, z) | y = 2 x - z }
= { (x, 2 x - z, z) | x, z € R }
= { (x, 2 x, 0) + ( 0, -z, z ) | x, z € R }
= { x(1, 2, 0) + z( 0, -1, 1 ) | x, z €
R}
= [ { (1, 2, 0), ( 0, -1, 1 ) } ]
Hence set of eigen vectors are
{ (1, 2, 0), ( 0, -1, 1 ) } – { ( 0, 0, 0, )}

You might also like