21
Lecture 7 Gaussian Elimination with Pivoting J. B. Schroder Department of Mathematics and Statistics University of New Mexico J. B. Schroder (UNM) Math/CS 375 1 / 21

Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

  • Upload
    others

  • View
    8

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Lecture 7Gaussian Elimination with Pivoting

J. B. Schroder

Department of Mathematics and StatisticsUniversity of New Mexico

J. B. Schroder (UNM) Math/CS 375 1 / 21

Page 2: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Naive Gaussian Elimination Algorithm

Remember:Forward Elimination+ Backward substitution= Naive Gaussian Elimination

J. B. Schroder (UNM) Math/CS 375 2 / 21

Page 3: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Goals for today. . .

Identify why our basic GE method is “naive”: identify where the errorscome from?

I division by zero, near-zero

Propose strategies to eliminate the errorsI 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 withoutpivoting?”

I Analysis tools: norms, condition number, . . .

J. B. Schroder (UNM) Math/CS 375 3 / 21

Page 4: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Why is our basic GE “naive”?

Example

A =

0 2 34 5 67 8 9

Example

A =

1e − 10 2 34 5 67 8 9

J. B. Schroder (UNM) Math/CS 375 4 / 21

Page 5: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

The Need for Pivoting

Solve:

A =

2 4 −2 −21 2 4 −3

−3 −3 8 −2−1 1 6 −3

b =

−4

577

Note that there is nothing ”wrong” with this system. A is full rank. The solutionexists and is unique.Form the augmented system.

2 4 −2 −21 2 4 −3

−3 −3 8 −2−1 1 6 −3

∣∣∣∣∣∣∣∣−4

577

J. B. Schroder (UNM) Math/CS 375 5 / 21

Page 6: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

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 −20 0 5 −20 3 5 −50 3 5 −4

∣∣∣∣∣∣∣∣−4

715

The next stage of Gaussian elimination will not work because there is a zeroin the pivot location, a22.

J. B. Schroder (UNM) Math/CS 375 6 / 21

Page 7: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

The Need for Pivoting

Swap second and fourth rows of the augmented matrix.2 4 −2 −20 3 5 −40 3 5 −50 0 5 −2

∣∣∣∣∣∣∣∣−4

517

Continue with elimination: subtract (1 times) row 2 from row 3.

2 4 −2 −20 3 5 −40 0 0 −10 0 5 −2

∣∣∣∣∣∣∣∣−4

5−4

7

J. B. Schroder (UNM) Math/CS 375 7 / 21

Page 8: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

The Need for Pivoting

Another zero has appear in the pivot position. Swap row 3 and row 4.2 4 −2 −20 3 5 −40 0 5 −20 0 0 −1

∣∣∣∣∣∣∣∣−4

57

−4

The augmented system is now ready for backward substitution.

J. B. Schroder (UNM) Math/CS 375 8 / 21

Page 9: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

another example

[ε 11 1

] [x1x2

]=

[12

]

ExampleWith Naive GE, [

ε 10 (1 − 1

ε)

] [x1x2

]=

[1

2 − 1ε

]Solving for x1 and x2 we get

x2 =2 − 1/ε1 − 1/ε

x1 =1 − x2

ε

For ε ≈ 10−20, x1 ≈ 0, x2 ≈ 1

J. B. Schroder (UNM) Math/CS 375 9 / 21

Page 10: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Pivoting Strategies

Partial Pivoting: Exchange only rowsExchanging rows does not affect the order of the xi

For increased numerical stability, make sure the largest possible pivotelement is used. This requires searching in the partial column below thepivot element.Partial pivoting is usually sufficient.

J. B. Schroder (UNM) Math/CS 375 10 / 21

Page 11: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Partial Pivoting

To avoid division by zero (small number), swap the row having the zero (smallnumber) pivot with one of the rows below it.

To minimize the effect of roundoff, always choose the row that puts the largestpivot element on the diagonal, i.e., find ip such that |aip,i| = max(|ak,i|) fork = i, . . . , n

J. B. Schroder (UNM) Math/CS 375 11 / 21

Page 12: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Partial Pivoting: Usually sufficient, but not always

Partial pivoting is usually sufficientConsider (first variable is x and second variable is y)[

2 2c1 1

∣∣∣∣ 2c2

]With Partial Pivoting, the first row is the pivot row:[

2 2c0 1 − c

∣∣∣∣ 2c2 − c

]and for large c on a machine, 1 − c→ −c and 2 − c→ −c:[

2 2c0 −c

∣∣∣∣ 2c−c

]so that x = 0 and y = 1. For large c, exact is y ≈ x ≈ 1.The pivot is selected as the largest in the column, but it should be thelargest relative to the full submatrix.

