Scientific Computing 2013 Maastricht Science Program Week 3 Frans Oliehoek <frans.oliehoek@maastrichtuniversity.nl>
Recap Matlab...! Advanced calculator operator priorities, variable names, matlab functions Using scripts Example of data reductions using PCA Floating point numbers
This Lecture Vectors & Matrices in Matlab creating, indexing, using functions Given data: figure out how variables relate. E.g., given medical symptoms or measurements, what is the probability of some disease? Estimating functions from a number of data points. Interpolation, Least Squares Regression NOTE: It is a lot...!
Matrices & Vectors
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.
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.
Linear System of Equations Example y = 0.5x + 1 y y = 2x − 3 Infinitely many, one, x or no solution matrices make these easy work with Another reason to care about matrices and vectors: they can make complex problems easy to write down!
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
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 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
Approximation of Data and Functions
Approximations of Functions Function approximation: Replace a function by a simpler one Reasons: Integration: replace a complex function with one that is easy to integrate. Function may be very complex: e.g. result of simulation. Function may be unknown...
“Approximation of Data” 'the function unknown' it is only known at certain points x 0, y 0 , x 1, y 1 , ... , x n , y n but we also want the know at other points these points are called the data → “approximation of data” Interpolation : find a function that goes exactly through data point Regression : find a function that minimizes some error measure better for noisy data. Related terms: curve fitting, extrapolation, classification
Recommend
More recommend