Lecture 7. Gaussian Elimination with Pivoting. David Semeraro. University of Illinois at Urbana-Champaign. February 11, 2014

Similar documents
Lecture 9. Errors in solving Linear Systems. J. Chaudhry (Zeb) Department of Mathematics and Statistics University of New Mexico

Solving Linear Systems of Equations

Outline. Math Numerical Analysis. Errors. Lecture Notes Linear Algebra: Part B. Joseph M. Mahaffy,

Linear Algebraic Equations

Numerical Methods I Solving Square Linear Systems: GEM and LU factorization

Scientific Computing: Dense Linear Systems

Numerical Analysis FMN011

Chapter 2 - Linear Equations

Review Questions REVIEW QUESTIONS 71

Linear Algebra Section 2.6 : LU Decomposition Section 2.7 : Permutations and transposes Wednesday, February 13th Math 301 Week #4

CS227-Scientific Computing. Lecture 4: A Crash Course in Linear Algebra

Computational Methods. Systems of Linear Equations

1 Error analysis for linear systems

COURSE Numerical methods for solving linear systems. Practical solving of many problems eventually leads to solving linear systems.

ANALYTICAL MATHEMATICS FOR APPLICATIONS 2018 LECTURE NOTES 3

Topics. Review of lecture 2/11 Error, Residual and Condition Number. Review of lecture 2/16 Backward Error Analysis The General Case 1 / 22

MTH 464: Computational Linear Algebra

Scientific Computing: An Introductory Survey

Notes on Row Reduction

Roundoff Analysis of Gaussian Elimination

CHAPTER 6. Direct Methods for Solving Linear Systems

Scientific Computing

CME 302: NUMERICAL LINEAR ALGEBRA FALL 2005/06 LECTURE 6

LU Factorization. Marco Chiarandini. DM559 Linear and Integer Programming. Department of Mathematics & Computer Science University of Southern Denmark

LU Factorization. LU factorization is the most common way of solving linear systems! Ax = b LUx = b

5 Solving Systems of Linear Equations

CSE 160 Lecture 13. Numerical Linear Algebra

Since the determinant of a diagonal matrix is the product of its diagonal elements it is trivial to see that det(a) = α 2. = max. A 1 x.

5. Direct Methods for Solving Systems of Linear Equations. They are all over the place...

Lecture Notes to Accompany. Scientific Computing An Introductory Survey. by Michael T. Heath. Chapter 2. Systems of Linear Equations

Numerical Linear Algebra

Dense LU factorization and its error analysis

Numerical Analysis: Solutions of System of. Linear Equation. Natasha S. Sharma, PhD

The matrix will only be consistent if the last entry of row three is 0, meaning 2b 3 + b 2 b 1 = 0.

Math 1314 Week #14 Notes

POLI270 - Linear Algebra

Review of matrices. Let m, n IN. A rectangle of numbers written like A =

Math 4A Notes. Written by Victoria Kala Last updated June 11, 2017

Linear Algebra Linear Algebra : Matrix decompositions Monday, February 11th Math 365 Week #4

Next topics: Solving systems of linear equations

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 1 x 2. x n 8 (4) 3 4 2

EECS 275 Matrix Computation

Example: Current in an Electrical Circuit. Solving Linear Systems:Direct Methods. Linear Systems of Equations. Solving Linear Systems: Direct Methods

Gaussian Elimination for Linear Systems

Example: 2x y + 3z = 1 5y 6z = 0 x + 4z = 7. Definition: Elementary Row Operations. Example: Type I swap rows 1 and 3

MATH 3511 Lecture 1. Solving Linear Systems 1

7.5 Operations with Matrices. Copyright Cengage Learning. All rights reserved.

Conditioning and Stability

Scientific Computing: Solving Linear Systems

MAC1105-College Algebra. Chapter 5-Systems of Equations & Matrices

Matrix decompositions

Lecture 12: Solving Systems of Linear Equations by Gaussian Elimination

Lecture 12 (Tue, Mar 5) Gaussian elimination and LU factorization (II)

Ax = b. Systems of Linear Equations. Lecture Notes to Accompany. Given m n matrix A and m-vector b, find unknown n-vector x satisfying

