Scientific Computing Maastricht Science Program Week 2 Frans Oliehoek <frans.oliehoek@maastrichtuniversity.nl>
Recap What is scientific programming? Programming Arithmetic, IF, conditions, WHILE, FOR Matlab Cheat Sheet General form of linear equations a 0 + a 1 x 1 + a 2 x 2 + ... = 0 Finding the zeros of non-linear equations bisection Newton
This Lecture A very short introduction linear algebra Vectors & Matrices in Matlab LU factorization Floating Point Numbers Computation Computation Errors Computational Costs
A Very Short Introduction to Linear Algebra
Linear Algebra (LA) Linear Algebra deals with linear functions You know what that is! but higher dimensions R n → R m I can only give a very brief introduction covering only basic things Please: get a linear algebra book, open it! Watch some video lectures. E.g., the first couple at: http://web.mit.edu/18.06/www/videos.shtml
Motivation LA is the basis of many methods in science For us: Important to solve systems of linear equations a 11 x 1 + a 12 x 2 + ... + a 1n x n = c 1 a 21 x 1 + a 22 x 2 + ... + a 2n x n = c 2 a 1 x 1 + a 2 x 2 + ... = c ... a m1 x 1 + a m2 x 2 + ... + a mn x n = c m Arise in many problems, e.g.: Identifying gas mixture from peaks in spectrum fitting a line to data. (Next week)
Motivation LA is the basis of many methods in science For us: ● x j - the amount of gas of type j Important to solve systems of linear equations ● a ij - how much a gas of type j contributes to wavelength i a 11 x 1 + a 12 x 2 + ... + a 1n x n = c 1 ● c i - the height of the peak of a 21 x 1 + a 22 x 2 + ... + a 2n x n = c 2 wavelength i a 1 x 1 + a 2 x 2 + ... = c ... a m1 x 1 + a m2 x 2 + ... + a mn x n = c m Arise in many problems, e.g.: Identifying gas mixture from peaks in spectrum fitting a line to data. (Next week)
Linear System of Equations Example y = 0.5x + 1 y y = 2x − 3 x Infinitely many, 1 or no solution
Matrices A = [ − 8 ] − 2 3 6 A matrix with 5 2 m rows, B = [ 435 ] n columns 5 54 6 75 24 81 is a collection of numbers 25 5 represented as a table v = [ 3 6 ] − 2 A vector is a matrix that is w = [ 25 ] 1 row (row vector), or 5 75 1 column (column vector)
Matrices A = [ − 8 ] − 2 3 6 A matrix with 5 2 m rows, octave:1> A = [3, -2, 6; 5, 2, -8] B = [ 435 ] A = n columns 5 54 6 3 -2 6 75 24 81 is a collection of numbers 5 2 -8 25 5 represented as a table octave:2> w = [5;75;25] w = 5 v = [ 3 6 ] − 2 75 A vector is a matrix that is 25 w = [ 25 ] 1 row (row vector), or 5 75 1 column (column vector)
Matrices A = [ − 8 ] − 2 3 6 A matrix with 5 2 m rows, octave:1> A = [3, -2, 6; 5, 2, -8] B = [ 435 ] A = n columns 5 54 6 3 -2 6 75 24 81 is a collection of numbers 5 2 -8 25 5 represented as a table octave:2> w = [5;75;25] w = 5 v = [ 3 6 ] octave:3> a1 = [4:8] − 2 75 A vector is a matrix that is a1 = 25 w = [ 25 ] 1 row (row vector), or 4 5 6 7 8 5 75 octave:4> a2 = [4:2:8] 1 column (column vector) a2 = 4 6 8
Some Special Matrices I = [ 1 ] Square matrix: m=n 1 0 0 Identity matrix - 'eye(3)' 0 1 0 0 0 Zero matrix – 'zeros(m,n)' Types: diagonal, triangular (upper & lower) D = [ ∗ ] TU = [ ∗ ] TL = [ ∗ ] ∗ ∗ ∗ ∗ 0 0 ∗ 0 0 ∗ ∗ 0 ∗ 0 0 ∗ ∗ 0 0 0 ∗ ∗ 0 0 '*' denotes any number
Operations on Vectors - 1 We can perform operations on them! First: vectors. Next: generalization to matrices. Transpose : convert row ↔ column vector T = [ 6 ] 3 v = [ 3 6 ] v − 2 − 2 w = [ 25 ] 5 T = [ 5 25 ] w 75 75
Operations on Vectors - 1 We can perform operations on them! First: vectors. Next: generalization to matrices. octave:9> a = [1,4,-2498, 12.4] a = 1.0000 4.0000 -2498.0000 12.4000 Transpose : convert row ↔ column vector octave:10> a' ans = T = [ 6 ] 3 1.0000 v = [ 3 6 ] v 4.0000 − 2 − 2 -2498.0000 12.4000 w = [ 25 ] octave:11> a'' 5 ans = T = [ 5 25 ] w 75 75 1.0000 4.0000 -2498.0000 12.4000
Operations on Vectors - 2 Sum [ 1 3 ] + [ 10 30 ] = [ 11 33 ] 2 20 22 Product with scalar 5 ∗ [ 1 3 ] = [ 5 15 ] 2 10 Inner product (also: 'scalar product' or 'dot product') n T w = ∑ ( v ,w )= v v k w k k = 1
Operations on Vectors - 2 Sum [ 1 3 ] + [ 10 30 ] = [ 11 33 ] 2 20 22 Product with scalar 5 ∗ [ 1 3 ] = [ 5 15 ] 2 10 Inner product (also: 'scalar product' or 'dot product') n T w = ∑ v = [ 3 ] ,w = [ 30 ] ( v ,w )= v v k w k 1 10 k = 1 2 20 3 ] [ 30 ] 10 [ 1 2 = 1 ∗ 10 + 2 ∗ 20 + 3 ∗ 30 = 10 + 40 + 90 = 140 20
Operations on Vectors - 2 Sum [ 1 3 ] + [ 10 30 ] = [ 11 33 ] 2 20 22 Product with scalar 5 ∗ [ 1 3 ] = [ 5 15 ] 2 10 octave:4> a = [1;2;3] a = 1 Inner product (also: 'scalar product' or 'dot product') 2 3 n T w = ∑ v = [ 3 ] ,w = [ 30 ] octave:5> b = [4;5;6] ( v ,w )= v v k w k 1 10 b = k = 1 2 20 4 5 3 ] [ 30 ] 6 10 [ 1 2 = 1 ∗ 10 + 2 ∗ 20 + 3 ∗ 30 = 10 + 40 + 90 = 140 octave:6> dot(a,b) 20 ans = 32 octave:7> a'*b ans = 32
Operations on Vectors - 2 Sum [ 1 3 ] + [ 10 30 ] = [ 11 33 ] 2 20 22 Product with scalar 5 ∗ [ 1 3 ] = [ 5 15 ] 2 10 Inner product (also: 'scalar product' or 'dot product') n T w = ∑ v = [ 3 ] ,w = [ 30 ] ( v ,w )= v v k w k 1 10 k = 1 2 20 3 ] [ 30 ] 10 [ 1 2 = 1 ∗ 10 + 2 ∗ 20 + 3 ∗ 30 = 10 + 40 + 90 = 140 20 Outer product (also: 'vector product')
Vector Indexing Retrieve parts of vectors octave:12> a = [10, 20, 30, 40, 50, 60, 70] a = 10 20 30 40 50 60 70 octave:13> a(3) ans = 30 octave:14> a([2,4]) ans = 20 40 octave:16> a([4:end]) ans = 40 50 60 70
Vector Indexing Retrieve parts of vectors octave:12> a = [10, 20, 30, 40, 50, 60, 70] a = indexing with 10 20 30 40 50 60 70 another vector octave:13> a(3) ans = 30 octave:14> a([2,4]) ans = 20 40 special 'end' octave:16> a([4:end]) index ans = 40 50 60 70
Operations on Matrices - 1 Now matrices! Transpose : convert each row → column vector (or convert each column→ row vector) A = [ 300 ] T = [ 300 ] 1 2 3 1 10 100 A 10 20 30 2 20 200 100 200 3 30
Operations on Matrices - 1 Now matrices! Transpose : convert each row → column vector (or convert each column→ row vector) A = [ 300 ] T = [ 300 ] 1 2 3 1 10 100 A 10 20 30 2 20 200 100 200 3 30
Operations on Matrices - 1 Now matrices! Transpose : convert each row → column vector (or convert each column→ row vector) A = [ 300 ] T = [ 300 ] 1 2 3 1 10 100 A 10 20 30 2 20 200 100 200 3 30
Operations on Matrices - 1 Now matrices! Transpose : convert each row → column vector (or convert each column→ row vector) A = [ 300 ] T = [ 300 ] 1 2 3 1 10 100 A 10 20 30 2 20 200 100 200 3 30 T = [ 30 ] 1 10 B = [ 30 ] 1 2 3 B 2 20 10 20 3
Operations on Matrices - 2 Sum and product with scalar : pretty much the same [ 6 ] + [ 60 ] = [ 66 ] 1 2 3 10 20 30 11 22 33 4 5 40 50 44 55 5 ∗ [ 6 ] = [ 30 ] 1 2 3 5 10 15 4 5 20 25
Matrix Product Inner product → Matrix product C = AB C = m x n, A = m x p, B = p x n, A c j B Each entry of C is an inner product: c ij = r i [ .. ] = [ 60 ] [ ... ... ... 10 20 6 ] 1 2 3 190 ... ... 30 40 4 5 ... ... 50
Matrix Product Inner product → Matrix product octave:22> A = [10, 20; 30, 40; 50, 60] A = C = AB 10 20 30 40 C = m x n, A = m x p, B = p x n, 50 60 A c j octave:23> B = [1,2,3;4,5,6] B Each entry of C is an inner product: c ij = r i B = 1 2 3 [ .. ] = [ 60 ] [ 4 5 6 ... ... ... 10 20 6 ] 1 2 3 octave:24> A*B 190 ... ... 30 40 ans = 4 5 ... ... 50 90 120 150 190 260 330 290 400 510
Matrix Product Inner product → Matrix product octave:22> A = [10, 20; 30, 40; 50, 60] C = AB A = 10 20 30 40 C = m x n, A = m x p, B = p x n, 50 60 A c j Matrix size is B Each entry of C is an inner product: c ij = r i octave:25> Btrans = B' important Btrans = 1 4 [ .. ] = [ 60 ] [ 2 5 ... ... ... 10 20 6 ] 3 6 1 2 3 190 ... ... 30 40 4 5 octave:26> A*Btrans ... ... 50 error: operator *: nonconformant arguments (op1 is 3x2, op2 is 3x2)
Matrix-Vector Product Matrix-vector product is just a (frequently occurring) special case: Ab = [ a mn ] [ = [ b n ] c m ] a 11 ... a 1n b 1 c 1 ... ... ... ... ... a m1 ...
Matrix-Vector Product Also represents a system of equations! Ax = [ a mn ] [ x n ] = [ c m ] a 11 ... a 1n x 1 c 1 ... ... ... ... ... a m1 ... a 11 x 1 + a 12 x 2 + ... + a 1n x n = c 1 a 21 x 1 + a 22 x 2 + ... + a 2n x n = c 2 ... a m1 x 1 + a m2 x 2 + ... + a mn x n = c m
Recommend
More recommend