13-matrixmath

49
Announcements • our final exam is the last week of class, in your lab period • Lab 10 will be our final and third-graded lab

Upload: radoi-mihai

Post on 02-May-2017

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 13-MatrixMath

Announcements

• our final exam is the last week of class, in your lab period

• Lab 10 will be our final and third-graded lab

Page 2: 13-MatrixMath

m files

• type commands into MATLABs "notepad"• load/save as usual• two ways to run:

– hit the PLAY button– Type the filename at the command prompt

(without the .m)

Page 3: 13-MatrixMath

rand

• I = rand(1,3) % rand(m,n) gives m*n matrix of uniformly distributed random numbers from 0 - .9999

>> I = rand(1,3)

I = 0.8147 0.9058 0.1270

Page 4: 13-MatrixMath

>> A = [rand(1,3); rand(1,3)*10; rand(1,3)*100 ]

A = 0.8235 0.6948 0.3171 9.5022 0.3445 4.3874 38.1558 76.5517 79.5200

Page 5: 13-MatrixMath

Create a 3 by 3 matrix with each element a random value between 0 and 9• rand( 1 ) create a 1 by 1 matrix with random values(0, 1)• rand(3, 3) create a 3 by 3 matrix with random values(0, 1)

Page 6: 13-MatrixMath

Create a 3 by 3 matrix with each element a random value between 0 and 9• rand( 1 ) create a 1 by 1 matrix with random values(0, 1)• rand(3, 3) create a 3 by 3 matrix with random values(0, 1)• rand(3,3)*10 create a 3 by 3 matrix with random values(0, 10)

Page 7: 13-MatrixMath

the single dimension

z = rand( 10 ) gives a 10 x 10 matrix

q = ones( 10 ) gives a 10 x 10 matrix

Page 8: 13-MatrixMath

transposing - swap rows and columnsA = [ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 ]>> B = transpose(A)B = [16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1 ]

Page 9: 13-MatrixMath

transposing a row makes it a column

>> x = [0:6]x = 0 1 2 3 4 5 6>> y = transpose(x)y = 0 1 2 3 4 5 6

>>

Page 10: 13-MatrixMath

transposing a column makes it a rowy = 0 1 2 3 4 5 6>> z = transpose(y)z = 0 1 2 3 4 5 6

>>

Page 11: 13-MatrixMath

transposing a 2 x 5 matrix>> A = [ 5 7 9 0 12 16 3 44 1 8 ]A = 5 7 9 0 12 16 3 44 1 8>> B = transpose(A)B = 5 16 7 3 9 44 0 1 12 8

>>

2 x 5

5 x 2

Page 12: 13-MatrixMath

Individual Matrix elements• Let's start with the simple case of a vector and a

single subscript. The vector is v = [16 5 9 4 2 11 7 14]

• The subscript can be a single value. v(3) % Extract the third element ans = 9

• Colons work:v(1:4)ans =

16 5 9 4

Page 13: 13-MatrixMath

• Now consider indexing into a matrix. A = [ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 ]

• indexing in matrices is done using two subscripts - one for the rows and one for the columns. A(2,4) % Extract the element in row 2, column 4ans = 8

Page 14: 13-MatrixMath

who and whos

• who shows your variables• whos lists your variables and their

characteristics

clc and clear• clc clears the command window• clear erases all variables

Page 15: 13-MatrixMath

e.g. surface plot

• x=[1:10]• y=transpose(x)• %matrix mult:• z= y * x• figure(1)• surf(x,y,z)• figure(2)• z = rand(10)• surf(x,y,z)

Page 16: 13-MatrixMath

matrix multiplication?

• it's an odd operation• matrices can be multiplied if their inside

dimensions match:– (m x n) * (n x q) – e.g. a (5 x 4) CANNOT multiply a (2 x 3)

• the resulting matrix has the outside dimensions:– (m x n) * (n x q) = (m x q) matrix

• We'll learn how

Page 17: 13-MatrixMath

.mat files and the SAVE command

http://www.mathworks.com/help/matlab/ref/save.html

saves all the variables and current values in a .mat file

different than saving a script filemore like saving your desktop, or workspace,

not your commands

Page 18: 13-MatrixMath

the SAVE command

http://www.mathworks.com/help/matlab/ref/save.html

saves all the variables and current values in a .mat file