Topics. Vectors (column matrices): Vector addition and scalar multiplication The matrix of a linear function y Ax The elements of a matrix A : A ij

1 Last time: linear systems and row operations

14.2 QR Factorization with Column Pivoting

6.241 Dynamic Systems and Control

Name: INSERT YOUR NAME HERE. Due to dropbox by 6pm PDT, Wednesday, December 14, 2011

Lecture 2 Systems of Linear Equations and Matrices, Continued

MAC Learning Objectives. Learning Objectives (Cont.) Module 10 System of Equations and Inequalities II

Linear Systems of n equations for n unknowns

Chapter 4. Solving Systems of Equations. Chapter 4

Solving Linear Systems Using Gaussian Elimination. How can we solve

Lecture Note 2: The Gaussian Elimination and LU Decomposition

Chapter 1 Linear Equations. 1.1 Systems of Linear Equations

CPE 310: Numerical Analysis for Engineers

The Solution of Linear Systems AX = B

System of Linear Equations

Introduction to Numerical Analysis

A Review of Matrix Analysis

Bindel, Fall 2011 Intro to Scientific Computing (CS 3220) Week 3: Wednesday, Jan 9

1300 Linear Algebra and Vector Geometry

Matrix decompositions

Gaussian Elimination and Back Substitution

Jim Lambers MAT 610 Summer Session Lecture 2 Notes

CS412: Lecture #17. Mridul Aanjaneya. March 19, 2015

Chapter 1: Systems of Linear Equations

Finite Mathematics Chapter 2. where a, b, c, d, h, and k are real numbers and neither a and b nor c and d are both zero.

Matrices. A matrix is a method of writing a set of numbers using rows and columns. Cells in a matrix can be referenced in the form.

LECTURES 4/5: SYSTEMS OF LINEAR EQUATIONS

Notes on Conditioning

Lecture 1 Systems of Linear Equations and Matrices

1 Backward and Forward Error

AM 205: lecture 6. Last time: finished the data fitting topic Today s lecture: numerical linear algebra, LU factorization

4. Direct Methods for Solving Systems of Linear Equations. They are all over the place...

Y = ax + b. Numerical Applications Least-squares. Start with Self-test 10-1/459. Linear equation. Error function: E = D 2 = (Y - (ax+b)) 2

LU Factorization a 11 a 1 a 1n A = a 1 a a n (b) a n1 a n a nn L = l l 1 l ln1 ln 1 75 U = u 11 u 1 u 1n 0 u u n 0 u n...

Midterm 1 Review. Written by Victoria Kala SH 6432u Office Hours: R 12:30 1:30 pm Last updated 10/10/2015

6 Linear Systems of Equations

Example: 2x y + 3z = 1 5y 6z = 0 x + 4z = 7. Definition: Elementary Row Operations. Example: Type I swap rows 1 and 3

Chapter 2: Approximating Solutions of Linear Systems

AM 205: lecture 6. Last time: finished the data fitting topic Today s lecture: numerical linear algebra, LU factorization

Math 552 Scientific Computing II Spring SOLUTIONS: Homework Set 1

CE 601: Numerical Methods Lecture 7. Course Coordinator: Dr. Suresh A. Kartha, Associate Professor, Department of Civil Engineering, IIT Guwahati.

A Review of Linear Algebra

Applied Mathematics 205. Unit II: Numerical Linear Algebra. Lecturer: Dr. David Knezevic

1 Problem 1 Solution. 1.1 Common Mistakes. In order to show that the matrix L k is the inverse of the matrix M k, we need to show that

Solving linear systems (6 lectures)

Direct Methods for Solving Linear Systems. Simon Fraser University Surrey Campus MACM 316 Spring 2005 Instructor: Ha Le

Transcription:

Lecture 7 Gaussian Elimination with Pivoting David Semeraro University of Illinois at Urbana-Champaign February 11, 2014 David Semeraro (NCSA) CS 357 February 11, 2014 1 / 41

Naive Gaussian Elimination Algorithm Forward Elimination + Backward substitution = Naive Gaussian Elimination David Semeraro (NCSA) CS 357 February 11, 2014 2 / 41

