MIT1_204S10_lec20

MIT1_204S10_lec20 - 1.204 Lecture 20 Linear systems...

This preview shows pages 1–7. Sign up to view the full content.

= 3 1.204 Lecture 20 Linear systems: Gaussian elimination LU decomposition Systems of Linear Equations 3x 0 + x 1 -2 x 2 2x 0 +4 x + 4x 1 + 3x 2 +3 x x 0 -3 x 1 1 -2 2 4 3 3 1 -3 0 x 0 x 1 x 2 = = 5 =3 5 35 = -5 5 35 -5 A x = b 3 x 3 3 x 1 3 x 1 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithm to Solve Linear System =a ij Create matrix x 0 x 1 x 2 = b 0 b 1 b 2 A x b 0 0 0 =a’ ij x 0 x 1 x 2 = Forward solve b’ 0 b’ 1 b’ 2 A’ x b’ 0 0 0 x 0 Back solve x 0 x 1 x 2 = b’ 0 b’ 1 b’ 2 x 1 x 2 A’ x b’ Gaussian Elimination: Forward Solve 3 1 -2 5 Q= 2 4 3 35 1 -3 0 -5 A b Form Q for convenience Do elementary row ops: Multiply rows Add/subtract rows Make column 0 have zeros below diagonal Pivot= 1/3 Pivot= 2/3 3 1 -2 0 10/3 13/3 0 -10/3 10/3 2/3 2/3 5 95/3 Row 1’= row 1 - (2/3) row 0 20/3 Row 2’=row 2 (1/3) row 0 Row 2= row 2 - (1/3) row 0 -20/3 Make column 1 have zeros below diagonal 3 1 -2 5 Pivot= -1 0 10/3 13/3 95/3 Row 2’’= row 2’ + 1 * row 1 0 0 15/3 75/3 2
Gaussian Elimination: Back Solve 3 1 -2 5 0 10/3 13/3 95/3 (15/3)x 2 =(75/3) x 2 = 5 0 0 15/3 75/3 3 1 -2 5 0 10/3 13/3 95/3 0 0 15/3 75/3 (10/3)x 1 + (13/3)*5= (95/3) x 1 = 3 0 10/3 13/3 95/3 0 0 15/3 75/3 3x 0 + 1*3 - 2*5 = 5 x 0 = 4 3 1 -2 5 A Complication 0 1 -2 5 2 4 3 3 5 35 Row 1’= row 1 - (2/0) row 0 1 -3 0 -5 Exchange rows: put largest pivot element in row: 2 4 3 35 0 1 -2 5 1 -3 0 -5 Do this as we process each column. If there is no nonzero element in a column, matrix is not full rank. 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Gaussian Elimination public class Gauss { public static double[] gaussian(double[][] a, double[] b) { int n = a length; int n = a.length; // Number of unknowns double[][] q = new double[n][n + 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) // Form q matrix q[i][j]= a[i][j]; q[i][n]= b[i]; } forward_solve(q); // Do Gaussian elimination back_solve(q); // Perform back substitution double[] x= new double[n]; // Extract column n of q, for (int i = 0; i < n; i++) // which contains the solution x x[i]= q[i][n]; return x; } Forward Solve private static void forward_solve(double[][] q) { int n = q.length; int m= q[0].length; for (int i = 0; i < n; i++) { // Find row w/max element in this int maxRow = i; // column, at or below diagonal for (int k = i + 1; k < n; k++) if (Math.abs(q[k][i]) > Math.abs(q[maxRow][i])) maxRow = k; if (maxRow != i) // If row not current row, swap for (int j = i; j < m; j++) { double t = q[i][j]; q[i][j]= q[maxRow][j]; q[maxRow][j]= t; } for (int j = i + 1; j < n; j++) { // Calculate pivot ratio double pivot = q[j][i] / q[i][i]; for (int k = i; k < m; k++) // Pivot operation itself q[j][k] -= q[i][k] * pivot; } } } 4
Back Substitution private static void back_solve(double[][] q) { int n = q length; int n = q.length; int m= q[0].length; for (int p= n; p < m; p++) { // Loop over p columns for (int j = n - 1; j >= 0; j--) { // Start at last row double t = 0.0; // t- temporary for (int k = j + 1; k < n; k++) t += q[j][k] * q[k][p]; q[j][p]= (q[j][p] - t) / q[j][j]; } } } Variations Multiple right hand sides: augment Q, solve all eqns at once 3 1 -2 5 7 87 2 4 3 35 75 -1 1 -3 0 -5 38 52 Matrix inversion (rarely done in practice) 31 3 1 -2 2 4 3 1 -3 0 100 1 0 0 0 1 0 0 0 1 ### # # # 0 # # 0 0 # @@@ @ @ @ @ @ @ @ @ @ A I A -1 A ? = I Q ? = A -1 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Invert public static double[][] invert(double[][] a) { int n = a.length; // Number of unknowns double[][] q = new double[n][n+n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) // Form q matrix q[i][j]= a[i][j]; // Form identity matrix in right half of q for (int i = 0; i < n; i++) q[i][n+i]= 1.0;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 15

MIT1_204S10_lec20 - 1.204 Lecture 20 Linear systems...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online