jacobi and gauss-seidel iteration methods, use of software ... · excel homework mike renfro jacobi...

30
Outlines Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages Mike Renfro September 30, 2004 Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Pa

Upload: lephuc

Post on 30-Mar-2019

286 views

Category:

Documents


0 download

TRANSCRIPT

Outlines

Jacobi and Gauss-Seidel Iteration Methods, Use ofSoftware Packages

Mike Renfro

September 30, 2004

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

OutlinesPart I: Review of Previous LecturePart II: Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Review of Previous Lecture

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

OutlinesPart I: Review of Previous LecturePart II: Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi and Gauss-Seidel Iteration Methods, Use ofSoftware Packages

Jacobi Iteration MethodIntroductionExampleNotes on Convergence Criteria

Gauss-Seidel Iteration MethodIntroductionExample

Use of Software PackagesMATLABExcel

Homework

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Part I

Review of Previous Lecture

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Review of Previous Lecture

Cramer’s Rule

Gauss Elimination

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

Part II

Jacobi and Gauss-Seidel Iteration Method, Use ofSoftware Packages

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Jacobi Iteration Method: Introduction

Consider a system of equations in algebraic form

a11x1 + a12x2 + a13x3 + a14x4 + · · · + a1nxn =b1

a21x1 + a22x2 + a23x3 + a24x4 + · · · + a2nxn =b2

a31x1 + a32x2 + a33x3 + a34x4 + · · · + a3nxn =b3

a41x1 + a42x2 + a43x3 + a44x4 + · · · + a4nxn =b4

...

an1x1 + an2x2 + an3x3 + an4x4 + · · · + annxn =bn

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Jacobi Iteration Method: IntroductionThese n equations can be rewritten to isolate an unknown on oneside of each equation:

x1 =1

a11(b1 − a12x2 − a13x3 − a14x4 − · · · − a1nxn)

x2 =1

a22(b2 − a21x1 − a23x3 − a24x4 − · · · − a2nxn)

x3 =1

a33(b3 − a31x1 − a32x2 − a34x4 − · · · − a3nxn)

x4 =1

a44(b4 − a41x1 − a42x2 − a43x3 − · · · − a4nxn)

...

xn =1

ann(bn − an1x1 − an2x2 − an3x3 − · · · − an,n−1xn−1)

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

So What’s the Point?

It looks like all we’ve done at this point is some useless algebraicmanipulations. But if we substitute some assumed starting valuesfor x1, x2, · · · , xn on the right hand side of each of the rewrittenequations, we’ll get a new set of xi values on the left hand side. Ifwe repeat the process, substituting the just-calculated x valuesinto the right hand side of the equations, we should get closer andcloser to the actual values of x that solve the equations.

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Example

Example 3.19 (p.184) — Solve the following system of equationsusing the Jacobi iteration method with an initial guess of xi = 0:

−5x1 − x2 + 2x3 =1

2x1 + 6x2 − 3x3 =2

2x1 + x2 + 7x3 =32

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Example

Step 1: reformat the equations, solving the first one for x1, thesecond for x2, and the third for x3:

x1 = − 1

5(1 + x2 − 2x3)

x2 =1

6(2 − 2x1 + 3x3)

x3 =1

7(32 − 2x1 − x2)

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Example

Step 2: Substitute the initial guesses for xi into the right-hand sideof the equations:

x1 = − 1

5(1 + 0 − 2 (0))

x2 =1

6(2 − 2 (0) + 3 (0))

x3 =1

7(32 − 2 (0) − (0))

x(2)1 = − 0.2000

x(2)2 =0.3333

x(2)3 =4.5714

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Example

Step 3: Substitute the calculated values for for xi into theright-hand side of the equations:

x1 = − 1

5(1 + 0.3333 − 2 (4.5714))

x2 =1

6(2 − 2 (−0.2000) + 3 (4.5714))

x3 =1

7(32 − 2 (−0.2000) − (0.3333))

x(3)1 =1.5619

x(3)2 =2.6857

x(3)3 =4.5810

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Example

Step 4, 5, · · · : Continue substituting xi values into the right-handside of the equations and watch for them to converge to finalvalues:

Iteration number x1 x2 x3

1 0.0000 0.0000 0.00002 -0.2000 0.3333 4.57143 1.5619 2.6857 4.58104 1.0952 2.1032 3.74155 0.8760 1.8390 3.95806 1.0154 2.0204 4.05847 1.0193 2.0241 3.99278 0.9923 1.9899 3.99109 0.9984 1.9981 4.0037

10 1.0018 2.0023 4.0007

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Notes on Convergence Criteria

Like many of the iterative root-finding methods in Chapter 2, theJacobi iteration method is not guaranteed to converge on the exactanswer in every possible case and every possible initial guess.However, if the equations in the system are diagonally dominant,then the Jacobi iteration method is guaranteed to convergeregardless of the starting guess for x .Diagonal dominance is defined as the condition where thecoefficient along the diagonal on any row is larger in absolute valuethan the sum of the absolute values of the other coefficients on thesame row.

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Is Example 3.19 Diagonally Dominant?

−5x1 − x2 + 2x3 =1

2x1 + 6x2 − 3x3 =2