Goals for today... Identify why our basic GE method is naive : identify where the errors come from? division by zero, near-zero Propose strategies to eliminate the errors partial pivoting, complete pivoting, scaled partial pivoting Investigate the cost: does pivoting cost too much? Try to answer How accurately can we solve a system with or without pivoting? Analysis tools: norms, condition number,... David Semeraro (NCSA) CS 357 February 11, 2014 3 / 41

Why is our basic GE naive? Example Example A = 0 2 3 4 5 6 7 8 9 1e 10 2 3 A = 4 5 6 7 8 9 David Semeraro (NCSA) CS 357 February 11, 2014 4 / 41

The Need for Pivoting Solve: 2 4 2 2 4 A = 1 2 4 3 3 3 8 2 b = 5 7 1 1 6 3 7 Note that there is nothing wrong with this system. A is full rank. The solution exists and is unique. Form the augmented system. 2 4 2 2 1 2 4 3 3 3 8 2 1 1 6 3 4 5 7 7 David Semeraro (NCSA) CS 357 February 11, 2014 5 / 41

The Need for Pivoting Subtract 1/2 times the first row from the second row, add 3/2 times the first row to the third row, add 1/2 times the first row to the fourth row. The result of these operations is: 2 4 2 2 0 0 5 2 0 3 5 5 0 3 5 4 4 7 1 5 The next stage of Gaussian elimination will not work because there is a zero in the pivot location, ã 22. David Semeraro (NCSA) CS 357 February 11, 2014 6 / 41

The Need for Pivoting Swap second and fourth rows of the augmented matrix. 2 4 2 2 4 0 3 5 4 5 0 3 5 5 1 0 0 5 2 7 Continue with elimination: subtract (1 times) row 2 from row 3. 2 4 2 2 4 0 3 5 4 5 0 0 0 1 4 0 0 5 2 7 David Semeraro (NCSA) CS 357 February 11, 2014 7 / 41

The Need for Pivoting Another zero has appear in the pivot position. Swap row 3 and row 4. 2 4 2 2 4 0 3 5 4 5 0 0 5 2 7 0 0 0 1 4 The augmented system is now ready for backward substitution. David Semeraro (NCSA) CS 357 February 11, 2014 8 / 41

another example [ ] [ ] [ ε 1 x1 1 = 1 1 x 2 2] Example With Naive GE, [ ] [ ] [ ] ε 1 x1 1 0 (1 1 ε ) = x 2 2 1 ε Solving for x 1 and x 2 we get x 2 = 2 1/ε 1 1/ε x 1 = 1 x 2 ε For ε 10 20, x 1 0, x 2 1 David Semeraro (NCSA) CS 357 February 11, 2014 9 / 41

Pivoting Strategies Partial Pivoting: Exchange only rows Exchanging rows does not affect the order of the x i For increased numerical stability, make sure the largest possible pivot element is used. This requires searching in the partial column below the pivot element. Partial pivoting is usually sufficient. David Semeraro (NCSA) CS 357 February 11, 2014 10 / 41

Partial Pivoting To avoid division by zero, swap the row having the zero pivot with one of the rows below it. Rows completed in forward elimination. 0 * Row with zero pivot element Rows to search for a more favorable pivot element. To minimize the effect of roundoff, always choose the row that puts the largest pivot element on the diagonal, i.e., find i p such that a ip,i = max( a k,i ) for k = i,..., n David Semeraro (NCSA) CS 357 February 11, 2014 11 / 41

Partial Pivoting: Usually sufficient, but not always Partial pivoting is usually sufficient Consider [ 2 2c 1 1 ] 2c 2 With Partial Pivoting, the first row is the pivot row: [ ] 2 2c 2c 0 1 c 2 c and for large c: [ 2 2c 0 c ] 2c c so that y = 1 and x = 0. (exact is x = y = 1) The pivot is selected as the largest in the column, but it should be the largest relative to the full submatrix. David Semeraro (NCSA) CS 357 February 11, 2014 12 / 41

