Lecture 4
What will I learn in this lecture? How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions eye , ones, zeros and diag to create matrices. Learn about the size function the matrix counterpart to the length vector function Readings: Matlab by Pratap Chapters 2.3.1, 2.3.2, 5.1
Arrays - Vectors and Matrices In Matlab (for CS101) all data will be stored in arrays. An array can represent a: vector - size 1 x n (row vector with 1 row and n columns) or n x 1 (column vector with n rows and 1 column) matrix - any size m x n (m rows and n columns). Conventions Two arrays are defined to be equal if they are of the same size and at each position, they have the same value.
Creating a Matrix m11 = 2 m12 = 3 m21 = 3 m22 = -2 A matrix is like a table with rows (horizontal) and columns (vertical). Specific elements are referenced by the pair (row,column). Example: For a general 2 x 2 matrix the math notation is, And the sub-scripted variables take values such as: m11 = 2 m12 = 3 m21 = 3 m22 = -2 Example: In Matlab the above matrix is created by >> M = [2 3 ; 3 -2]; Note the use of the “ ; “ operator.
Transpose of a Matrix >> M1 = [2 3 ; 4 5] M1 = 2 3 4 5 2 3 4 5 >> M2 = M1’ M2 = 2 4 3 5 The transpose operator ‘ makes the first row of M1 become the first column of M2 and the second row of M1 become the second column of M2.
Creating a Matrix: Using eye,ones and zeros Arrays can be constructed from built-in Matlab functions (see Pratap pp. 70-71). >> A = eye(3) (eye(n) constructs an n x n “identity” matrix) A = 1 0 0 0 1 0 0 0 1 >> A = ones(3,2) (ones(m,n) constructs an m x n array of 1’s) A = 1 1 1 1 >> A = zeros(2,3) (zeros(m,n) constructs an m x n array of 0’s) A = 0 0 0
Creating a Matrix: Using diag Arrays can be constructed from the diag function and a given vector (see 2.9) >> K = [1 2 3]; >> diag(K,0) (diag(K,0) constructs a diagonal matrix with values of K down the diagonal) 1 0 0 0 2 0 (green line represents the “diagonal” of a matrix”) 0 0 3 K = [2 3]; >> diag(K,1) (diag(K,1) constructs a matrix with values of K above the diagonal) 0 2 0 0 0 0
Creating a Matrix: Using diag >> K = [3 4]; >> diag(K,-1) (diag(K,-1) constructs a diagonal matrix with values of K below the diagonal) 0 0 0 3 0 0 0 4 0
repmat - replicate and tile matrix Arrays can be replicated and tiled to make larger matricies.. Example: >> A = [1 2 ; 3 4] A = 1 2 3 4 Create a matrix named B that tiles A in the following manner >> B = repmat(A,3,4) (constructs an 3 x 4 tiling of A) B = 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4
Selecting elements from a Matrix - Subscripting Example >> mat1 = [1 -2 ;5 3 ] ; >> mat1(1,1) ( first row ,first column) ans = 1 >> mat1(1,2) ( first row ,second column) -2 >> mat1(2,1) (second row, first column) 5 >> mat1(2,2) (second row, second column) 3
Selecting elements from a Matrix - Subscripting You can select an entire row or column of a matrix by using the colon “ : ” symbol. >> mat1 = [1 -2 3 -4 ; 5 3 7 8 ; 9 10 11 12] mat1 = 1 -2 3 -4 5 3 7 8 9 10 11 12 >> mat1( 1 , : ) % (first row, any column) ans = 1 -2 3 -4 >> mat1( : , 2) % (any row , second column) -2 3 10
Selecting elements from a Matrix - Subscripting >> mat1 = [1 -2 3 -4 ; 5 3 7 8 ; 9 10 11 12] mat1 = 1 -2 3 -4 5 3 7 8 9 10 11 12 >> mat1( end , end ) % (third row, fourth column) ans = 12 >> mat1( [1 2] , [1 4] ) % (first and second row, first and fourth column) ans = 1 -4 5 8
Modifing elements of a Matrix - Subscripting You can use subscripting on a Matlab variable to modify the value of the matrix. Note that the matrix is located on the left hand side of the = . >> mat1 = [1 -2 ; 5 3 ] ; >> mat1(1,:) = 4 mat = 4 4 5 3 >> mat1(2, 2) = 7 mat1 = 5 7 >> mat1(: ,1) = [ -1 -2] (or [-1 -2]’) mat1 = -1 4 -2 7
Arrays - Arithmetic operators + - * / \ ^ .* ./ .\ .^ (dot form) add sub mult right div left div power Rules for the valid operations. If A and B are arrays then it is not always true that >>A op B is defined when op is one of the operations in the table above. The size of an array is its number of rows and columns. We will consider some operators on a case by case basis.
Arrays - Addition >> A = [2 3 4; 5 6 7] ; >> B = 3; A and B must be of the same size or a scalar. >> A = [2 3 4; 5 6 7] ; >> B = 3; >> A + B ans = 5 6 7 8 9 10 >> B = [1 2 3; 4 5 6] ; >> A + B ans = 3 5 7 9 11 13
Arrays - Subtraction >> A = [2 3 4; 5 6 7; 8 9 10] ; A and B must be of the same size or a scalar. >> A = [2 3 4; 5 6 7; 8 9 10] ; >> B = 3; >> A - B ans = -1 0 1 2 3 4 5 6 7 >> B = [1 2 3; 4 5 6; 7 8 9] ; >> A - B ans = 1 1 1
Arrays - (dot) Multiplication The size of A and B must be the same or either could be a scalar . >> A = [2 3 4; 5 6 7] ; >> B = [1 2 3; 4 5 6] ; >> A .* B ans = 2 6 12 20 30 42 Note: the result is the product of the individual elements of the array. For the other operators in the table on slide 4 - 14 , try to determine what the operator does and the sizes for which A op B is valid. We will consider the “ \ “ operator in the future.
Matrix Product The dot or scalar product of two vectors F * s is: To compute the product “ * ” of two matrices A * B we will take the scalar products of the rows of A with the columns of B. If A has size (m x n) and B has size (n x p) then there are m x p possible combinations. In fact A * B is defined only when the number of columns of A equals the number of rows of B.
Matrix Product Given the two matrices (math notation):
Matrix Product C(m x p) = A (m x n) * B (n x p) The product of matrix A and B is another matrix C. The size of C is m x p (the number of combinations of scalar products of the rows of A with the columns of B) C(m x p) = A (m x n) * B (n x p)
Matrix Product The possible combinations of scalar products of rows and columns are: ( c11 = first row of A * first column of B) ( c12 = first row of A * second column of B)
Matrix Product The possible combinations of scalar products of rows and columns are: ( c21 = second row of A * first column of B) ( c22 = second row of A * second column of B)
Arrays - Multiplication Example: >> A = [2 3 4; 5 6 7] ; >> B = [1 2 3; 4 5 6; 7 8 9]; >> C = A * B C = 42 51 60 78 96 114 Note: the size of A * B is computed by: size(A) is 2 x 3 size(B) is 3 x 3 size(A * B) is 2 x 3 Note: In general A * B does not equal B * A.
Arrays - Multiplication Example: >> A = [2 3 4; 5 6 7] ; >> B = [1 ; 2 ; 3] ; >> C = A * B C = 20 38 Note: again, the size of A * B is computed by: size(A) is 2 x 3 size(B) is 3 x 1 size(A * B) is 2 x 1
Linear Systems of Equations Consider the three equations in three unknowns(below): 100x + 10y + z = 1 225x + 15y + z = 20 400x + 20y + z = 10 This system of three equations is called linear since the powers on all the variables are one and each variable occurs separately (i.e. no xy or xyz terms). The plot of each equation in 3D is a plane. If the three planes intersect at one point in 3D space, then what is that point?
Linear Systems of Equations To solve a system of equations A*B = C , i.e. to find the point of intersection of the three planes, use the “ \ ” operator. >> A = [100 10 1; 225 15 1; 400 20 1] ; >> C = [1 ; 20 ; 10] ; >> B = A \ C B = -0.5800 18.3000 -124.0000 You can test this is the solution by typing >>A*B 1.0000 20.0000 10.0000 A is called the “coefficient matrix” for the system of linear equations. The three planes intersect at the point (-0.5800, 18.3000, -124.0000)
\ operator - How It Works If we naively wanted to solve the equation for B given A and C A * B = C (and we forgot that A is a matrix, B and C column vectors), then we might try to solve the above for B by dividing both sides by A. A\(A*B) = A\C where we read the black-slash A\C means A divided into C (A is underneath C). If A\(A*B) = (A\A)*B then since A\A is one we get B = A\C We can use this heuristic to remember how to use back-slash. The “ \ ” back-slash operator uses a combination of numerical methods including LU decomposition.
Problem: Weights suspended by springs ceiling -VVVV- K6 Problem: Weights suspended by springs W5 Hooke’s Law: F = - K*d di is the length of spring Si, Ki is the i-th spring constant, Wi is the i-th weight, hi is i-th vertical position -VVVV- K5 W4 unknowns: H -VVVV- K4 d1 + d2 + d3 + d4 + d5 +d6 = H, height of floor to ceiling. Also, h1 = d1 and hi = d1+d2+ ...+di for i = 2, ... , 5 . From Newton’s second Law: Ki+1*di+1 - Ki*di = Wi for i=1 , ... ,5 . If we can find the di values then we can compute the hi values. W3 K3 -VVVV- W2 -VVVV- K2 W1 -VVVV- K1 floor
For a system with five weights K2*d2 - K1*d1 = W1 K3*d3 - K2*d2 = W2 K4*d4 - K3*d3 = W3 K5*d5 - K4*d4 = W4 K6*d6 - K5*d5 = W5 and d1 + d2 + d3 + d4 + d5 +d6 = H In math notation:
Matlab Solution to the Spring-Weight problem Construct the following vectors. K = [K1 ... K6]; W = [W1 ... W5 H] ’ ; Form the matrix A in three steps 1) create a “diagonal” matrix >> A = diag(-K,0); A now has the form:
Matlab Solution to the Spring-Weight problem Form the matrix A in steps 2) change the bottom row to a row of 1’s. >> A(end, : ) = 1; A now has the form:
Matlab Solution to the Spring-Weight problem Form the matrix A in steps 3) Put in the values above the diagonal. >> A = A + diag( K(2:end) , 1 ); A now has the form:
Matlab Solution to the Spring-Weight problem Solve the linear system A * D = W by using the back-slash operator “ \ ” >> D = A \ W; and finally, since [h1 , h2 h3 , h4 , h5 , h6 ] = [d1 ,d1+d2 ,d1+d2+d3 , d1+d2+d3+d4 , ... ] >> H = cumsum(D);
Function - cumsum The cumsum function returns the cumulative sum of values for a vector. For a matrix, cumsum operates on the columns of the matrix. Example >> p1 = [1 -2 5]; >> mat1 = [1 -2 ;1 3 ; 2 4] ; >> cumsum(p1) ans = 1 -1 4 >> cumsum(mat1) 1 -2 2 1 4 5
Matlab Solution to the Spring-Weight problem
What have I learned in this lecture? Matlab arrays can be vectors or matrices. You can construct matrices using the semicolon operator “ ; ” which means append values to next row. The functions eye, ones, zeros and diag can also be used to create matrices. You can use subscripting on matrices. Use the colon “ : ” to denote all of the row or column. The back-slash operator “ \ ” is used in obtaining solutions to linear systems of equations. The end command can be used in subscripting to return the last row or column of the matrix (or vector).