Matlab lesson1

Matlab lesson1 - Lesson 1: Matrices and Vectors When you...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lesson 1: Matrices and Vectors When you think of arithmetic, you probably think of adding, subtracting, multiplying and dividing numbers. However, basic arithmetic in Matlab is concerned with matrices. In this lesson, we recall what matrices are and what it means to perform various arithmetic operations with them. This will lay the groundwork for introducing Matlab in the next lesson. General Terminology A matrix is a rectangular array of numbers. It is usually written with brackets enclosing the numbers on either side. The following are all examples of matrices. ￿ ￿ 1 411 a) A = −1 0 6 5 2 −1 0 4 b) B = 3 5 0 3 −7 ￿ ￿ c) C = 3.2 −2.5 4.0 10.7 −5.4 0 1 d) D = 2 ￿￿ e) E = 17 The number of rows and columns is called the dimension of the matrix. For instance, the matrix A above has 2 rows and 4 columns and is referred to as a 2 × 4 (read, “two by four”) matrix. The matrix B is 3 × 3, C is 1 × 5, D is 3 × 1, and E is 1 × 1. Notice that the number of rows is always stated first followed by the number of columns. Thus, D is a 3 × 1 matrix and not a 1 × 3 matrix. If a matrix has the same number of rows as columns then it is called a square matrix. The matrices B and E above are both square matrices. If a matrix has only one row or only one column then it can also be called a vector. More specifically, a matrix that has only one row is called a row vector and a matrix with only one column is called a column vector. For instance, C , D, and E are all vectors; C is a row vector, D is a column vector, and E is both a row vector and a column vector. Notice that a 1 × 1 matrix such as E above is simply a number. It can be written without brackets surrounding it. It is both a row vector and a column vector, but is most commonly referred to as a scalar. The rows of a matrix are numbered from top to bottom and the columns are numbered from left to right. For example, the first row of A is ￿ ￿ 1411 and the second column is ￿￿ 4 . 0 The numbers in a matrix are called entries. They can be identified by the row and column that they are in. For example, the entry in the second row and first column of A is -1 and the entry in the second row and third column of B is 4. If M is a matrix, then Mij refers to the entry in its i’th row and j ’th column. For example A21 = −1 and B23 = 4. Notice that, just as the dimension of a matrix is the number of rows followed by the number of columns, an entry in a matrix is specified by stating first its row number and then its column number. Thus, A21 = −1 but A12 = 4. Two matrices are equal if and only if they have the same dimension and their corresponding entries are equal to each other. 1 2 The transpose of a matrix M is the matrix that is obtained when the rows of M are turned into columns (and vice versa). The transpose of M is denoted M t . For instance, consider the matrix A above. To form At we take the first row of A ￿ ￿ 1411 and write it as a column, 1 4 . 1 1 This is the first column of At . Similarly, we take the second row of A and write it as a column obtaining −1 0 . 6 5 This is the second column of At . Thus, 1 −1 4 0 At = 1 6 . 15 Shown below are the transposes of B , C , D and E . 3.2 −2.5 230 ￿ ￿ t t B = −1 5 3 , C = 4.0 , Dt = 0 1 2 , 10.7 0 4 −7 −5.4 and E t = 17. Notice that if M is an n × m matrix then M t is an m × n matrix and that (M t )ij = Mji . For example, (B t )23 = 3 = B32 and (C t )41 = 10.7 = C14 . Adding and Subtracting Matrices If two matrices A and B have the same dimension then we define A + B to be the matrix that is formed when corresponding elements of A and B are added together. Similarly, A − B is the matrix that is formed when each element in B is subtracted from its corresponding element in A. For example ￿ ￿￿ ￿￿ ￿ 2 −5 0 03 2 2 −2 2 + = , −1 4 5 1 4 −2 083 ￿ ￿￿ ￿￿ ￿ 2 −5 0 03 2 2 −8 −2 − = , −1 4 5 1 4 −2 −2 0 7 but 01 2 −5 0 + 3 4 −1 4 5 2 −2 is not defined. Notice that addition of matrices, like addition of numbers, is both commutative and associative. In other words, if A, B and C all have the same dimension, then ￿ ￿ 3 A+B =B+A A + (B + C ) = (A + B ) + C If all of the entries in a matrix are 0 then it zero matrices: ￿ ￿ ￿ 000 0 0 0, 000 is called a zero matrix. Here are some examples of ￿ 0 , 0 000 0 0 0 , 000 00 0 0 00 A zero matrix will often be written 0 irrespective of its dimension. Zero matrices play the same role in the arithmetic of matrices as the number 0 does in the arithmetic of numbers. In particular, they are additive identities. This means that given any matrix A and the zero matrix 0 that has the same size as A, A + 0 = 0 + A = A. The negative of a matrix A is the matrix that is obtained by multiplying each entry in A by −1. The negative of A is denoted −A. For example ￿ ￿￿ ￿ 2 −5 0 −2 5 0 − = −1 4 5 1 −4 −5 Notice that A + (−B ) is the same as A − B . Given any matrix A, the negative of A is the additive inverse of A. In other words, A + (−A) = −A + A = 0. Multiplying Matrices by a Scalar Recall that a scalar refers to a 1 × 1 matrix which is just a number. Given any scalar, k , and any matrix A, we define the products kA and Ak to be the matrix that is obtained when each entry in A is multiplied by k . For example ￿ ￿￿ ￿ ￿ ￿ 03 2 03 2 0 15.6 10.4 5.2 = 5.2 = 1 4 −2 1 4 −2 5.2 20.8 −10.4 Mutliplication by a scalar is distributive over addition. In other words, if k is a scalar and A and B are matrices that have the same dimension then k (A + B ) = kA + kB Notice that the negative of a matrix is obtained by multiplying it by −1. In other words, for any matrix A. Also, if k is a positive integer then kA is the same as adding A to itself k times. For example, 3A = A + A + A. The Product of Two Matrices If A is an n × p matrix and B is an p × m matrix then the product AB is defined and is an n × m matrix. Notice that the product is only defined when the number of columns in A is equal to the number of rows in B . If this is not the case, then the product is not defined. (There is one exception to this; the product is always defined if either A is 1 × 1 or B is 1 × 1 since that matrix (−1)A = −A 4 is then a scalar.) The definition of the product might seem rather convoluted at first, but you will see later in this lesson why it is defined the way it is. We define the product AB by defining its ij ’th element, (AB )ij . To find this element, consider the i’th row of A ￿ ￿ Ai1 Ai2 Ai3 . . . Aip and the j ’th column of B B1j B2j B3j . . . . Bpj Notice that these two vectors have the same number of entries; they both have p entries. Multiply the first entry in the row vector with the first entry in the column vector, the second entry in the row vector with the second entry in the column vector etc. This yields p numbers each of which is the product of an entry in A and an entry in B . Add these numbers together to obtain (AB )ij . In other words, (AB )ij = Ai1 B1j + Ai2 B2j + Ai3 B3j + . . . Aip Bpj . To illustrate this definition, consider the matrices ￿ ￿ 13 −1 3 5 A= and B = 2 0 . 2 −1 −2 −1 2 We will find the product AB . Notice that A is 2 × 3 and B is 3 × 2, so the number of columns in A is equal to the number of rows in B . This means that the product is defined and is a 2 × 2 matrix. We will find each of its entries one at a time. To find the entry in the first row and first column, consider the first row of A and the first column of B : 1 ￿ ￿ 2 . −1 3 5 and −1 Multiply the corresponding entries and add them to obtain To find the entry in the first row and second column, consider the first row of A and the second column of B : 3 ￿ ￿ 0 . −1 3 5 and 2 Multiply the corresponding entries and add them to obtain To find the entry in the second row and first column, consider the second row of A and the first column of B : 1 ￿ ￿ 2 . 2 −1 −2 and −1 Multiply the corresponding entries and add them to obtain (2)(1) + (−1)(2) + (−2)(−1) = 2. (−1)(3) + (3)(0) + (5)(2) = 7. (−1)(1) + (3)(2) + (5)(−1) = 0. 5 To find the entry in the second row and second column, consider the second row of A and the first column of B : 3 ￿ ￿ 0 . 2 −1 −2 and 2 Multiply the corresponding entries and add them to obtain Thus the product is (2)(3) + (−1)(0) + (−2)(2) = 2. ￿ ￿ 07 AB = 22 Here is another example worked out more compactly. Notice that the first matrix is 4 × 2 and the second matrix is 2 × 1 so the product is a 4 × 1 matrix. −5 2 ￿ ￿ (−5)(1) + (2)(−2) −9 0 1 1 (0)(1) + (1)(−2) −2 2 3 −2 = (2)(1) + (3)(−2) = −4 00 (0)(1) + (0)(−2) 0 Matrix multiplication has some of the properties that multiplication of numbers has but not all of them. Like multiplication of numbers, it is associative and distributive over addition. In other words, if A and B are both n × p, C is p × q , D is q × r, and E and F are both p × m then A(CD) = (AC )D A(E + F ) = AE + AF (A + B )E = AE + BE However, it is not commutative. Indeed, if A is n × p and B is p × m and n ￿= m then BA is not defined so it certainly cannot be equal to AB . Even when AB and BA are both defined and have the same size, they are not necessarily equal to each other. For instance, ￿ ￿￿ ￿￿ ￿ 1 2 −2 4 00 = , −1 −2 1 −2 00 ￿ ￿￿ ￿￿ ￿ −2 4 1 2 −6 4 but = 1 −2 −1 −2 3 −2 In other words, Matrix multiplication is not commutative. With real numbers, we know that if xy = 0 then either x = 0 or y = 0. We use this property a lot when solving equations. For example, one way to solve the equation is to factor the left-hand side obtaining x2 − 4x + 3 = 0 (x − 1)(x − 3) = 0. It follows that either x − 1 = 0 (i.e x = 1) or x − 3 = 0 (i.e. x = 3). This property is not true with matrix multiplication. As shown in the example above it is possible to have AB = 0 without either A or B being equal to 0. AB = 0 ￿⇒ A = 0 or B = 0 6 Square matrices that have 1’s on the main diagonal and 0’s everywhere else are matrices. Here are some examples of identity matrices: 100 ￿ ￿ 100 0 1 0 ￿￿ 10 I1 = 1 , I2 = , I3 = 0 1 0 , I4 = 0 0 1 01 001 000 called identity 0 0 0 1 They play the same role in the multiplication of matrices that 1 plays in the multiplication of numbers. In particular they are multiplicative identities. In other words, if A is an n × m matrix then AIm = A and In A = A. Interpreting Matrix Products in Applications The definition of the product of two matrices seems mysterious to most people the first time they see it. The following examples illustrate why the product is defined the way it is and how it can be interpreted in different contexts. Example 1: (Adapted from ‘Finite Mathematics’ by R. A. Barnett, M. R. Ziegler, and K. E. Byleen) A company that makes two-person and four-person inflatable boats has two manufacturing plants: one in Massachusetts and the other in Virginia. Each boat is first cut in the Cutting Department, then assembled in the Assembly Department and then packaged in the Packaging Department. The time needed in each department to work on a boat depends on the type of boat and the wages of the workers in each department depends on the plant in which they work. The matrix T below shows the time needed (in hours) for each boat in each department. ￿ ￿ 1.0 0.9 0.3 T= 1.5 1.2 0.4 The rows of T show the times for two-person and four-person boats in that order, whilst the columns show the times spent in the Cutting Department, the Assembly Department, and the Packaging Department in that order. For instance, notice that T23 = 0.4. This means that it takes 0.4 hours to package a four-person boat in the Packaging Department. The matrix W below shows the wages (in dollars per hour) of the workers in each department at each plant. 17 15 W = 12 10 11 10 The rows of W show the wages for the workers in the Cutting Departments, Assembly Departments, and Packaging Departments in that order, whilst the columns show the wages of the workers at the Massachusetts plant and at the Virginia Plant in that order. For instance, notice that W21 = 12. This means that a worker in the Assembly Department at the Massachusetts plant earns $12 per hour. Notice that T is 2 × 3 and W is 3 × 2. This means that the product T W is defined and is a 2 × 2 matrix. In the calculation of T W below we have put labels on the rows and columns of T and W to remind us of their meaning. We will see why the rows and columns of the product matrix are 7 labeled the way they are when we have understood the meaning of the product matrix T W . ￿CAP￿ 1.0 0.9 0.3 TW = 2 4 1.5 1.2 0.4 M V ￿MV￿ C 17 15 31.1 27 =2 A 12 10 4 44.3 38.5 P 11 10 To understand what T W tells us about the manufacture of these boats, consider first the entry in the first row and first column. This entry was obtained using the first row of T and the first column of W : (1.0)(17) + (0.9)(12) + (0.3)(11) = 31.1. Notice that 1.0 is the number of hours it takes to cut a 2-person boat in the Cutting Department and that 17 is the number of dollars per hour workers in the Cutting Department are paid at the Massachusetts plant. In other words, (1.0)(17) is the amount it costs to cut a 2-person boat at the Massachusetts plant. Similarly, (0.9)(12) is the amount it costs to assemble a 2-person boat at the Massachusetts plant and (0.3)(11) is the amount it costs to package a 2-person boat at the Massachusetts plant. Thus, the sum 31.1 is the cost to manufacture (cut, assemble and package) a 2-person boat at the Massachusetts plant. Similarly the entry in, say, the second row and second column, is the cost to manufacture a 4-person boat at the Virginia plant. Thus, the matrix T W indicates the cost of manufacturing each of the different types of boats at each of the different plants. Notice that the units of the entries in T are $/hour and the units of the entries in W are hours. So, it makes sense that the units of T W would be $/hour × hours = $. To multiply two matrices A and B you need the number of columns of A to be equal to the number of rows of B . The dimension of the product is then the number of rows of A and the number of columns of B . The labels work similarly. When we multiplied T and W together we saw that the labels on the columns of T matched the labels on the rows of W . Moreover, the labels on the rows of T W were the labels on the rows of T and the labels on the columns of T W were the labels on the columns of W . On the other hand, consider the product W T . This is defined and is a 3 × 3 matrix: M V C 17 15 WT = 12 10 A P 11 10 ￿CAP￿ ? 39.5 2 1.0 0.9 0.3 = 27.0 ? 4 1.5 1.2 0.4 ? 26.0 ??? 33.3 11.1 22.8 7.6 . 21.9 7.3 However, the column labels of W don’t match the row labels of T and we see, upon reflection, that the matrix doesn’t really mean anything. For instance, consider the entry in the first row and second column that was calculated using the first row of W and the second column of T : (17)(0.9) + (15)(1.2) = 33.3. The number 17 in this calculation represents the number of dollars per hour a worker in the Cutting Department receives at the Massachusetts plant. This is multiplied by 0.9 which is the number of hours it takes to assemble a 4-person boat in the Assembly Department. There is no discernible reason to multiply these two numbers together. Example 2: (Adapted from ‘Finite Mathematics’ by R. A. Barnett, M. R. Ziegler, and K. E. Byleen) A nationwide air freight service has connecting flights between five cities as illustrated in the diagram below. 8 ✗✔ ✗✔ 1✛ 5 ✖✕ ✑ ✖✕ ◗ ✄ ❖ ❈ ◗✑ ❇ ❈ ✑◗ ✄ ✑ ◗ ❇ ❈ ✄ ✑ ◗ ✎✰ ✄✑ ❇ ✑ ◗ ✗✔ ￿ ◗❈ ✗✔ ❇ Chicago 2 ✛ ❇ 4 Los Angeles ✖✕ ❇ ✖✕ ✸ ✑ ◗ ◆ ❇ ✑ ◗ ✗✔ ✑ ￿ ◗ New York Houston Atlanta ✖✕ 3 This diagram can be represented by the incidence matrix A below where Aij is equal to 1 if there is a flight from city i to city j and is equal to 0 otherwise. 01110 0 0 1 0 0 A = 0 0 0 1 0 0 1 0 0 1 11000 Notice that the row number indicates the city of departure and the column number indicates the city of arrival. Let’s consider the product matrix AA = A2 : 0 0 0 0 1 Arrive 111 010 001 100 100 0 0 0 1 0 0 0 0 0 1 Arrive 111 010 001 100 100 0 0 0 1 0 0 0 0 1 0 Arrive 111 001 100 110 121 1 0 1 0 0 A2 = It looks at first as if the column label of the first matrix doesn’t match up with the row label of the second matrix. However, in a sense it does, since any city that you arrive at can then be departed from. This is how these labels should be interpreted in order to make sense of the product matrix. To understand the full product, notice that Aij Ajk can take on two possible values: 0 or 1. When it is possible to fly from City i to City j and from City j to City k both Aij = 1 and Ajk = 1, so the product is 1. When it is not possible to do this because one of these flights doesn’t exist, then at least one of Aij and Ajk will be 0 which will make the product equal to 0. Thus the value of Aij Ajk tells us whether or not it is possible to fly i → j → k , and (A2 )ik = Ai1 A1k + Ai2 A2k + Ai3 A3k + Ai4 A4k + Ai5 A5k tells us how many ways there are to get from City i to City k if we insist on taking exactly 2 flights to get there. Thus, each entry in A2 tells us how many ways there are of getting from the city determined by the row number to the city determined by the column number by taking exactly two flights. It follows that A + A2 shows the number of ways of flying from any city to any other by taking at most 2 flights. Similarly, AAA = A3 shows the number of ways to get between each pair of cities taking exactly three flights and A + A2 + A3 shows the number of ways of flying from any city to any other by taking at most 3 flights. = . Depart Depart Depart 9 The Length of a Vector and the Distance Between Vectors Most of the matrices you will deal with this semester when you use Matlab will be vectors. A vector is simply a list of numbers. We think of a vector as small if all of its entries are close to 0 and large if any of its entries are large (in absolute value). More precisely, we define the length of ￿ ￿ ￿ ￿t a vector x = x1 x2 . . . xn or x = x1 x2 . . . xn to be the square root of the sum of the squares of all of its entries and denote the length by ||x||. In other words ￿ ||x|| = x2 + x2 + . . . x2 . n 1 2 If x is a row vector then xt is a column vector and xxt is a scalar and is equal to the sum of the squares of all of the entries in x. Thus √ ||x|| = xxt . Similarly, if x is a column vector then ￿ ￿ For example, if x = −3 0 4 then ￿ ￿ ￿￿ ￿ ￿ −3 ￿ ||x|| = ￿ −3 0 4 0 = (−3)2 + 02 + 42 = 5. 4 ||x|| = √ xt x. Two vectors x and y that have the same dimension are close to each other if all of their corresponding entries are close to each other. In other words, they are close if x −￿y is small. We ￿ think of ||x − y || as ￿ distance between x and y . For instance, suppose x = −2 5 4 and the ￿ y = −2.1 30 4.01 . Even though the first and third entries in x and y are close to each other, the second entry is not and we see that the distance of x from y , ￿ ￿ ￿ ||x − y || = || 0.1 −25 −0.01 || = 0.12 + (−25)2 + (−0.01)2 = 25.000002 is not particularly small. Further Study Linear algebra is the study of matrices and vectors. If you pursue your studies in science and/or engineering you will undoubtedly see that matrices and vectors are ubiquitous. For instance, they are used extensively in computer graphics, operations research, graph theory, differential equations, and geometry. The introduction to matrices given here is very rudimentary. The goal in this lesson was to cover them sufficiently to allow you to use Matlab and other high-level scientific programming languages effectively. In particular, you should notice that we haven’t discussed what it means to divide two matrices. ...
View Full Document

This note was uploaded on 09/21/2010 for the course BISC 13004 at USC.

Ask a homework question - tutors are online