0% found this document useful (0 votes)
102 views

Lecture8 Linearalg Matlab2 PDF

The document provides an overview of linear algebra concepts involving matrices and vectors, including: - Matrices can represent systems of equations, with each row of the matrix corresponding to an equation. - Vectors are represented as matrices with a single column or row. - Operations like addition and subtraction on matrices and vectors follow the same rules as ordinary numbers, working element-by-element. - Matrix multiplication follows specific rules about the dimensions of the matrices, and is not commutative. Vector multiplication can produce either a scalar (inner product) or a matrix (outer product) depending on whether a row and column vector are multiplied.

Uploaded by

ud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Lecture8 Linearalg Matlab2 PDF

The document provides an overview of linear algebra concepts involving matrices and vectors, including: - Matrices can represent systems of equations, with each row of the matrix corresponding to an equation. - Vectors are represented as matrices with a single column or row. - Operations like addition and subtraction on matrices and vectors follow the same rules as ordinary numbers, working element-by-element. - Matrix multiplication follows specific rules about the dimensions of the matrices, and is not commutative. Vector multiplication can produce either a scalar (inner product) or a matrix (outer product) depending on whether a row and column vector are multiplied.

Uploaded by

ud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Modified

 Gary  Larson  Far  Side  cartoon  


Matlab  

Linear  Algebra  Review  


Matrices  can  represent  sets  of  
equa>ons!  
a11x1+a12x2+…+a1nxn=b1  
a21x1+a22x2+…+a2nxn=b2  
   …  

am1x1+am2x2+…+amnxn=bm  

What’s  the  matrix  representa>on?  


⎡ a11 a12 ... a1n ⎤ ⎡ x1⎤ ⎡ b1 ⎤
⎢ ⎥ ⎢ ⎥
Ax = b
⎢ ⎥ x2⎥ b2 ⎥
⎢ a21 a22 ... a2n ⎥ ⎢ ⎢
A= x= b=
⎢ ... ⎥ ⎢ . ⎥ ⎢ . ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎣am1 am2 ... amn ⎦ ⎣ xn⎦ ⎣bm ⎦
Vectors  
Matrices  

u11              u12    …              u1n  


u21              u22    …              u2n  
U  =  [umn]  =            …  
um1    um2    …    umn  

The  general  matrix  consists  of  m  rows  and  n  columns.    It  is  also  
known  as  an  m  x  n  (read  m  by  n)  array.  

Each  individual  number,  uij,  of  the  array  is  called  the  element  

Elements  uij  where  i=j  is  called  the  principal  diagonal  


Transpose  of  a  Matrix  
Matrix  &  Vector  Addi>on  

Vector/Matrix  addi>on  is  associa>ve  and  commuta>ve  


(A  +  B)  +  C  =  A  +  (B  +  C);          A  +  B  =  B  +  A  
Matrix  and  Vector  Subtrac>on  

Vector/Matrix  subtrac>on  is  also  associa>ve  and  commuta>ve  


(A  -­‐  B)  -­‐  C  =  A  -­‐  (B  -­‐  C);          A  -­‐  B  =  B  -­‐  A  
Matrix  and  Vector  Scaling  
•  For  addi>on  and  subtrac>on,  the  size  of  the  
matrices  must  be  the  same  
 Amn  +  Bmn  =  Cmn    

•  For  scalar  mul>plica>on,    


the  size  of  Amn  does  not  ma^er  

•  All  three  of  these  opera>ons  do  not  differ  


from  their  ordinary  number  counterparts  

•  The  operators  work  element-­‐by-­‐element  


through  the  array,    aij+bij=cij  
Vector  Mul>plica>on  
•  The  inner  product  or  dot  product  

v ⋅ w = (x1, x 2 ) ⋅ (y1, y 2 ) = x1 y1 + x 2 y 2

The  inner  product  of  vector  mul>plica>on  


is  a  SCALAR  

v ⋅ w = (x1, x 2 ) ⋅ (y1, y 2 ) =|| v ||⋅ || w || cos α
Inner  product  represents  a  row  matrix  mul>plied  by  a  
column  matrix.  A  row  matrix  can  be  mul>plied  by  a  
column  matrix,  in  that  order,  only  if  they  each  have    
the  same  number  of  elements!  
In  MATLAB,  in  order  to  properly  calculate  
the  dot  product  of  two  vectors  use  

>>sum(a.*b)  

element  by  element  mul>plica>on  (.*)  


sum  the  results  

