Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 116: Introduction to Scientific Programming Lecture 7: Matrices and Linear Systems.

Similar presentations


Presentation on theme: "COMP 116: Introduction to Scientific Programming Lecture 7: Matrices and Linear Systems."— Presentation transcript:

1 COMP 116: Introduction to Scientific Programming Lecture 7: Matrices and Linear Systems

2 Recap exercises Indexing: what does A(3:7,5:end) return? Creating: how do you create this matrix? Statistical functions for matrices: ◦ min, max, sum, mean, std

3 A gambling game One matrix M Two teams: the house and the gambler Gambler picks a column of M, house picks an element from this column. The gambler wins this amount ◦ How much should the house charge the gambler for playing this game? ◦ How much should the gambler pay for playing this game?

4 Today Linear systems ◦ Solving simultaneous (linear) equations

5 Simultaneous equations Solve this set of (math) equations ◦ 5x+7y=17 ◦ 2x+3y=7 Now solve this one ◦ 5x+7y+z+2w=30 ◦ 3x+4y+8z+w=39 ◦ 9x+y+4z+2w=31 ◦ 6x+2y+5z+8w=57

6 Linear Equations

7 Solving linear equations Ax=b ◦ A is an m x n matrix  Rows of A correspond to equations  Columns of A correspond to variables ◦ x is a n x 1 matrix (n element column vector) ◦ b is a m x 1 matrix ( m element column) In general if m equals n, then x=(A) -1 b ◦ Or in matlabspeak:  x=inv(A)*b  Matlab shorthand >> x=A\b

8 Exercise Solve these set of equations in the Matlab way ◦ Convert to Ax=b format ◦ Then x=inv(A)*b Set I ◦ 5x+7y=17 ◦ 2x+3y=7 Set II ◦ 5x+7y+z+2w=30 ◦ 3x+4y+8z+w=39 ◦ 9x+y+4z+2w=31 ◦ 6x+2y+5z+8w=57

9 Matrix multiplication

10 Vector multiplication >> [1 2 3]*[4;5;6] ans = 32 >> [1 2 3]*[4 5 6] ??? Error using ==> mtimes Inner matrix dimensions must agree. >> [1 2 3]*[4 5 6]' ans = 32

11 Matrix-Vector Multiplication

12 Linear Equations Key idea: Can write a complete linear system in vector notation: Ax=b. Since x is unknown, we need some way to express it in terms of A and b.

13 Matrix Math Matrix multiplication

14 Matrix Math Exercise Identity matrix

15 Matrix Math Inverse >> A*inv(A) ans = 1.0000 0 0.0000 1.0000 >> A\A ans = 1 0 0 1 Inverse needs to exist.

16 Linear Equations: Solving One line: x = A\b Mathematical formulation MATLAB solution:

17 Matrix element naming convention a11a12a13a14 a21a22a23a24 a31a32a33a34


Download ppt "COMP 116: Introduction to Scientific Programming Lecture 7: Matrices and Linear Systems."

Similar presentations


Ads by Google