different than saving a script filemore like saving your desktop, or workspace,

not your commands

Page 19: 13-MatrixMath

file extension .mat is automatic

save myStuff % saves all variables to myStuff.mat

load myStuff % loads variables from myVars.mat

Page 20: 13-MatrixMath

Matrix Math, MATLAB, and data tables

Page 21: 13-MatrixMath

Reading

• Chapter 9 & 10 for Matrix Math,• Chapter 11 for data interpolation

Page 22: 13-MatrixMath

3 equations in 3 unknowns1 equation: 3 x + 4y + 16z = 12 many solutions (x=0, y=3, z=0 / x=4, y=0, z=0 / etc. )

2 equations: 3 x + 4y + 16z = 12 6x - 17y - 23z = -168 solutions become limited...

3 equations: 3 x + 4y + 16z = 12 6x - 17y - 23z = -168 x + 42y + 101 z = 140

one solution only: x = 62.0129 y = 70.2452 z = -28.4387

Page 23: 13-MatrixMath

matrices

• usually represent a table, or a data relationship• or - referring to C++ - correlated arrays

3 x + 4y + 16z = 12 3 4 16 126x - 17y - 23z = -168 6 -17 -23 = -168x + 42y + 101 z = 140 1 42 101 140

3 equations in 3 unknowns, represented by matrices

Page 24: 13-MatrixMath

row and column vectors a row vector: a b c d e f g h

usually reflects different variables

a column vector: abcdefg

usually reflects the SAME variable

Page 25: 13-MatrixMath

matrix dimensions: R x C• given as Rows x Columns 1 56 98 3 0 1 x 5

45 67 3 x 1 94

101 46 2 x 2 3 17

Page 26: 13-MatrixMath

matrix multiplication?

• it's an odd operation, not just multiplying corresponding elements

• matrices can be multiplied if their inside dimensions match:– (m x n) * (n x q) – e.g. a (5 x 4) CANNOT multiply a (2 x 3)

• the resulting matrix has the outside dimensions:– (m x n) * (n x q) = (m x q) matrix

Page 27: 13-MatrixMath

matrix multiplicationsimple

A * B = C

each Row in A x each Col in B = (Row,Col) item in C

a11b12 + a12b22 = c12

Page 28: 13-MatrixMath

1 2 3 * 1 2 = 22 284 5 6 3 4 49 64 5 6

2 X 3 * 3 X 2 results in a 2 x 2

inner dimensions must be the same

out dimensions reveal size

row 1 column 1 item (1, 1)

1*1 + 2*3 + 3*5 = 22

Page 29: 13-MatrixMath

Watch the video 5.4• Harvey explains how to multiply matrices

Page 30: 13-MatrixMath

3 equations in 3 unknownsx + y − z = 4 has many solutions

(e.g. 0,0,-4 or 0,4,0 or 2,2,0 etc...)two equations together....x + y − z = 4 have fewer solutionsx − 2y + 3z = −6

but three equations in three unknownsx + y − z = 4 x − 2y + 3z = −6 has exactly one solution2x + 3y + z = 7

Page 31: 13-MatrixMath

represent N equations in N unknowns1) x + y − z = 4 2) x − 2y + 3z = −6 3) 2x + 3y + z = 7

1 1 -1 x 41 -2 3 * y = -62 3 1 z 7

A = [1 1 -1; 1 -2 3; 2 3 1] % called coefficient matrix[ x; y; z ] % called the variables matrixC = [ 4; -6; 7 ] % called the constants matrix

Page 32: 13-MatrixMath

the identity matrix

• a square matrix with 1s in the diagonal and 0s everywhere else

I = eye(3) % yields a 3x3 Identity Matrix

Page 33: 13-MatrixMath

the Identity Matrix (1s in the diagonal)

Any matrix times an appropriately sized identity matrix yields itself

3x2 2x2 3x2 23 45 1 0 23 45 17 22 * 0 1 = 17 22 1 32 1 32Size of ID matrix: SQUARE, dictated by

COLUMNS of the multiplying matrix

Page 34: 13-MatrixMath

what is a matrix inverse?

• A matrix multiplied by it's Inverse yields the identity matrix

• Ainv * A = Identity• "Singular" matrices have no Inverse

Page 35: 13-MatrixMath

Why?