A  .  prior  to  the  *  or  /  indicates  


that  matlab  should  perform  the  array  or  
element  by  element  calcula>on  rather  
than  linear  algebra  equivalent  

or  

>>a’*b  
•     The  outer  product  

A  column  vector  mul>plied  by  a  row  vector.  

In  Matlab:  

>>a*b’  

ans  =  
The  outer  product  of  vector          24          6        30  
mul>plica>on            8          2        10  
is  a  MATRIX        -­‐12        -­‐3      -­‐15  
Matrix  Mul>plica>on  

The  inner  numbers  have  to  match  

Two  matrices  can  be  mul>plied  together    


if  and  only  if    
the  number  of  columns  in  the  first  equals    
the  number  of  rows  in  the  second.  
B     C   A  

a21   a22  

In  MATLAB,  the  *  symbol  represents    


matrix  mul>plica>on  :        
>>A=B*C  
•     Matrix  mul>plica>on  is  not  commuta>ve!  

C*B      #taken  from  previous  slide                                  ans  =  


                           19        18        17  
                           16        24        32  
                           32        24        16  

•     Matrix  mul>plica>on  is  distribu>ve  and  associa>ve  

A(B+C)  =  AB  +  BC  

(AB)C  =  A(BC)  
Revisit  the  vector  example  

>>a'*b   >>a*b’  

ans  =   ans  =  

       11          24          6        30  


         8          2        10  
     -­‐12        -­‐3      -­‐15  
a1x3  *  b3x1  =  c1x1  
a3x1  *  b1x3  =  c3x3  
Dot  products  in  Matlab  
(using  this  form  –  built  in  func>ons  -­‐  don’t  have  to  match  dimensions  of  vectors  –  can  mix  
column  and  row  vectors  –  although  they  have  to  be  the  same  length)  

>> a=[1 2 3];!


>> b=[4 5 6];!

>> c=dot(a,b)!
c =!
32!

>> d=dot(a,b’)!
d =!
32!
Dot  products  using  built-­‐in  func>on  
For  matrices  –  does  dot  product  of  columns.  

Essen>ally  trea>ng  the  columns  like  vectors.  


The  matrices  have  to  be  the  same  size.  
>> a=[1 2;3 4]!
a =!
1 2!
3 4!

>> b=[5 6;7 8]!


b =!
5 6!
7 8!

>> dot(a,b)!
ans =!
26 44!
Determinant  of  a  Matrix  
or  follow  the  diagonals  

det  (A)  =  a11a22a33+  a12a23a31  +  a13a21a32  −  a11a23a32  −  a12a21a33    −  a13a22a31  


Cross-­‐product  of  two  vectors  
The  cross  product  a  ×  b  is  defined  as  a  vector  c  that  is  
perpendicular  to  both  a  and  b,  with  a  direc>on  given  by  the  
right-­‐hand  rule  and  a  magnitude  equal  to  the  area  of  the  
parallelogram  that  the  vectors  span.  

a × b = a b sin θ

c  =      a2b3-­‐a3b2      =      2*5  –  (-­‐3)*1          =        13  

€   a3b2-­‐a1b3                -­‐3*4  –  6*5                            -­‐42  

  a1b2-­‐a2b1                6*1  –  2*4                                -­‐2  


Cross  products  in  Matlab  
(using  this  form  –  built  in  func>ons  -­‐  don’t  have  to  match  dimensions  of  vectors  –  can  mix  
column  and  row  vectors  –  although  they  have  to  be  the  same  length)  

>> a=[1 2 3];!


>> b=[4 5 6];!

>> e=cross(a,b)!
e =!
-3 6 -3!

>> f=cross(a,b’)!
f =!
-3 6 -3!

>> g=cross(b,a)!
g =!
3 -6 3!
For  matrix  –  does  cross  product  of  columns.    
(one  of  the  dimensions  has  to  be  3  and  takes  other  dimension  as  addi>onal  vectors)  

>> a=[1 2;3 4;5 6]!


a =!
1 2!
3 4!
5 6!

>> b=[7 8;9 10;11 12]!


b =!
7 8!
9 10!
11 12!

