170 likes | 289 Views
COMP 116: Introduction to Scientific Programming . Lecture 7 : Matrices and Linear Systems. 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. A gambling game. One matrix M
E N D
COMP 116: Introduction to Scientific Programming Lecture 7: Matrices and Linear Systems
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
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?
Today • Linear systems • Solving simultaneous (linear) equations
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
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)-1b • Or in matlabspeak: • x=inv(A)*b • Matlab shorthand >> x=A\b
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
Vector multiplication >> [1 2 3]*[4 5 6] ??? Error using ==> mtimes Inner matrix dimensions must agree. >> [1 2 3]*[4;5;6] ans = 32 >> [1 2 3]*[4 5 6]' ans = 32
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.
Matrix Math • Matrix multiplication
Matrix Math Exercise • Identity matrix
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.
Linear Equations: Solving Mathematical formulation One line: x = A\b MATLAB solution: