0% found this document useful (0 votes)
8 views23 pages

Matlab 2

This document provides a tutorial on basic matrix operations and force analysis in Matlab. It defines vectors, matrices, and their basic properties such as addition and multiplication. It also discusses operations like the transpose, norm, and dot product of vectors. Examples are provided for calculating the angle and projection between two vectors, as well as the cross product and moment of forces. The document aims to introduce fundamental matrix concepts and their application to force analysis using Matlab.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views23 pages

Matlab 2

This document provides a tutorial on basic matrix operations and force analysis in Matlab. It defines vectors, matrices, and their basic properties such as addition and multiplication. It also discusses operations like the transpose, norm, and dot product of vectors. Examples are provided for calculating the angle and projection between two vectors, as well as the cross product and moment of forces. The document aims to introduce fundamental matrix concepts and their application to force analysis using Matlab.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Matlab

 Tutorial  for  Basic  Matrix  


Operations  and  Force  Analysis  
Quan  Wang,  ECSE,  RPI  
IEA  Graduate  TA  for  Section  3  
[email protected]  
 
My  Office  Hours  

* Location:    JEC  2001  


* Monday:      4:00  –  5:00  
* Tuesday:    1:00  –  5:00  
* Friday:      1:00  –  4:00  
Row  Vector  

* Use  space  or  comma  to  separate  elements  


* E.g.  
>> A=[1 2 3]!
!
A =!
!
1 2 3!
!
>> B=[4,5,6]!
!
B =!
!
4 5 6!
Column  Vector  

* Use  semicolon  to  separate  elements  


* E.g.  
!
>> C=[7;8;9]!
!
C =!
!
7!
8!
9!
Matrix  

* Matrix   can   be   viewed   as   a   column   vector   of   row  


vectors  
* Use   semicolon   to   separate   rows,   and   use   space   or  
comma  to  separate  elements  in  a  row  
* E.g.  
!
>> D=[1 2 3;4,5,6]!
!
D =!
!
1 2 3!
4 5 6!
Vector  and  Matrix  

* Row  vector  is  a  special  case  of  matrix:  only  one  row  

* Column   vector   is   a   special   case   of   matrix:   only   one  


column  

* Scalar   is   also   a   special   case   of   matrix:   only   one   row  


and  only  one  column  
Element  of  Matrix  

* If   A   is   a   matrix,   then   A(i,j)   is   the   element   in   the   ith   row  


and  jth  column  
* E.g.  
>> A=[1 2 3 4;5 6 7 8;1 3 5 7]!
!
A =!
!
1 2 3 4!
5 6 7 8!
1 3 5 7!
!
>> A(3,2)!
!
ans =!
!
3!
Submatrix  

* If   A   is   a   matrix,   then   A(i:j,m:n)   is   the   submatrix   of   A,  


which  is  the  intersection  of  ith  row  to  jth  row  of  A  and  
mth  column  to  nth  column  of  A  (colon  means  “to”)  
* E.g.   >> A=[1 2 3 4;5 6 7 8;1 3 5 7]!
!
A =!
!
1 2 3 4!
5 6 7 8!
1 3 5 7!
!
>> A(2:3,1:2)!
!
ans =!
!
5 6!
1 3!
Basic  Operations  of  Matrix  

* Addition:  A+B  
* Subtraction:  A-­‐B  
* Scalar  multiplication:  a*A  
* Element  by  element  multiplication:  A.*B  
* Matrix  multiplication:  A*B  
* Power  of  matrix:  A^n  
* Determinant  of  matrix:  det(A)  
Example  

* Use   Matlab   to   verify   that   AB=BA   is   not   necessarily  


true  
* Observe  the  difference  between  A.*B  and  A*B  
* E.g.  Use  
A =!
1 2!
3 4!
!
B =!
4 3!
2 1!
Example  

>> A=[1 2;3 4]! >> A*B!


A =! !
1 2! ans =!
3 4! 8 5!
! 20 13!
>> B=[4 3;2 1]! !
! >> B*A!
B =! !
4 3! ans =!
2 1! 13 20!
! 5 8!
! !
! >> A.*B!
! !
! ans =!
! 4 6!
! 6 4!
!
!
!
Transpose  

* Transpose  of  A:  A’  


* Transpose   of   a   row   vector   is   a   column   vector,   vice  
versa  
>> A=[1 2;3 4;5 6]! >> u=[1 2 3]!
! !
A =! u =!
! !
1 2! 1 2 3!
3 4! !
5 6! >> u'!
! !
>> A'! ans =!
! !
ans =! 1!
! 2!
1 3 5! 3!
2 4 6!
Norm  and  Size  

