230 likes | 401 Views
Zen and the Art of MatLab. Damian Gordon. Hard work done by :. Daphne Gilbert & Susan Lazarus. Introduction to MatLab. MatLab is an interactive, matrix-based system for numeric computation and visualisation MATrix LABoratory
E N D
Zen and the Art of MatLab Damian Gordon
Hard work done by : Daphne Gilbert & Susan Lazarus
Introduction to MatLab • MatLab is an interactive, matrix-based system for numeric computation and visualisation • MATrix LABoratory • Used in image processing, image synthesis, engineering simulation, etc.
References • “Mastering MatLab” Duane Hanselman, Bruce Littlefield • “The MatLab Primer” http://www.fi.uib.no/Fysisk/Teori/KURS/WRK/mat/mat.html • “The MatLab FAQ” http://www.isr.umd.edu/~austin/ence202.d/matlab-faq.html
MATLAB Command Window To get started, type one of these: helpwin, helpdesk, or demo. For product information, type tour or visit www.mathworks.com. » » help HELP topics:
Creating Variables >> varname = 12 varname = 12 >> SS = 56; N = 4; Tot_Num = SS + N Tot_Num = 60
Operations + Addition - Subtraction * Multiplication ^ Power \ Division / Division • Add vars • Subtract vars Multiplication • Raise to the power • Divide vars (A div B) • Divide vars (B div A)
Creating Complex Numbers >> 3 + 2i >> sqrt(9) + sin(0.5)*j ans = 3.0000 + 0.4794i Num = sqrt(9) + sin(0.5)*j real(Num) imag(Num)
Entering Matrices (1) >> A = [1 2 3; 4 5 6; 7 8 9] OR >> A = [ 1 2 3 4 5 6 7 8 9 ]
Entering Matrices (2) • To create an NxM zero-filled matrix >> zeros(N,M) • To create a NxN zero-filled matrix >> zeros(N) • To create an NxM one-filled matrix >> ones(N,M) • To create a NxN one-filled matrix >> ones(N)
Entering Matrices (3) • To create an NxM randomly-filled matrix (which is uniformly distributed) >> rand(N,M) • To create an NxM randomly-filled matrix (which is normally distributed) >> randn(N,M)
Complex Matrices • To enter a complex matrix, you may do it in one of two ways : >> A = [1 2; 3 4] + i*[5 6;7 8] OR >> A = [1+5i 2+6i; 3+7i 4+8i]
MATLAB Command Window » who Your variables are: a b c » whos Name Size Bytes Class a 8x8 512 double array b 9x9 648 double array c 9x9 648 double array Grand total is 226 elements using 1808 bytes
Matrix Addition » A = [ 1 1 1 ; 2 2 2 ; 3 3 3] » B = [3 3 3 ; 4 4 4 ; 5 5 5 ] » A + B ans = 4 4 4 6 6 6 8 8 8
Matrix Subtraction » A = [ 1 1 1 ; 2 2 2 ; 3 3 3] » B = [3 3 3 ; 4 4 4 ; 5 5 5 ] » B - A ans = 2 2 2 2 2 2 2 2 2
Matrix Multiplication » A = [ 1 1 1 ; 2 2 2 ; 3 3 3] » B = [3 3 3 ; 4 4 4 ; 5 5 5 ] » A * B ans = 12 12 12 24 24 24 36 36 36
Matrix - Power » A ^ 2 ans = 6 6 6 12 12 12 18 18 18 » A ^ 3 ans = 36 36 36 72 72 72 108 108 108
Matrix Transpose A = 1 1 1 2 2 2 3 3 3 » A' ans = 1 2 3 1 2 3 1 2 3
Left Division \ x = A\B (is A*x=B) >> A = rand(4) >> B = rand(4) >> C = A \ B => A * C = B Right Division / x=A/B (is x*A=B) >> A = rand(4) >> B = rand(4) >> C = A / B => C * A = B Matrix Division
Matrix Operations + Addition - Subtraction * Multiplication ^ Power ‘ Conjugate Transpose \ Left Division / Right Division • Add matrices • Subtract matrices Matrix Multiplication • Raise to the power • Get transpose • x = A\B (is A*x=B) • x=A/B (is x*A=B)