J. B. Schroder (UNM) Math/CS 375 12 / 21

Page 13: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

More Pivoting Strategies

Full (or Complete) Pivoting: Exchange both rows and columnsColumn exchange requires changing the order of the xi

For increased numerical stability, make sure the largest possible pivotelement is used. This requires searching in the pivot row, and in all rowsbelow the pivot row, starting in the pivot column.That is, you search through a square submatrix of A for the largestelement.Full pivoting is less susceptible to roundoff, but the increase in stabilitycomes at a cost of more complex programming (not a problem if you usea library routine) and an increase in work associated with searching anddata movement.

J. B. Schroder (UNM) Math/CS 375 13 / 21

Page 14: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Full Pivoting

J. B. Schroder (UNM) Math/CS 375 14 / 21

Page 15: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Scaled Partial Pivoting

We simulate full pivoting by using a scale with partial pivoting.pick pivot element as the largest entry in the column, but scale by thelargest entry in each row, i.e., consider maxi |ai,k/si| for finding the pivot incolumn ksi is the largest entry in row i, so that we can “simulate” full pivoting bychoosing the “largest” equation as the pivot row.do not swap, just keep track of the order of the pivot rowscall this vector ` = [`1, . . . , `n].

J. B. Schroder (UNM) Math/CS 375 15 / 21

Page 16: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

SPP Process1 Determine a scale vector s. For each row

si = max16j6n

|aij|

2 initialize ` = [`1, . . . , `n] = [1, . . . , n].3 select row j to be the row with the largest ratio

|a`i1|

s`i

1 6 i 6 n

4 swap `j with `1 in `

5 Now we need n − 1 multipliers for the first column:

m1 =a`i1

a`11

6 So the index to the rows are being swapped, NOT the actual row vectorswhich would be expensive

7 finally use the multiplier m1 times row `1 to subtract from rows `i for2 6 i 6 n

J. B. Schroder (UNM) Math/CS 375 16 / 21

Page 17: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

SPP Process continued

1 For the second column in forward elimination, we select row j that yieldsthe largest ratio of

|a`i,2|

s`i

2 6 i 6 n

2 swap `j with `2 in `

3 Now we need n − 2 multipliers for the second column:

m2 =a`i,2

a`22

4 finally use the multiplier m2 times row `2 to subtract from rows `i for3 6 i 6 n

5 the process continues for row k6 note: scale factors are not updated

J. B. Schroder (UNM) Math/CS 375 17 / 21

Page 18: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

An Example

Consider 2 4 −21 3 45 2 0

x1x2x3

=

6−12

J. B. Schroder (UNM) Math/CS 375 18 / 21

Page 19: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

Back Substitution. . .

1 Solve for xn using last index `n:

a`nnxn = b`n ⇒ xn =b`n

a`nn

2 Solve for xn−1 using the second to last index `n−1:

xn−1 =1

a`n−1n−1

(b`n−1 − a`n−1nxn

)

J. B. Schroder (UNM) Math/CS 375 19 / 21

Page 20: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

The Algorithms

Listing 1: (forward) GE with SPP1 Initialize ` = [1, . . . , n]2 Set s to be the max of rows3 for k = 1 to n4 rmax = 05 for i = k to n6 r = |a`ik/s`i |

7 if(r > rmax)8 rmax = r9 j = i

10 end

11 swap `j and `k

12 for i = k + 1 to n13 xmult = a`ik/a`kk

14 a`ik = xmult15 for j = k + 1 to n16 a`ij = a`ij − xmult · a`k j

17 end

18 end

19 end

See Gauss algorithm on page 89 of NMC7 (p267 NMC6)J. B. Schroder (UNM) Math/CS 375 20 / 21

Page 21: Lecture 7 - Gaussian Elimination with Pivotingschroder/2020_Spring_375/lecture07.pdfLecture 7 - Gaussian Elimination with Pivoting Author J. B. Schroder Created Date 2/20/2020 3:27:35

The AlgorithmsNote: the multipliers are stored in the location a`ik in the text

Listing 2: (back solve) GE with SPP1 for k = 1 to n − 12 for i = k + 1 to n3 b`i = b`i − a`ikb`k

4 end

5 end

6 xn = b`n/a`nn

7 for i = n − 1 down to 18 sum = b`i

9 for j = i + 1 to n10 sum = sum − a`ijxj

11 end

12 xi = sum/ali,i

13 end

See Solve algorithm on page 92 of NMC7 (p269 NMC6)

J. B. Schroder (UNM) Math/CS 375 21 / 21