* norm()  computes  the  magnitude  of  a  vector  


* size()  gives  the  dimensionality  of  a  matrix  
* E.g.  
>> u=[1;1]! >> A=[1 2 3;4 5 6]! >> size(A)!
! ! !
u =! A =! ans =!
! ! !
1! 1 2 3! 2 3!
1! 4 5 6!
! !
>> norm(u)! >> size(u)!
! !
ans =! ans =!
! !
1.4142! 2 1!
! !
Dot  (Inner)  Product  of  Vectors  

* dot(u,v)  
* If   both   u   and   v   are   column   vectors,   we   can   also   use  
matrix  multiplication:  u’*v  
>> u=[1;2;3]! >> dot(u,v)!
! !
u =! ans =!
! !
1! 10!
2! !
3! >> u'*v!
! !
>> v=[3;2;1]! ans =!
! !
v =! 10!
!
3!
2!
1!
Example:  Angle  and  Projection  

* u=(2,0,0),  v=(3,3,3)  
* Use  Matlab  to  compute  
1. The  angle  between  u  and  v  in  degrees  
2. The  projection  of  v  on  u  

!!!
!"#! ! ! !
! !

!!!
!"#!! ! ! !!
! !
Example:  Angle  and  Projection  

>> u=[2;0;0]! >> angle=acosd(dot(u,v)/(norm(u)*norm(v)))!


! !
u =! angle =!
! !
2! 54.7356!
0! !
0! >> proj_v_on_u=dot(u,v)/norm(u)^2*u!
! !
>> v=[3;3;3]! proj_v_on_u =!
! !
v =! 3!
! 0!
3! 0!
3!
3!
!
Cross  Product  and  Moment  

* cross(u,v)  computes  the  cross  product  of  u  and  v  

* E.g.    
* F=(3,1,1)   is   a   force   on   point   B,   the   vector   from   B   to  
point   A   is   (2,0,0),   what   is   the   moment   of   F   about  
point  A?    
Cross  Product  and  Moment  

* F=(3,1,1)   is   a   force   on   point   B,   the   vector   from   B   to  


point   A   is   (2,0,0),   what   is   the   moment   of   F   about  
point  A?    
>> F=[3,1,1]!
!
F =!
!
3 1 1!
!
>> B_A=[2,0,0]!
!
B_A =!
!
2 0 0!
!
>> moment=cross(F,B_A)!
!
moment =!
!
0 2 -2!
Cross  Product  and  Moment  

* P=(2,3,1),  Q=(4,0,3),  R=(6,1,0),  what  is  P×(Q×R)?    


>> P=[2 3 1]!
!
P =!
!
2 3 1!
!
>> Q=[4 0 3]!
!
Q =!
!
4 0 3!
!
>> R=[6 1 0]!
!
R =!
!
6 1 0!
!
>> cross(P,cross(Q,R))!
!
ans =!
!
-6 -11 45!
Linear  Equations  

>> A=[1 3;2 1]!


!

* x1+3x2=9   A =!
!
1 3!
* 2x1+x2=8   !
2 1!

>> b=[9;8]!
!
b =!
!
* First  method:     9!
8!

A=[1  3;  2  1],  x=[x1;  x2],  b=[9;  8]  


!
* >> x=inv(A)*b!
!

Equations:  Ax=b  
x =!
* !
3!

* Solution:  x=A-­‐1b   !
2!

>> x=A^(-1)*b!
!
x =!
!
3!
2!
Linear  Equations  

* x1+3x2=9  
>> B=[1 3 9;2 1 8]!
!
B =!

* 2x1+x2=8   !
1 3 9!
2 1 8!
!
>> B=rref(B)!
!

* Second  method:     B =!
!
1 0 3!
* rref():  reduced  row  echelon  form   !
0 1 2!

>> x=B(:,end)!
* B=[1  3  9;  2  1  8],  x=[x1;  x2]   !
x =!
!
* B  is  the  augmented  matrix     3!
2!
Exercise  

* Solve:    
* 2x1+x2-­‐4x3=5  
* 3x1+8x3+10x4=7  
* x1-­‐5x2+9x3+3x4=8  
* 5x1+3x3+6x4=-­‐15  
Key  to  Exercise  
>> A=[2 1 -4 0;3 0 8 10;1 -5
9 3;5 0 3 6]!
!
A =!
!
2 1 -4 0!
3 0 8 10!
1 -5 9 3!
5 0 3 6!
!
>> b=[5;7;8;-15]!
!
b =!
!
5!
7!
8!
-15!
!
>> x=inv(A)*b!
!
x =!
!
-12.6680!
-17.0810!
-11.8543!
13.9838!

You might also like