More Pivoting Strategies Full (or Complete) Pivoting: Exchange both rows and columns Column exchange requires changing the order of the x i For increased numerical stability, make sure the largest possible pivot element is used. This requires searching in the pivot row, and in all rows below the pivot row, starting the pivot column. Full pivoting is less susceptible to roundoff, but the increase in stability comes at a cost of more complex programming (not a problem if you use a library routine) and an increase in work associated with searching and data movement. David Semeraro (NCSA) CS 357 February 11, 2014 13 / 41

Full Pivoting 0 * Rows completed in forward elimination. Row with zero pivot element * Rows to search for a more favorable pivot element. Columns to search for a more favorable pivot element. David Semeraro (NCSA) CS 357 February 11, 2014 14 / 41

Scaled Partial Pivoting We simulate full pivoting by using a scale with partial pivoting. pick pivot element as the largest relative entry in the column (relative to the other entries in the row) do not swap, just keep track of the order of the pivot rows call this vector l = [l 1,..., l n ]. David Semeraro (NCSA) CS 357 February 11, 2014 15 / 41

SPP Process 1 Determine a scale vector s. For each row s i = max 1 j n a ij 2 initialize l = [l 1,..., l n ] = [1,..., n]. 3 select row j to be the row with the largest ratio a li 1 s li 1 i n 4 swap l j with l 1 in l 5 Now we need n 1 multipliers for the first column: m 1 = a l i 1 a l1 1 6 So the index to the rows are being swapped, NOT the actual row vectors which would be expensive 7 finally use the multiplier m 1 times row l 1 to subtract from rows l i for 2 i n David Semeraro (NCSA) CS 357 February 11, 2014 16 / 41

SPP Process continued 1 For the second column in forward elimination, we select row j that yields the largest ratio of a li,2 2 i n s li 2 swap l j with l 2 in l 3 Now we need n 2 multipliers for the second column: m 2 = a l i,2 a l2 2 4 finally use the multiplier m 2 times row l 2 to subtract from rows l i for 3 i n 5 the process continues for row k 6 note: scale factors are not updated David Semeraro (NCSA) CS 357 February 11, 2014 17 / 41

An Example Consider 3 4 3 x 1 10 1 5 1 x 2 = 7 6 3 7 x 3 15 David Semeraro (NCSA) CS 357 February 11, 2014 18 / 41

Back Substitution... 1 The first equation corresponds to the last index l n : a ln nx n = b ln x n = b l n a ln n 2 The second equation corresponds to the second to last index l n 1 : x n 1 = 1 ( bln 1 a ) l n 1nx n a ln 1 n 1 David Semeraro (NCSA) CS 357 February 11, 2014 19 / 41

The Algorithms Listing 1: (forward) GE with SPP 1 Initialize l = [1,..., n] 2 Set s to be the max a ij in each row 3 for k = 1 to n 4 rmax = 0 5 for i = k to n 6 r = a li k/s li 7 if(r > rmax) 8 rmax = r 9 j = i 10 end 11 swap l j and l k 12 for i = k + 1 to n 13 xmult = a li k/a lk k 14 a lik = xmult 15 for j = k + 1 to n 16 a li j = a li j xmult a lk j 17 end 18 end 19 end See Gauss algorithm on page 89 of NMC7 David Semeraro (NCSA) CS 357 February 11, 2014 20 / 41

The Algorithms Note: the multipliers are stored in the location a li k in the text 1 for k = 1 to n 1 2 for i = k + 1 to n 3 b li = b li a li kb lk 4 end 5 end 6 x n = b ln /a lnn 7 for i = n 1 down to 1 8 sum = b li 9 for j = i + 1 to n 0 sum = sum a li jx j 1 end 2 x i = sum/a li,i 3 end Listing 2: (back solve) GE with SPP See Gauss algorithm on page 89 of NMC7 David Semeraro (NCSA) CS 357 February 11, 2014 21 / 41

Geometric Interpretation of Singularity Consider a 2 2 system describing two lines that intersect y = 2x + 6 y = 1 2 x + 1 The matrix form of this equation is [ ] [ ] [ 2 1 x1 6 = 1/2 1 x 2 1] The equations for two parallel but not intersecting lines are [ ] [ ] [ 2 1 x1 6 = 2 1 x 2 5] Here the coefficient matrix is singular (rank(a) = 1), and the system is inconsistent David Semeraro (NCSA) CS 357 February 11, 2014 22 / 41

