Matrices and MATLAB Dr Viktor Fedun Automatic Control and Systems Engineering, C09 Based on lectures by Dr Anthony Rossiter Examples of a matrix Examples of a matrix Examples of a matrix A matrix can be thought of simply as a table of numbers with a given number of rows and columns. Example A has 3 rows and 3 columns, example B has 4 rows and 4 columns. 2 A 5 3 0 6 1 3 9 ; 4 2 4 B 7 54 3 4 5 2 8 6 23 1 6 5 8 2 Examples of square matrices Examples of a matrix A matrix can be thought of simply as a table of numbers with a given number of rows and columns. Example A has 2 rows and 3 columns, example B has 3 rows and 1 column. 2 A 1 3 0 4 ; 5 5 B 6 7 Examples of non-square matrices What use are matrices? • Compact form for storing data (equivalent to a table but more flexible). • Each column(or row) can relate to a different ‘state’: e.g. Time, displacement, velocity, temperature, etc. • Convenient for computer code (and algebra) as can store and share large quantities of related data with a single variable. • Easily extend to dynamic relations/modelling. Indexing of matrices Always give as {row index,column index} Start counting from top left, so the top left component is index {1,1}, e.g. indices are given as: Column Row index increasing {1,1} { 2 ,1} {3 ,1} { 4 ,1} {1, 2} {1, 3} { 2 , 2} { 2 , 3} {3 , 2} {3 , 3} { 4 , 2} { 4 , 3} {1, 4} { 2 , 4} {3 , 4} { 4 , 4} index increasing What is the index of the component -4 1. 2. 3. 4. 5. {1,3} {1,2} {2,1} {3,1} {4,3} 2 4 B 7 54 3 4 5 2 8 6 23 1 6 5 8 2 What is the index of the component -4 1. 2. 3. 4. 5. {1,3} {1,2} {2,1} {3,1} {4,3} 2 4 B 7 54 {1,1} { 2 ,1} {3 ,1} { 4 ,1} 3 4 5 2 8 6 23 1 {1, 2} {1, 3} { 2 , 2} { 2 , 3} {3 , 2} {3 , 3} { 4 , 2} { 4 , 3} 6 5 8 2 {1, 4} { 2 , 4} {3 , 4} { 4 , 4} Create a matrix with following information 2nd row and 3rd column is 5 4th row and 2nd column is 2 1st row and 5th column is 6 Remainder are 0. TRY this on MATLAB A(2,3)=5; A(4,2)=2; A(1,5)=6; disp(A) Note MATLAB syntax matches that used in mathematics, i.e. row index and then column index. Extracting parts of matrices Example of selecting given rows and columns. Can rearrange the order if desired. Matrices The main thing to note is that the default variable in MATLAB is a matrix (or vector if the row or column dimension is one). Any BODMAS type operation that is valid with matrices can be carried out with the same syntax. MATLAB also includes a large number of matrix analysis tools that you will find useful in the 2nd year. Matrices and MATLAB for plots A student collects data from an experiment and stores the answer in a matrix data_exp1. • Column 1 corresponds to time. • Column 2 to temperature • Column 3 is voltage supplied. It is now simple to code and plot this data – the corresponding code is shown next. MATLAB example 30 25 20 Temperature Voltage 15 10 5 0 0 20 40 60 Time (sec) 80 100 120 Correcting data Student is told that the temperature readings were all out by 2 degrees due to a calibration error. Once the data is in a matrix, update is very efficient. Use line: data_exp1(:,2)=data_exp1(:,2)+2 This will work not matter how many items of data are affected. Matrix notation Within a textbook, it is more normal to use subscripts to represent the indices, so for example: A1 ,1 A 2 ,1 A A 3 ,1 A 4 ,1 A 1, 2 A1 , 3 A 2,2 A2 , 3 A 3,2 A3 , 3 A 4,2 A4 , 3 A1 , 4 A2 , 4 A3 , 4 A4 , 4 Often the comma is omitted for low value integers B B11 B12 B13 Matrix notation Matrix notation Matrix notation Matrix notation Matrix notation Flexibility As an engineer you need to be flexible and work out what notation a particular article, website, book, etc is using. Usually it will be obvious from the context and usage as long as your understand the core principles. Which statement is false? 8 D 6 2 1. 2. 3. 4. 5. 3 5 D12=-3 D21=6 D32=2 D23=2 D33=0 4 2 0 0 Matrix dimensions What are the dimensions of the following matrices? 8 D 6 2 3 5 4 2 A 1 C 2 1 5 2 0 ; 0 3 E 4 2 0 4 ; 5 4 0 3 3 2 5 1 4 0 5 B 6 7 4 0 4 5 3 Matrix dimensions summary • Dimensions are always number of rows first and then number of columns second. Typical notation is 2 by 3 or 4 by 1 or similar. Describe dimensions of matrices on previous slide. • Indexing begins from top left corner. MATRIX TRANSPOSE This is a very common operation so it is critical that students are familiar with the notation. There is nothing to understand as it is simply a definition. MATRIX TRANSPOSE This is a very common operation so it is critical that students are familiar with the notation. There is nothing to understand as it is simply a definition. Matrix transpose definition The transpose is determined by swapping the position of all the indices, that is {1,2} becomes {2,1}, etc. T Ai , j A j ,i You could view this a bit like doing a reflection about the principle diagonal. Diagonal elements do not change. 2 A 3 3 ; 4 2 A 3 T 3 ; 4 Examples of Matrix transpose Ai , j A 2 A 6 3 C 2 1 3 4 ; 4 5 T j ,i 2 A 3 T 4 0 4 Alternative insight. The jth column of A is the jth row of A transpose. 6 4 0 ; 3 4 C T 21 5 4 0 4 0 Matrix transpose examples Find the transpose of the following: 8 D 6 2 3 3 E 4 2 3 2 5 1 4 0 2 0 0 5 4 5 B 6 7 4 5 3 Which is the correct transpose of G? 2 G 2 2 2 2 ; 2 2 C 2 2 1. 2. 3. 4. 5. 2 2 ; 2 2 A 2 2 D 2 A B C D None of these 2 2 2 2 2 ; 2 2 2 2 B 2 2 2 2 2 ADDITION AND SUBTRACTION OF MATRICES Addition and subtraction of matrices Add (or subtract) components in same position, that is with the same {row, column} index. C A B C i , j Ai , j B i , j Matrices must be same dimensions or you cannot add them! 2 A 3 3 ; 4 1 B 7 2 ; 6 3 A B 4 5 2 Do following matrices computations 2 A 3 2 A 6 3 3 ; 4 3 4 ; 4 4 B 7 2 ; 6 AB 2 B 12 8 0 6 ; 7 A 2B 2 G 3 1 C 2 3 F 1 1. 2. 3. 4. 5. 1 ; 4 3 ; 5 4 ; 9 2 A 0 2 D 2 2 ; 8 6 2 1 0 2 0 E 2 G+C=F G-C=-F A+D=E D-A=-E None of these 2 2 8 4 9 Which is incorrect (could be several)? Do following matrix computations 2 A 3 2 A 1 3 3 ; 4 3 5 ; 0 4 B 7 2 ; 6 2 B 1 0 0 6 ; 4 A 3B T AB T MATRIX handling in MATLAB • Matrix addition/subtraction • Matrix multiplication • Matrix powers • Matrix inversion • Extracting parts of matrices • • • • A+B, A-B, A-2*B+C A*B A^3 inv(A) or A^(-1) • A([1,3],[2, 4]) MATRIX MULTIPLICATION This is the most common skill you will need. The technique is simply a definition that you need to memorise. There is no such thing as matrix division! Basic rule for multiplication (BY DEFINITION – not to understand) Index {i,j} of result is determined from row {i} of left hand matrix and column {j} of right hand matrix. Multiplying a row on left by a column on the right is a ‘dot-product’ type of operation, e.g. e a b c f d ae bf cg dh g h What is the result of the following ? 3 3 5 2 6 8 1. 88 2. 52 3. 32 What is the result of the following ? 3 1 1 1. 2. 3. 4. 2 2 3 7 4 0 2 18 21 27 Don’t know What is the result of the following ? 1 1. 2. 3. 4. 1 2 0 3 1 6 NA Not sure KEY OBSERVATION A row vector can only be multiplied onto a column vector if the two vectors are the same length. Number of columns of left hand vector must match the number of rows of the right hand vector. If vectors have different dimensions, multiplication is not defined. Rules for matrix multiplication Assume we wish to do C=A*B. A must have same number of columns as B has rows Columns of A Columns of C Rows of C C A = Rows of A Columns of B B Rows of B Basic rule for multiplication Index {i,j} or result is determined from row {i} of left hand matrix and column {j} of right hand matrix. Multiplying a row on left by a column on the right is a ‘dot-product’ type of operation, e.g. a k a c e b g d k f b c n m h i n m e d f ae bf cg dh p g ke nf mg ph h ag bk j cg dk p eg fk ah bn ai bm ch dn ci dm eh fn ei fm aj bp cj dp ej fp Student problem Solve the following multiplications. 2 4 2 4 5 6 5 6 1 7 6 4 5 8 6 2 1 7 9 1 6 4 5 10 1 8 6 2 12 2 Student problem 2 – compute C,D 2 A 4 5 6 6 8 4 ; 6 1 7 B 4 5 ; 6 2 C AB ; D BA KEY OBSERVATION Two matrices can only be multiplied if the column dimension of the left matrix matches the row dimension of the right The property of matrix. AB=BA is called In general commutativity AB BA and does not hold for matrices in general. In fact, it is possible that AB exists but BA does not! Uses of matrix multiplication A simple example involves the change of coordinates from one set to another – this is commonly needed for robotics and mechanics. X ' x cos y sin V=(x,y) y Y’ X’ θ x Y ' x sin y cos X ' cos Y ' sin sin x cos y SIMULTANEOUS EQUATIONS Matrix/vector algebra is a compact and efficient method for handling linear simultaneous equations and most effective techniques deployed assume matrix representation of this problem. [Details in semester 2] MATLAB automatically handles simultaneous equations with matrices and vectors which greatly simplifies solution and manipulation. [Try this now!] Using Matrices for simultaneous equations Consider a single linear equation and write in MATRIX/VECTOR format, e.g. 3x 2 y 4 x 5y 6 3x 2 y 3 x 5 y 1 x [3 2 ] 4 y x [ 1 6 ] 6 y 2 x 4 5 y 6 Put following into matrix-vector format 5 x 6 y 1 x 3 y 2 x z 2y 3 3 z 6 y 4 20 x 13 y 4 z 10 a b c 12 b 2 c 3 d 4 a 14 c d 2 b 12 a 14 d 9 Simultaneous equations Assume user knows how to put simultaneous equations into matrix vector format, ie. AX=b Can solve with MATLAB several different ways; 1. X=inv(A)*b 2. X=A\b 3. Numerically efficient code is discussed in text books and semester 2 of ACS123. 1 Try on MATLAB x A b; 1 b 3 ; 2 2 A 1 0 1 4 2 0 2 8 DETERMINANTS You will cover solution of simultaneous equations in semester 2. However, in order to prepare for this, you will need familiarity and skill with the determinant of a matrix. Final matrix topic here is define ‘determinant’. NOTE: This is a definition and therefore must be learnt as a fact. ONLY DEFINED FOR SQUARE MATRICES Determinant in brief In conceptual terms, a determinant gives an indication of the ‘magnitude’ of a matrix, that is, on average, what scale of impact does it have on vectors when multiplying those vectors. You will notice that the effective magnification is direction dependent, and so the determinant is an average in some sense. 3 2 2 1 5 ; 3 1 5 Scaled by factor of 5. 3 2 2 1 1 ; 3 1 1 Scaled by factor of 1. Determinant – key fact If the determinant is very small, then there exists a least one direction, which when multiplied by the matrix, results in a very small scaling factor. 10 18 1 1 11 ; 2 1 20 Scaled by factor of 16 (approx). 10 18 1 0 . 1 0 . 1 ; 2 0 .9 0 Scaled by factor of 0.1 (approx). A zero determinant means at least one direction can be mapped to zero. Determinant for 2 by 2 matrices The determinant is defined as product of the diagonal elements minus the product of the offdiagonal elements: a A c b ; d A ad bc Note use of vertical lines around matrix is notation use to define determinant. Try this on the examples of the previous few slides. Determinant computations 3 A 2 10 B 18 2 ; 3 1 ; 2 A B Find the determinant of matrix D 1 D 5 1. 2. 3. 4. 5. -12 28 12 -28 unsure 4 8 Find the determinant of matrix F 1 F 4 1. 2. 3. 4. 5. 0 24 -24 -32 unsure 3 12 Determinant for 3 by 3 matrices The determinant is defined using the concept of a matrix of cofactors Aij. For convenience define the original matrix with lower case and the matrix of cofactors with upper case. a 11 A a 21 a 31 a 12 a 22 a 32 a 13 a 23 ; a 33 Next, by definition: A11 cof ( A ) A 21 A31 A12 A 22 A32 A13 A 23 A33 A a 11 A11 a 12 A 12 a 13 A13 or a 21 A 21 a 22 A 22 a 23 A 23 or a 31 A31 a 32 A 32 a 33 A33 Defining the cofactor terms A cofactor is taken as the determinant of a minor with a sign change if appropriate to the position. A minor is the matrix remaining when the row and column associated to an element are crossed out. a 11 A a 21 a 31 sign a 12 a 22 a 32 a 13 a 23 ; a 33 ; a 21 min or ( a 13 ) a 31 a 22 a 32 A13 cof ( a13 ) a 21 a 32 a 22 a 31 Defining the cofactor terms A cofactor is taken as the determinant of a minor with a sign change if appropriate to the position. A minor is the matrix remaining when the row and column associated to an element are crossed out. a 11 A a 21 a 31 sign a 12 a 22 a 32 ; a 13 a 23 ; a 33 a 12 min or ( a 21 ) a 32 a 13 a 33 A21 cof ( a 21 ) [ a 12 a 33 a 32 a13 ] Find the matrix of cofactors and determinant 3 A 8 0 6 1 1 5 0 2 A a 11 A11 a 12 A 12 a 13 A13 Find the matrix of cofactors and determinant 0 A 2 2 3 1 1 4 2 5 Determinant for 4 by 4 matrices The determinant is defined using the concept of a matrix of cofactors Aij (as for the 3 by 3 case) For convenience define the original matrix with lower case and the matrix of cofactors with upper case. Next, by definition: A a 11 A11 a 12 A 12 a 13 A13 a 14 A14 4 or a ij Aij j 1 The extension to larger matrices should be obvious, although this is not a paper and pen exercise. Defining the cofactor terms for a 4 by 4 A cofactor is taken as the determinant of a minor with a sign change if appropriate to the position. A minor is the matrix remaining when the row and column associated to an element are crossed out. a 1 ,1 a 2 ,1 A a 3 ,1 a 4 ,1 a 1, 2 a1, 3 a 2,2 a 2 ,3 a 3,2 a 3,3 a 4,2 a 4 ,3 a1, 4 a 2,4 a 3,4 a 4,4 Remarks on 4 by 4 determinants A minor is now a 3 by 3 matrix and therefore each cofactor requires the computation of the determinant of a 3 by 3 For a 5 by 5 matrix, the minors would each require the computation of a 4 by 4 determinant. Determinants of larger matrices are best done using properties and tricks to minimise the numeric computations. Alternatively, and more likely, use a computer. Properties of determinants 1. Adding a multiple of any row to another row does not change the determinant. 2. If two rows (or two columns) are equal then the determinant is zero. 3. Scaling any row by λ results in a scaling of the determinant by λ 4. Interchanging rows (or columns) changes the sign of the determinant. 5. A lower or upper triangular matrix has a determinant given as product of diagonal terms. 6. The determinant of a matrix and determinant of its transpose are equal Properties of determinants 1. Adding a multiple of any row to another row does not change the determinant. Properties of determinants 2. If two rows (or two columns) are equal then the determinant is zero. Properties of determinants 3. Scaling any row by λ results in a scaling of the determinant by λ. Properties of determinants 4. Interchanging rows (or columns) changes the sign of the determinant. Properties of determinants 5. A lower or upper triangular matrix has a determinant given as product of diagonal terms. Properties of determinants 6. The determinant of a matrix and determinant of its transpose are equal Find the determinant (using properties) 3 A 0 0 6 1 0 5 0 2 Check answers using MATLAB – e.g det(A) Find the determinant (using properties) 1 22 B 5 104 0 0 2 0 18 3 56 13 0 0 0 2 Check answers using MATLAB – e.g det(A) Find the determinant (using properties) 1 A 2 1 6 1 4 5 0 2 Find the determinant (using properties) 1 3 B 5 104 4 5 12 15 18 3 56 13 0 0 7 2 Find the determinant (using properties) 1 A 6 4 1 3 B 5 104 6 1 0 5 0 0 2 0 12 5 0 0 56 13 0 0 0 2 Adjoint The adjoint matrix is the transpose of the matrix of cofactors. This will be used in semester 2 for solving linear simultaneous equations. All the cofactors need to be computed! THIS IS TEDIOUS! However: The adjoint is not needed if only the determinant is required.