2x1 + x2 + 7x3 =32

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExampleNotes on Convergence Criteria

Is Example 3.19 Diagonally Dominant?

−5 −1 22 6 −32 1 7

x1

x2

x3

=

1232

| − 5| > | − 1| + |2|

|6| > |2| + | − 3|

|7| > |2| + |1|

Yes, this system of equations is diagonally dominant. We willconverge to the exact solution regardless of the values of xi westart with.

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExample

Gauss-Seidel Iteration: Introduction

Gauss-Seidel iteration is similar to Jacobi iteration, except thatnew values for xi are used on the right-hand side of the equationsas soon as they become available. It improves upon the Jacobimethod in two respects:

Convergence is quicker, since you benefit from the newer,more accurate xi values earlier.

Memory requirements are reduced by 50%, since you onlyneed to keep track of one set of xi values, not two sets.

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExample

Example

Example 3.20 (p.186) — Solve the following system of equationsusing the Gauss-Seidel iteration method with an initial guess ofxi = 0:

−5x1 − x2 + 2x3 =1

2x1 + 6x2 − 3x3 =2

2x1 + x2 + 7x3 =32

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExample

Example

Step 1: reformat the equations, solving the first one for x1, thesecond for x2, and the third for x3:

x1 = − 1

5(1 + x2 − 2x3)

x2 =1

6(2 − 2x1 + 3x3)

x3 =1

7(32 − 2x1 − x2)

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExample

Example

Step 2a: Substitute the initial guesses for xi into the right-handside of the first equation:

x(2)1 = −1

5(1 + 0 − 2 (0)) = −0.2000

Step 2b: Substitute the new x1 value with the initial guess for x3

into the second equation:

x(2)2 =

1

6(2 − 2 (−0.2000) + 3 (0)) = 0.4000

Step 2c: Substitute the new x1 and x2 values into the thirdequation:

x(2)3 =

1

7(32 − 2 (−0.2000) − (0.4000)) = 4.5714

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

IntroductionExample

Example

Step 3, 4, · · · : Repeat step 2 and watch for the xi values toconverge to an exact solution.

Iteration number x1 x2 x3

1 0.0000 0.0000 0.00002 -0.2000 0.4000 4.57143 1.5486 2.1029 3.82864 0.9109 1.9440 4.03355 1.0246 2.0085 3.99186 0.9950 1.9975 4.00187 1.0012 2.0005 3.99968 0.9997 1.9999 4.0001

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

MATLABExcel

Example 3.27

Find the solution to the following system of equations:−4 1 1 0

1 −4 0 11 0 −4 10 1 1 −4

x1

x2

x3

x4

=

−200−400

0−200

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

MATLABExcel

MATLAB Setup

>> A=[-4 1 1 -0; 1 -4 0 1; 1 0 -4 1; 0 1 1 -4]A =

-4 1 1 01 -4 0 11 0 -4 10 1 1 -4

>> b=[ -200; -400; 0; -200]b =

-200-400

0-200

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

MATLABExcel

2 MATLAB Solutions

>> x=inv(A)*bx =

100.0000150.000050.0000

100.0000>> x=A\bx =

100.0000150.000050.0000

100.0000

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

MATLABExcel

Example 3.29 (Corrected)

Use MATLAB to find the inverse of the matrix [A] given by

[A] =

1 1

213 · · · 1

n12

13

14 · · · 1

n+113

14

15 · · · 1

n+2...

......

...1n

1n+1

1n+2 · · · 1

2n−1

, n = 50

and find the error in the inverse matrix by calculating[C ] = [A][A]−1 and summing up the absolute values of the matrixelements. If there was no error at all, the sum would be n.

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

MATLABExcel

MATLAB Solution 1

>> A=hilb (50);>> result=inv(A)*A;Warning: Matrix is close to singular or badly scaled.

Results may be inaccurate. RCOND = 1.603366e -020.>> err=sum(abs(result (:)))err =

7.3886e+011

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

MATLABExcel

MATLAB Solution 2

>> A=hilb (50);>> result=A\A;Warning: Matrix is close to singular or badly scaled.

Results may be inaccurate. RCOND = 1.603366e -020.>> err=sum(abs(result (:)))err =

192.9856>>

The error on this version is much, much lower. This is why wedon’t use the inv() function on ill-conditioned matrices.

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

MATLABExcel

Excel

Microsoft Excel can also do matrix inversion, but it’s a bitcumbersome. However, if it’s the only tool you have at somepoint, it’s better than nothing. See the the ’matrix inversion.xls’file located with the lecture slides on the web.

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages

Jacobi Iteration MethodGauss-Seidel Iteration Method

Use of Software PackagesHomework

Homework

Solve Problem 3.108 using Gauss-Seidel iteration. Do a handsolution first for the first 5 steps, then write a Gauss-Seidelprogram in MATLAB that will converge to the exact solution.You should double-check your answer using MATLAB’s \

operator or another method, but credit is only given for theGauss-Seidel work.

Is there any way to rearrange the equations to ensure diagonaldominance? If there is, rearrange the equations accordinglybefore solving the problem. If not, explain why the system ofequations cannot be made diagonally dominant.

Mike Renfro Jacobi and Gauss-Seidel Iteration Methods, Use of Software Packages