Geometric Interpretation of Singularity The equations for two parallel and coincident lines are [ ] [ ] [ 2 1 x1 6 = 2 1 x 2 6] The equations for two nearly parallel lines are [ ] [ ] [ ] 2 1 x1 6 = 2 + δ 1 x 2 6 + δ David Semeraro (NCSA) CS 357 February 11, 2014 23 / 41

Geometric Interpretation of Singularity 8 6 4 2 0 8 6 4 2 0 A and b are consistent A is nonsingular 0 1 2 3 4 A and b are consistent A is singular 0 1 2 3 4 8 6 4 2 0 8 6 4 2 0 A and b are inconsistent A is singular 0 1 2 3 4 A and b are consistent A is ill conditioned 0 1 2 3 4 David Semeraro (NCSA) CS 357 February 11, 2014 24 / 41

Effect of Perturbations to b Consider the solution of a 2 2 system where [ ] 1 b = 2/3 One expects that the exact solutions to [ ] [ ] 1 1 Ax = and Ax = 2/3 0.6667 will be different. Should these solutions be a lot different or a little different? David Semeraro (NCSA) CS 357 February 11, 2014 25 / 41

Norms Vectors: Matrices: x p = ( x 1 p + x 2 p +... + x n p) 1/p x 1 = x 1 + x 2 +... + x n = n x i i=1 x = max ( x 1, x 2,..., x n ) = max ( x i ) i A = max x 0 A p = max x 0 A 1 = max 1 j n A = max 1 i m Ax x Ax p x p m a ij i=1 n a ij j=1 David Semeraro (NCSA) CS 357 February 11, 2014 26 / 41

Effect of Perturbations to b Perturb b with δb such that The perturbed system is The perturbations satisfy δb b 1, A(x + δx b ) = b + δb Aδx b = δb Analysis shows (see next two slides for proof) that δx b x A A 1 δb b Thus, the effect of the perturbation is small only if A A 1 is small. δx b x 1 only if A A 1 1 David Semeraro (NCSA) CS 357 February 11, 2014 27 / 41

Effect of Perturbations to b (Proof) Let x + δx b be the exact solution to the perturbed system A(x + δx b ) = b + δb (1) Expand Ax + Aδx b = b + δb Subtract Ax from left side and b from right side since Ax = b Aδx b = δb Left multiply by A 1 δx b = A 1 δb (2) David Semeraro (NCSA) CS 357 February 11, 2014 28 / 41

Effect of Perturbations to b (Proof, p. 2) Take norm of equation (2) δx b = A 1 δb Applying consistency requirement of matrix norms δx b A 1 δb (3) Similarly, Ax = b gives b = Ax, and b A x (4) Rearrangement of equation (4) yields 1 A x b (5) David Semeraro (NCSA) CS 357 February 11, 2014 29 / 41

Effect of Perturbations to b (Proof) Multiply Equation (4) by Equation (3) to get δx b x A A 1 δb b (6) Summary: If x + δx b is the exact solution to the perturbed system A(x + δx b ) = b + δb then δx b x A A 1 δb b David Semeraro (NCSA) CS 357 February 11, 2014 30 / 41

Effect of Perturbations to A Perturb A with δa such that The perturbed system is δa A 1, Analysis shows that (A + δa)(x + δx A ) = b δx A x + δx A A A 1 δa A Thus, the effect of the perturbation is small only if A A 1 is small. δx A x + δx A 1 only if A A 1 1 David Semeraro (NCSA) CS 357 February 11, 2014 31 / 41

Effect of Perturbations to both A and b Perturb both A with δa and b with δb such that The perturbation satisfies δa A 1 and δb b 1 (A + δa)(x + δx) = b + δb Analysis shows that δx x + δx A A 1 1 A A 1 δa A [ δa A + δb ] b Thus, the effect of the perturbation is small only if A A 1 is small. δx x + δx 1 only if A A 1 1 David Semeraro (NCSA) CS 357 February 11, 2014 32 / 41