>> cross(a,b)!
ans =!
-12 -12!
24 24!
-12 -12!
Matrix  Operators  
•  +  Addi>on  
•  -­‐  Subtrac>on  
•  *  Mul>plica>on  
•  /  Division  
•  \  Let  division    
•  ^  Power  
•  '  Complex  conjugate  transpose  
•  (  )  Specify  evalua>on  order  
Array  Operators  
•  +  Addi>on  
•  -­‐  Subtrac>on  
•  .*  Element-­‐by-­‐element  mul>plica>on  
•  ./  Element-­‐by-­‐element  division.      
•  A./B:  divides  A  by  B  by  element  
•  .\  Element-­‐by-­‐element  let  division  
•  A.\B      divides  B  by  A  by  element    
•  .^  Element-­‐by-­‐element  power  
•  .'  Unconjugated  array  transpose  
•  the  signs  of  imaginary  numbers  are  not  changed,  unlike  a  
regular  matrix  transpose  
Operators as built-in commands

plus - Plus +
uplus - Unary plus +
minus - Minus -
uminus - Unary minus -
mtimes - Matrix multiply *
times - Array multiply .*
mpower - Matrix power ^
power - Array power .^
mldivide - Backslash or left matrix divide \
mrdivide - Slash or right matrix divide /
ldivide - Left array divide .\
rdivide - Right array divide ./
cross - cross product
Mul>plica>on  in  Matlab  

>>  x=[1  2];  


>>  y=[3  4];  

>>  z=x*y’  
z  =  
Regular matrix multiplication – in this case
       11   with vectors 1x2 * 2x1 = 1x1 => dot product
>>  w=x.*y  
w  =   Element by element multiplication
         3          8  

>>  z=x'*y  
z  =   Regular matrix multiplication – in this case
         3          4  
         6          8   with vectors 2x1 * 1x2 = 2x2 matrix
Division  in  Matlab  
In  ordinary  math,  division  (a/b)  can  be  thought  of  as  a*1/b  or  a*b-­‐1.      

A  unique  inverse  matrix  of  B,  B-­‐1,  only  poten>ally  exists  if  B  is  square.      
And  matrix  mul>plica>on  is  not  communica>ve,  unlike  ordinary  
mul>plica>on.      
There  really  is  no  such  thing  as  matrix  division  in  any  simple  sense.  

/  :  B/A  is  roughly  the  same  as  B*inv(A).  A  and  B  must  have  
   the  same  number  of  columns  for  right  division.      
\  :      If  A  is  a  square  matrix,  A\B  is  roughly  the  same  as  
   inv(A)*B,  except  it    is  computed  in  a  different    
     way.  A  and  B  must  have  the  same  number  of    
     rows  for    let  division.  

 If  A  is  an  m-­‐by-­‐n  matrix  (not  square)  and  B  is  a  matrix  
of  m  rows,  AX=B  is  solved  by  least  squares.    
The  /  and  \  are  related  
B/A  =  (A'\B')’    
Inverse  of  a  Matrix  
the  principal  diagonal  elements  switch  
the  off  diagonal  elements  change  sign  

the  determinant  
•  Square  matrices  with  inverses  are  said  to  be  
nonsingular  

•  Not  all  square  matrices  have  an  inverse.    


These  are  said  to  be  singular.  

•  Square  matrices  with  determinants  =  0  are  


also  singular.  

•  Rectangular  matrices  are  always  singular.  


Right-­‐  and  Let-­‐  Inverse    
If  a  matrix  G  exists  such  that  GA  =  I,  than  G  is  a  
let-­‐inverse  of  A  

If  a  matrix  H  exists  such  that  AH  =  I,  than  H  is  a  


right-­‐inverse  of  A  

Rectangular  matrices  may  have  right-­‐  or  let-­‐  


inverses,  but  they  are  s>ll  singular.  
Some  Special  Matrices  
•  Square  matrix:  m  (#  rows)  =  n  (#  columns)  

•  Symmetric  matrix:  subset  of  square  matrices  


where  AT  =  A    

•  Diagonal  matrix:  subset  of  square  matrices  where  


elements  off  the  principal  diagonal  are  zero,  aij  =  
0  if  i  ≠  j  

•  Iden>ty  or  unit  matrix:  special  diagonal  matrix  


where  all  principal  diagonal  elements  are  1  
Linear  Dependence  

a            b              c  

2a  +  1b  =  c  
Linear  Independence  

a            b              c  

There  is  no  simple,  linear  equa>on  that  can  make  


these  vectors  related.      
Rank  of  a  matrix  
Acknowledgement    
•  This  lecture  borrows  heavily  from  online  
lectures/ppt  files  posted  by  
•  David  Jacobs  at  Univ.  of  Maryland  
•  Tim  Marks  at  UCSD  
•  Joseph  Bradley  at  Carnegie  Mellon  

You might also like