1) x + y − z = 4 2) x − 2y + 3z = −6 3) 2x + 3y + z = 7

multiply both sides by AInv:

1 1 -1 x 41 -2 3 * y = -62 3 1 z 7

x 4 AI * A * y = AI * -6 z 7

find AInv, and you can solve for x, y, z

A

Page 36: 13-MatrixMath

watch Harvey explain Matrix Math in Video 5.1

Page 37: 13-MatrixMath

let's try oneThe admission fee at a small fair is $1.50 for children and $4.00 for adults. On a certain day, 2200 people enter the fair and $5050 is collected. How many children and how many adults?

Lets call adults x and children y2200 people attend the fair x + y = 2200Child admission fee is $1.50, Adult admission fee is $4.00The total amount collected is $5050.00: 4x + 1.5y = 5050non-matrix way:Take the first equation and set it equal to x(subtract y from both sides x + y - y = 2200 - yx = 2200 - yNow since we have shown that x equals 2200-y we can substitute that for x in thesecond equation and solve for y4x + 1.5y = 50504(2200-y) + 1.5y = 5050

Page 38: 13-MatrixMath

8800 - 4y + 1.5y = 5050combine like terms: 8800 - 2.5y = 5050subtract 8800 from both sides: 8800 - 8800 - 2.5y = 5050 - 8800we have: -2.5y = -3750divide both sides by -2.5: -2.5y/-2.5 = -3750/-2.5y = 1500Answer: 1500 children attended the fairNow use this answer to find xTake the first equation and substitute y with 1500x + y = 2200x + 1500 = 2200x + 1500 - 1500 = 2200 - 1500x = 700Answer: 700 adults attended the fair

Page 39: 13-MatrixMath

or….x + y = 2200 1 1 x = 22004x + 1.5y = 5050 4 1.5 y 5050

find the Inverse of A: -.6 .4 1.6 -.4

-.6 .4 1 1 x = -.6 .4 22001.6 -.4 4 1.5 y 1.6 -.4 5050 x 700 y = 1500

Page 40: 13-MatrixMath

m file

Page 41: 13-MatrixMath
Page 42: 13-MatrixMath

summary• matrix multiplication (and its vector equivalent, the

"dot product") is essentially a transformation which combines properties.

• "dividing by a matrix" is only possible by multiplying by its inverse (i.e. dividing by 5, is the same as multiplying by .20, or 1/5, which is the inverse of 5).

• element-by-element multiplication is called the Hadamard product, (in MATLAB " .* ") and is used in compressing JPEGs, where display properties are represented by a matrix, .

Page 43: 13-MatrixMath

Use matrices in MATLAB to solve the following problem. You must display all matrices and the resultant 3-digit number as output.

• Consider a three-digit number given as “xyz”. For example, if the number were 123, x would be 1, y would be 2, and z would be 3. Remember that the number represented by xyz is actually (x*100) + (y*10) + z ...

• If you add up the digits of a 3-digit number, the sum is 11.If the digits are all reversed, the new number is 46 more than 5x the old number.The hundreds digit plus twice the tens digit is equal to the units digit.

• What is the number?

Page 44: 13-MatrixMath

numbers...

Page 45: 13-MatrixMath

If you add up the digits of a 3-digit number, the sum is 11.

x + y + z = 11

Page 46: 13-MatrixMath

If the digits are reversed, the new number is 46 more than five times the old number.

note: xyz is really 100x + 10y + z

100z + 10y + x = 5(100x + 10y + z) + 46100z + 10y + x = 500x + 50y + 5z +46(100z - 5z) + (10y -50y) + (x - 500x) = 4695z -40y -499x = 46

-499x -40y +95z = 46

Page 47: 13-MatrixMath

The hundreds digit plus twice the tens digit is equal to the units digit.

x + 2y = z

1x + 2y -1z = 0

Page 48: 13-MatrixMath

x + y + z = 11-499x -40y +95z = 461x + 2y -1z = 0

1 1 1 x 11-499 -40 95 * y = 461 2 -1 z 0

Page 49: 13-MatrixMath

Answer is 137A = [ 1 1 1 ; -499 -40 95; 1 2 -1]Z = [11; 46; 0] A_I = inv (A)Ans = A_I * Zx = Ans(1,:)y = Ans(2,:)z = Ans(3, :)