Condition number of A The condition number κ(a) A A 1 indicates the sensitivity of the solution to perturbations in A and b. The condition number can be measured with any p-norm. The condition number is always in the range 1 κ(a) κ(a) is a mathematical property of A Any algorithm will produce a solution that is sensitive to perturbations in A and b if κ(a) is large. In exact math a matrix is either singular or non-singular. κ(a) = for a singular matrix κ(a) indicates how close A is to being numerically singular. A matrix with large κ is said to be ill-conditioned David Semeraro (NCSA) CS 357 February 11, 2014 33 / 41

Computational Stability In Practice, applying Gaussian elimination with partial pivoting and back substitution to Ax = b gives the exact solution, ˆx, to the nearby problem (A + E)ˆx = b where E ε m A Gaussian elimination with partial pivoting and back substitution gives exactly the right answer to nearly the right question. Trefethen and Bau David Semeraro (NCSA) CS 357 February 11, 2014 34 / 41

Computational Stability An algorithm that gives the exact answer to a problem that is near to the original problem is said to be backward stable. Algorithms that are not backward stable will tend to amplify roundoff errors present in the original data. As a result, the solution produced by an algorithm that is not backward stable will not necessarily be the solution to a problem that is close to the original problem. Gaussian elimination without partial pivoting is not backward stable for arbitrary A. If A is symmetric and positive definite, then Gaussian elimination without pivoting in backward stable. David Semeraro (NCSA) CS 357 February 11, 2014 35 / 41

The Residual Let ˆx be the numerical solution to Ax = b. ˆx x (x is the exact solution) because of roundoff. The residual measures how close ˆx is to satisfying the original equation It is not hard to show that r = b Aˆx ˆx x ˆx κ(a) r b Small r does not guarantee a small ˆx x. If κ(a) is large the ˆx returned by Gaussian elimination and back substitution (or any other solution method) is not guaranteed to be anywhere near the true solution to Ax = b. David Semeraro (NCSA) CS 357 February 11, 2014 36 / 41

Rules of Thumb Applying Gaussian elimination with partial pivoting and back substitution to Ax = b yields a numerical solution ˆx such that the residual vector r = b Aˆx is small even if the κ(a) is large. If A and b are stored to machine precision ε m, the numerical solution to Ax = b by any variant of Gaussian elimination is correct to d digits where d = log 10 (ε m ) log 10 (κ(a)) David Semeraro (NCSA) CS 357 February 11, 2014 37 / 41

Rules of Thumb d = log 10 (ε m ) log 10 (κ(a)) Example: NUMPY computations have ε m 2.2 10 16. For a system with κ(a) 10 10 the elements of the solution vector will have d = log 10 (2.2 10 16 ) log 10 ( 10 10 ) correct (decimal) digits = 16 10 = 6 David Semeraro (NCSA) CS 357 February 11, 2014 38 / 41

Summary of Limits to Numerical Solution of Ax = b 1 κ(a) indicates how close A is to being numerically singular 2 If κ(a) is large, A is ill-conditioned and even the best numerical algorithms will produce a solution, ˆx that cannot be guaranteed to be close to the true solution, x 3 In practice, Gaussian elimination with partial pivoting and back substitution produces a solution with a small residual even if κ(a) is large. r = b Aˆx David Semeraro (NCSA) CS 357 February 11, 2014 39 / 41

Python and numpy Consider the scalar equation 5x = 20 = x = (5) 1 20 The extension to a system of equations is, of course Ax = b = x = A 1 b where A 1 b is the formal solution to Ax = b In numpy notation the system is solved with 1 x = np. linalg. solve(a,b) David Semeraro (NCSA) CS 357 February 11, 2014 40 / 41

Python and numpy Given an n n matrix A, and an n 1 vector b the \ operator performs a sequence of tests on the A matrix. MATLAB attempts to solve the system with the method that gives the least roundoff and the fewest operations. When A is an n n matrix the Python statement: 1 x = np. linalg. solve(a,b) solves the linear system by using the LINPACK routines DGESV or ZGESVṪhese routines solve a dense linear system using gausian elimination with partial pivoting. David Semeraro (NCSA) CS 357 February 11, 2014 41 / 41