Download presentation

Presentation is loading. Please wait.

Published byAshlynn Seals Modified about 1 year ago

1

2
4-2 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

3
3 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.

4
4-4 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, Example: In Matlab the above matrix is created by And the sub-scripted variables take values such as: m 11 = 2 m 12 = 3 m 21 = 3 m 22 = -2 >> M = [2 3 ; 3 -2]; Note the use of the “ ; “ operator.

5
4-5 >> M1 = [2 3 ; 4 5] M1 = >> M2 = M1’ M2 = 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.

6
4-6 Arrays can be constructed from built-in Matlab functions (see Pratap pp ). >> A = eye(3) (eye(n) constructs an n x n “identity” matrix) A = >> A = ones(3,2) (ones(m,n) constructs an m x n array of 1’s) A =11 11 >> A = zeros(2,3) (zeros(m,n) constructs an m x n array of 0’s) A = 000

7
4-7 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) (green line represents the “diagonal” of a matrix”) 003 K = [2 3]; >> diag(K,1) (diag(K,1) constructs a matrix with values of K above the diagonal)

8
4-8 >> K = [3 4]; >> diag(K,-1) (diag(K,-1) constructs a diagonal matrix with values of K below the diagonal)

9
4-9 Arrays can be replicated and tiled to make larger matricies.. Example: >> A = [1 2 ; 3 4] A = 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 =

10
4-10 >> mat1 = [1 -2 ;5 3 ] ; Example >> mat1(1,1) ( first row,first column) ans = 1 >> mat1(1,2) ( first row,second column) ans = -2 >> mat1(2,1) (second row, first column) ans = 5 >> mat1(2,2) (second row, second column) ans = 3

11
4-11 You can select an entire row or column of a matrix by using the colon “ : ” symbol. >> mat1 = [ ; ; ] mat1 = >> mat1( 1, : ) % (first row, any column) ans = >> mat1( :, 2) % (any row, second column) ans =

12
4-12 >> mat1 = [ ; ; ] mat1 = >> mat1( end, end ) % (third row, fourth column) ans = 12 >> mat1( [1 2], [1 4] ) % (first and second row, first and fourth column) ans =

13
4-13 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 = >> mat1(2, 2) = 7 mat1 = >> mat1(:,1) = [ -1 -2] (or [-1 -2]’) mat1 =

14
4-14 (dot form) 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. add sub mult right div left div power + - * / \ ^.*./.\.^

15
4-15 >> A = [2 3 4; 5 6 7] ; >> B = 3; >> A + B ans= A and B must be of the same size or a scalar. >> B = [1 2 3; 4 5 6] ; >> A + B ans=

16
4-16 >> A = [2 3 4; 5 6 7; ] ; >> B = 3; >> A - B ans= A and B must be of the same size or a scalar. >> B = [1 2 3; 4 5 6; 7 8 9] ; >> A - B ans= 111

17
4-17 >> A = [2 3 4; 5 6 7] ; >> B = [1 2 3; 4 5 6] ; >> A.* B ans= Note: the result is the product of the individual elements of the array. The size of A and B must be the same or either could be a scalar. For the other operators in the table on slide , 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.

18
4-18 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.

19
4-19 Given the two matrices (math notation):

20
4-20 = 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)

21
4-21 The possible combinations of scalar products of rows and columns are: ( c 11 = first row of A * first column of B) ( c 12 = first row of A * second column of B)

22
4-22 The possible combinations of scalar products of rows and columns are: ( c 21 = second row of A * first column of B) ( c 22 = second row of A * second column of B)

23
4-23 Example: >> A = [2 3 4; 5 6 7] ; >> B = [1 2 3; 4 5 6; 7 8 9]; >> C = A * B C= 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 * Bdoes not equal B * A.

24
4-24 >> A = [2 3 4; 5 6 7] ; >> B = [1 ; 2 ; 3] ; >> C = A * B C= Example: 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

25
4-25 Consider the three equations in three unknowns(below): 100x + 10y + z = 1 225x + 15y + z = x + 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?

26
4-26 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 = [ ; ; ] ; >> C = [1 ; 20 ; 10] ; >> B = A \ C B= You can test this is the solution by typing >>A*B A is called the “coefficient matrix” for the system of linear equations. The three planes intersect at the point ( , , )

27
4-27 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.

28
-VVVV- Problem: Weights suspended by springs W5W5 W4W4 W3W3 W2W2 W1W1 K6K6 K5K5 K4K4 K3K3 K2K2 K1K1 Hooke’s Law: F = - K*d d i is the length of spring S i, K i is the i-th spring constant, W i is the i-th weight, h i is i-th vertical position d 1 + d 2 + d 3 + d 4 + d 5 +d 6 = H, height of floor to ceiling. Also, h 1 = d 1 and h i = d 1 +d d i for i = 2,..., 5. From Newton’s second Law: K i+1 *d i+1 - K i *d i = W i for i=1,...,5. If we can find the d i values then we can compute the h i values. unknowns : ceiling floor H

29
For a system with five weights K 2 *d 2 - K 1 *d 1 = W 1 K 3 *d 3 - K 2 *d 2 = W 2 K 4 *d 4 - K 3 *d 3 = W 3 K 5 *d 5 - K 4 *d 4 = W 4 K 6 *d 6 - K 5 *d 5 = W 5 and d 1 + d 2 + d 3 + d 4 + d 5 +d 6 = H In math notation:

30
4-30 Construct the following vectors. K = [K 1... K 6 ]; W = [W 1... W 5 H] ’ ; Form the matrix A in three steps 1) create a “diagonal” matrix >> A = diag(-K,0); A now has the form:

31
4-31 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:

32
4-32 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:

33
4-33 Solve the linear system A * D = W by using the back-slash operator “ \ ” >> D = A \ W; and finally, since [h 1, h 2 h 3, h 4, h 5, h 6 ] = [d 1,d 1 +d 2,d 1 +d 2 +d 3, d 1 +d 2 +d 3 +d 4,... ] >> H = cumsum(D);

34
4-34 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 = >> cumsum(mat1) ans =

35

36
4-36 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).

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google