14 - Matrices and Linear Equations

14 - Matrices and Linear Equations - Engineering 101 A...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 A programmer started to cuss Because getting to sleep was a fuss As he lay there in bed Looping 'round in his head while(!asleep()) while(!asleep()) {sheep++}; Quote of the Day Honest disagreement is often a good sign of progress. - Mahatma Gandhi Matrices and Linear Equations Matrices One very important data structure for doing engineering and scientific computation is the matrix. A matrix is a two dimensional array. A matrix can be represented as a vector of vectors. 3 2 5 4 4 1 0 2 -1 Matrices We can think of labeling each element of the matrix by its row and column A11 A12 A13 Aij = A21 A22 A23 A31 A32 A33 Matrices The numbers of rows and columns can be different from each other A11 A12 A13 Matrices and Vectors Matrices and vectors can be multiplied by each other. To do this the number of columns in the matrix must equal the number of elements in the vector. A11 A12 A13 A21 A22 A23 A31 A32 A33 A41 A42 A43 b1 b2 b3 Aij = A21 A22 A23 A31 A32 A33 A41 A42 A43 1 Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. A11 A12 A13 A21 A22 A23 A31 A32 A33 A41 A42 A43 b1 b2 b3 A11b1+A12b2+A13b3 Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. A11 A12 A13 A21 A22 A23 A31 A32 A33 A41 A42 A43 b1 b2 b3 A11b1+A12b2+A13b3 = = A21b1+A22b2+A23b3 Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. A11 A12 A13 A21 A22 A23 A31 A32 A33 A41 A42 A43 b1 b2 b3 A11b1+A12b2+A13b3 Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. A11 A12 A13 A21 A22 A23 A31 A32 A33 A41 A42 A43 b1 b2 b3 A11b1+A12b2+A13b3 = A21b1+A22b2+A23b3 A31b1+A32b2+A33b3 = A21b1+A22b2+A23b3 A31b1+A32b2+A33b3 A41b1+A42b2+A43b3 Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. 4 3 0 1 0 7 1 0 -1 0 0 1 3 2 1 4*3 + 0*2 + (-1)*1 4 3 0 1 Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. 0 7 1 0 -1 0 0 1 3 2 1 12 + 0 1 = 3*3 + 7*2 + 0 *1 0*3 + 1*2 + 0*1 1*3 + 0*2 + 1*1 = 9 + 14 + 0 0+2+0 3+0+1 2 Matrices and Vectors To perform the multiplication each row of the matrix is multiplied by the vector and all the results are added up. 4 3 0 1 0 7 1 0 -1 0 0 1 3 2 1 11 4 3 0 1 Matrices and Vectors A matrix times a vector gives another vector. An (M x N) matrix multiplied by a size N vector gives a size M vector 0 7 1 0 -1 0 0 1 3 2 1 11 = 23 2 4 = 23 2 4 Exercise 1 0 -1 122 -1 0 7 5 1 3 2 -5 6 4 2 -1 -6 8 37 -5 -6 45 6 8 Which program performs matrix multiplication? 1. =? 2. 3. More Matrix Multiplication The element xi,j at the intersection of row i and column j of the product matrix is the dot product (inner product) of row i of the first matrix and column j of the second matrix. Multiplying an mn matrix with an np matrix results in an mp matrix. A little something extra: Command Line Arguments What if we want to take input from the user right when the program is run? % myProgram inputfile outputfile Solution: Convert your "int main()" into: int main(int argc, char * argv[ ]) { ... } 3 What is the output using the given command line? int main(int argc, char * argv[ ]) int argc Specifies the number of arguments that were on the command line There is at least 1 (i.e. the program's name) int main(int argc, char * argv[ ]) { string command; int i = 1; while (i < argc) { command = argv[i]; i++; if (command == "All") cout << "Are "; else if (command == "Your") cout << "Belong "; else if (command == "Base") cout << "To Us! " << endl; else cout << "Make Your Time!"; } } % runProgram All Your Base . 1 Are Belong To Us! 2 Are Belong To Us! Make Your Time! 3 Someone set up us the bomb. 4 For great justice. char * argv[ ] This is a native array where each element is a C-string CTo conveniently use them you must do something like: string comLine; comLine = argv[1]; Matrices and Linear Equations Matrices are so useful because they can be used to represent linear equations: w=4ac x=3a+7b y=b z=a+c Matrices and Linear Equations Matrices are so useful because they can be used to represent linear equations: w=4ac x=3a+7b y=b z=a+c w x y z A11 A12 A13 a b c = A21 A22 A23 A31 A32 A33 A41 A42 A43 Matrices and Linear Equations Matrices are so useful because they can be used to represent linear equations: w=4ac x=3a+7b y=b z=a+c What are matrices good for? Consider the problem of tomography (CAT scans). A beam of radiation is passed through an object or person. By measuring the attenuation of the beam the density of the slice is determined. w x y z 4 0 7 1 0 -1 0 0 1 a b c = 3 0 1 4 Tomography How can we compute the mass through which each beam passes? First row: 8 0 = m0 Tomography How can we compute the mass through which each beam passes? First row: 8 0 = m0 Second row: 2 0 + 6 1 = m1 3 = 1.0 2 = 0.5 1 = 0.2 0 = 0.1 3 = 1.0 2 = 0.5 1 = 0.2 0 = 0.1 m0 m1 m2 m3 m3 m2 m1 m0 m0 m1 m2 m3 m3 m2 m1 m0 Tomography How can we compute the mass through which each beam passes? First row: 8 0 = m0 Second row: 2 0 + 6 1 = m1 Third row: 2 0 + 2 1 + 4 2 = m2 Tomography How can we compute the mass through which each beam passes? First row: 8 0 = m0 Second row: 2 0 + 6 1 = m1 Third row: 2 0 + 2 1 + 4 2 = m2 Fourth row: 2 0 + 2 1 + 2 2 + 2 3 = m3 3 = 1.0 2 = 0.5 1 = 0.2 0 = 0.1 3 = 1.0 2 = 0.5 1 = 0.2 0 = 0.1 m0 m1 m2 m3 m3 m2 m1 m0 m0 m1 m2 m3 m3 m2 m1 m0 Tomography How can we compute the mass through which each beam passes? 8 0 = m0 2 0 + 6 1 = m1 2 0 + 2 1 + 4 2 = m2 2 0 + 2 1 + 2 2 + 2 3 = m3 Tomography How can we compute the mass through which each beam passes? 8 0 = m0 2 0 + 6 1 = m1 2 0 + 2 1 + 4 2 = m2 2 0 + 2 1 + 2 2 + 2 3 = m3 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 m0 m1 m2 m3 3 = 1.0 2 = 0.5 1 = 0.2 0 = 0.1 3 = 1.0 2 = 0.5 1 = 0.2 0 = 0.1 = m0 m1 m2 m3 m3 m2 m1 m0 m0 m1 m2 m3 m3 m2 m1 m0 5 Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 m0 m1 m2 m3 Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 = m0 m1 m2 m3 = vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); set up identifiers This problem is fairly easy to encode: Set up the matrix Read in the densities Print out the masses Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 = m0 m1 m2 m3 = m0 m1 m2 m3 vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); for(int i = 0; i < A.size(); i ++) for(int j = 0; j < A[i].size(); j ++) if( i == j ) A[ i ][ j ] = 8 2* i; else if ( j < i ) A[ i ][ j ] = 2; set up the matrix vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); for(int i = 0; i < A.size(); i ++) for(int j = 0; j < A[i].size(); j ++) if( i == j ) A[ i ][ j ] = 8 2* i; else if ( j < i ) A[ i ][ j ] = 2; for(int i = 0; i< rho.size( ); i++){ cout << "Input rho " << i << " : " << endl; cin >> rho[ i ]; } read in the densities Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 Tomography 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 = m0 m1 m2 m3 = m0 m1 m2 m3 vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); for(int i = 0; i < A.size(); i ++) for(int j = 0; j < A[i].size(); j ++) if( i == j ) A[ i ][ j ] = 8 2* i; else if ( j < i ) A[ i ][ j ] = 2; for(int i = 0; i< rho.size( ); i++){ cout << "Input rho " << i << " : " << endl; cin >> rho[ i ]; } m = multiply(A, rho); calculate the masses vector <double> rho(4,0), m; vector < vector <double> > A(4,rho); for(int for(int i = 0; i < A.size(); i ++) A.size(); for(int for(int j = 0; j < A[i].size(); j ++) A[i if( i == j ) A[ i ][ j ] = 8 2* i; else if ( j < i ) A[ i ][ j ] = 2; for(int for(int i = 0; i< rho.size( ); i++){ rho.size( cout << "Input rho " << i << " : " << endl; endl; cin >> rho[ i ]; } m = multiply(A, rho); // see next slide for this function for (i=0; i< m.size( ); i++ ) cout << m.at( i ) << " "; (i m.size( m.at( print out the masses 6 Recall Matrix Multiply vector <double> multiply( vector < vector <double> > A, vector <double> v){ int i, j; vector <double> ans(A.size(),0); for(i=0; i< A.size(); i++) for(j=0; j< A[ i ].size(); j++) ans[ i ] = ans[ i ] + A[ i ][ j ]*v[ j ]; return ans; } Tomography Of course the problem of finding the mass of a slice is not the problem we are usually interested in solving. We can measure the masses of the slices by tomography. We want to know the density of the rings given the masses of the slices. This is straightforward if the matrix is triangular. The Inverse Problem 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 m0 m1 m2 m3 8 2 2 2 0 6 2 2 0 0 4 2 The Inverse Problem 0 0 0 2 0 1 2 3 m0 m1 m2 m3 = = Getting the first density is easy: A00 0 = m0 0 = m0 / A00 Getting the second density is a bit harder: A10 0 + A11 1 = m1 1 = (m1 - A10 0) / A11 The Inverse Problem 8 2 2 2 0 6 2 2 0 0 4 2 0 0 0 2 0 1 2 3 m0 m1 m2 m3 8 2 2 2 0 6 2 2 The Inverse Problem 0 0 4 2 0 0 0 2 0 1 2 3 m0 m1 m2 m3 = = Getting the third density is similar: A20 0 + A21 1 + A22 2 = m2 2 = (m2 A20 0 A21 1) / A22 The final solution: 0 = m0 / A00 1 = (m1 - A10 0) / A11 2 = (m2 A20 0 A21 1) / A22 3 = (m3 A30 0 A31 1 A32 2) / A33 7 Which of the functions solves the inverse problem given a matrix A and a vector m? 1. r0 = m0/A00 r1 = (m1A10*r0)/A11 r2 = (m2A20*r0A21*r1)/A22 r3 = (m3A30*r0A31*r1A32*r2)/A33 Some Inverse Problems are Easy To Solve If the matrix is lower triangular (the upper triangle is all zeros and the diagonal elements are all non-zero) the problem is easily solvable nonThe same is true if the matrix is upper triangular But most matrices are neither. 2. Next Lecture Intro to MATLAB 8 ...
View Full Document

This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Spring '07 term at University of Michigan.

Ask a homework question - tutors are online