Docsity

Iterative Methods for Solving Linear Equations: Jacobi Iteration Method, Study notes of Engineering

Toggle sidebar
bg1
ITERATIVE METHODS FOR SOLVING LINEAR EQUATIONS
There are other methods that can be used to solve a set of linear equations that are based
on iteration. In these cases, an initial estimate of the parameters is estimated and then the
equations are solved, yielding an updated version of the parameters. These new values
are then inserted back into the equations and the process continues until the desired
solution is reached. The two iterative methods discussed here are the Jacobi method and
the Gauss-Seidel method.
Jacobi Iteration Method
Given a set of linear equations,
nnn2211
2nn2211
1nn2211
sxdxdxd
sxbxbxb
sxaxaxa
=+++
=+++
=+++
L
M
L
L
the problem is one of solving for x
1, x
2, …, x
n. The right hand side of these equations, si,
represents the solution. We begin by rearranging these equations in the form of solving
for the unknown parameters one equation at a time. Thus,
( ) ( ) ( )
( ) ( ) ( )
( ) ( ) ( )
01n
n
1n0
2
n
20
1
n
1
n
n
n
0
n
2
n0
3
2
30
1
2
1
12
2
2
0
n
1
n0
3
1
30
2
1
2
1
1
1
x
d
d
x
d
d
x
d
d
a
s
x
x
b
b
x
b
b
x
b
b
b
s
x
x
a
a
x
a
a
x
a
a
a
s
x
−
−
−−−−=
−−−−=
−−−−=
L
M
L
L
The superscript (0) indicates the initial estimate of the parameters. For the first pass,
these parameters are given the value zero. The equations are then solved which results in
an updated value of the parameters. These current estimates are then inserted back into
the equations and a newer set of parameters is arrived at by solving these equations. The
process continues until the solution converges.
As an example, take the following linear equations:
6x12x4x
12x4x9x2
18xx3x7
321
321
321
=+−
=+−
=++
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Iterative Methods for Solving Linear Equations: Jacobi Iteration Method and more Study notes Engineering in PDF only on Docsity!

ITERATIVE METHODS FOR SOLVING LINEAR EQUATIONS

There are other methods that can be used to solve a set of linear equations that are based on iteration. In these cases, an initial estimate of the parameters is estimated and then the equations are solved, yielding an updated version of the parameters. These new values are then inserted back into the equations and the process continues until the desired solution is reached. The two iterative methods discussed here are the Jacobi method and the Gauss-Seidel method.

Jacobi Iteration Method

Given a set of linear equations,

1 1 2 2 n n n

1 1 2 2 n n 2

1 1 2 2 n n 1

dx d x d x s

bx b x b x s

a x a x a x s

L

M

L

L

the problem is one of solving for x 1 , x 2 , …, xn. The right hand side of these equations, si, represents the solution. We begin by rearranging these equations in the form of solving for the unknown parameters one equation at a time. Thus,

( ) ( ) ( )

( ) ( ) ( )

( ) ( ) ( ) 0 n 1 n

0 n 1 2 n

0 2 1 n

1 n

n n

0 n 2

0 n 3 2

0 3 1 2

1 12

2 2

0 n 1

0 n 3 1

0 3 2 1

2 1

1 1

x d

d x d

d x d

d a

s x

x b

b x b

b x b

b b

s x

x a

a x a

a x a

a a

s x

−

L

M

L

L

The superscript (0) indicates the initial estimate of the parameters. For the first pass, these parameters are given the value zero. The equations are then solved which results in an updated value of the parameters. These current estimates are then inserted back into the equations and a newer set of parameters is arrived at by solving these equations. The process continues until the solution converges.

As an example, take the following linear equations:

x 4 x 12 x 6

2 x 9 x 4 x 12

7 x 3 x x 18

1 2 3

1 2 3

1 2 3

Rearrange these equations

3 1 2 3 1 2

2 1 3 2 1 3

1 2 3 1 2 3

x x 0. 500 0. 083 x 0. 333 x 12

x 12

x

x x 1. 333 0. 222 x 0. 444 x 9

x 9

x

x x 2. 571 0. 429 x 0. 143 x 7

x 7

x

Use as the initial estimates: x 1 (0)^ = x 2 (0)^ = x 3 (0)^ = 0. Insert these estimates into these equations yielding new estimates of the parameters.

( ) ( ) ( ) ( ) x^ ( )^0. 500 0. 083 ( 0 ) 0. 333 ( ) 0 0. 500

x 1. 333 0. 222 0 0. 444 0 1. 333

x 2. 571 0. 429 0 0. 143 0 2. 571

1 3

( 1 ) 2

( 1 ) 1

Insert these updated estimates back into original equation again, yielding

( ) ( ) ( ) ( ) x^ (^ )^0. 500 0. 083 ( 2. 571 ) 0. 333 ( 1. 333 ) 0. 159

x 1. 333 0. 222 2. 571 0. 444 0. 500 0. 540

x 2. 571 0. 429 1. 333 0. 143 0. 500 3. 071

2 3

( 2 ) 2

( 2 ) 1

This process is continued until the desired results are obtained. The following table shows the solutions arrived at after each iteration. These results are from the attached Fortran program. The output shows x(i) that are the parameters xi. Also output is the change in the parameters, dx(i), between each iteration.

How good are these results? Lets take our equations and put them into an augmented matrix and solve using Gauss-Jordan elimination.

SOLVING LINEAR EQUATION USING THE JACOBI ITERATION METHOD The estimated results after each iteration are shown as: Iteration x(1) x(2) x(3) dx(1) dx(2) dx(3) 1 2.57143 -1.33333 .50000 2.57143 -1.33333. 2 3.07143 -.53968 -.15873 .50000 .79365 -. 3 2.82540 -.72134 .06415 -.24603 -.18166. 4 2.87141 -.67695 .02410 .04601 .04439 -. 5 2.85811 -.68453 .03506 -.01330 -.00757. 6 2.85979 -.68261 .03365 .00168 .00192 -.

coef(i,j) = x(i,j)/d 7 end do c c Because the Jacobi method solves for the unknown variable with c respect to the current estimates of the other variables, the c coefficient for the variable is made to be zero for subsequent c use in the loop to compute the adjusted estimates c coef(i,i) = 0.d write(6,900)(x(i,j),j=1,nv+1) 10 end do write(6,902) do 13 i=1,no write(6,900)(coef(i,j),j=1,nv+1) 13 end do write(6,903) 15 iter = 0 c c iterate is just a counter to keep track of the number of iterations c iterate = iterate+ c c Solve for the estimate of the unknown parameters c do 20 i=1,no xnp(i) = coef(i,nv+1) do 18 j=1,nv xnp(i) = xnp(i) - coef(i,j)*xn(j) 18 end do 20 end do c c dx is a vector showing the change in the estimate of the variable c with respect to the estimated value used in the previous iteration c do 50 i=1,no dx(i) = xnp(i) - xn(i) c c Test to see if the change is greater than the threshold c If it is, then the variable iter is made equal to 1 c At the beginning of each loop, this value is made equal to 0 c If iter is 1 then this means to iterate again c if (dabs(dx(i)).gt.0.01d0) iter = 1 c c Update the estimated parameter value c xn(i) = xnp(i) 50 continue write(6,904)iterate,(xn(i),i=1,nv),(dx(i),i=1,nv) if (iter.gt.0) go to 15 900 FORMAT(5x,4(f10.4,5x)) 901 FORMAT(15x,'SOLVING LINEAR EQUATION USING THE JACOBI ITERATION MET 1HOD',//,'The coefficients to the equations with the solution at th 1e end are:',/) 902 FORMAT(//,'Rearranging the equations to solve for the unknown vari 1ables yields',/,5x,'the following coefficients: ',/) 903 FORMAT(//,'The estimated results after each iteration are shown as 1:'//,'Iteration',2x,'x(1)',9x,'x(2)',9x,'x(3)',7x,'dx(1)',7x,'dx( 2)',7x,'dx(3)') 904 FORMAT(i3,4x,6(f10.5,2x)) stop end

Gauss-Seidel Iterative Method

The Gauss-Seidel iterative method of solving for a set of linear equations can be thought of as just an extension of the Jacobi method. Start out using an initial value of zero for each of the parameters. Then, solve for x 1 as in the Jacobi method. When solving for x 2 , insert the just computed value for x 1. In other words, for each calculation, the most current estimate of the parameter value is used. To see how the Gauss-Seidel method works, lets use the values in the last example. The initial estimates are set to zero. Then, the results from the first iteration are shown as:

( ) ( ) (^) ( ) ( ) ( ) ( ) (^) ( ) ( )

x^ ( )^0. 500 0. 083 x( )^0. 333 x( )^0. 500 0. 083 ( 2. 571 ) 0. 333 ( 0. 762 ) 0. 033

x 1. 333 0. 222 x 0. 444 x 1. 333 0. 222 2. 571 0. 444 0 0. 762

x 2. 571 0. 429 x 0. 143 x 2. 571 0. 429 0 0. 1430 2. 571

1 2

1 1

1 3

0 3

1 1

( 1 ) 2

0 3

0 2

( 1 ) 1

The next iteration is performed in a similar fashion. It can be shown as:

( ) ( ) ( ) (^) ( ) ( ) ( ) ( ) ( ) (^) ( ) ( )

x^ ( )^0. 500 0. 083 x( )^0. 333 x( )^0. 500 0. 083 ( 2. 893 ) 0. 333 ( 0. 676 ) 0. 034

x 1. 333 0. 222 x 0. 444 x 1. 333 0. 2222. 893 0. 444 0. 033 0. 676

x 2. 571 0. 429 x 0. 143 x 2. 571 0. 429 0. 762 0. 1430. 033 2. 893

2 2

2 1

2 3

1 3

2 1

2 2

1 3

1 2

2 1

A Fortran program was written to solve this problem. The results are shown in the next table.

As with the Jacobi method, the results from the Gauss-Seidel method are essentially correct. The Fortran program used to compute the Jacobi iteration method was modified to solve for the Gauss-Seidel iterative method. The program is shown as follows:

c Program Gaus_sdl.for c Program to solve a linear equation using the Gauss-Seidel c Iteration method c IMPLICIT none REAL*8 coef(3,4), d, dx(3), x(3,4), xn(3), xnp(3)

SOLVING LINEAR EQUATION USING THE GAUSS-SEIDEL ITERATION METHOD The estimated results after each iteration are shown as: Iteration x(1) x(2) x(3) dx(1) dx(2) dx(3) 1 2.57143 -.76190 .03175 -2.57143 .76190 -. 2 2.89342 -.67624 .03347 -.32200 -.08566 -. 3 2.85646 -.68369 .03406 .03696 .00745 -. 4 2.85957 -.68273 .03412 -.00311 -.00096 -.

xn(i) = xn(i) - coef(i,j)*xn(j) 18 end do 20 end do c c dx is a vector showing the change in the estimate of the variable c with respect to the estimated value used in the previous iteration c do 50 i=1,no dx(i) = xnp(i) - xn(i) xnp(i) = xn(i) c c Test to see if the change is greater than the threshold c If it is, then the variable iter is made equal to 1 c At the beginning of each loop, this value is made equal to 0 c If iter is 1 then this means to iterate again c if (dabs(dx(i)).gt.0.01d0) iter = 1 c c Update the estimated parameter value c xn(i) = xnp(i) 50 continue write(6,904)iterate,(xn(i),i=1,nv),(dx(i),i=1,nv) if (iter.gt.0) go to 15 900 FORMAT(5x,4(f10.4,5x)) 901 FORMAT(15x,'SOLVING LINEAR EQUATION USING THE GAUSS-SEIDEL ITERATI 1ON METHOD',//,'The coefficients to the equations with the solution 1 at the end are:',/) 902 FORMAT(//,'Rearranging the equations to solve for the unknown vari 1ables yields',/,5x,'the following coefficients: ',/) 903 FORMAT(//,'The estimated results after each iteration are shown as 1:'//,'Iteration',2x,'x(1)',9x,'x(2)',9x,'x(3)',7x,'dx(1)',7x,'dx( 2)',7x,'dx(3)') 904 FORMAT(i3,4x,6(f10.5,2x)) stop end

A more sophisticated subroutine for solving the Gauss-Seidel is shown as [source unknown]:

subroutine gsitrn(a,b,x,n,ndim,niter,tol,ierr) c----------------------------------------------------------------------- c c Gauss-Seidel Iterative Method c ***************************** c c This subroutine obtaines the solution to n linear equations by Gauss- c Seidel iteration. An initial approximation is sent to the subroutine c in the vector x. The solution, as approximated by the subroutine is c returned in x. The iterations are continued until the maximum change c in any x component is less than tol. If this cannot be accomplished c in niter iterations, a non-zero error flag is returned. The matrix c is to be arranged so as to have the largest values on the diagonal. c (from "Applied Numerical Analysis," C.F. Gerald, p 138) c c c c INPUT/OUTPUT VARIABLES: c

c a(n,n) coefficient matrix with largest values on diagonal c b(n) right hand side vector c x(n) solution vector (initial guess) c n Dimension of the system you're solving c ndim Dimension of matrix a (Note: In the main program, c matrix a may have been dimensioned larger than c necessary, i.e. n, the size of the system you're c decomposing, may be smaller than ndim.) c niter Number of iterations c tol tolerance for solution c ierr Error code: ierr=0 - no errors; ierr=1 - the c solution was not obtained in maximum iterations c c----------------------------------------------------------------------- dimension a(ndim,ndim),b(ndim),x(ndim) ierr = 0 c c We can save some divisions by making all the diagonal c elements equal to unity: c do 1 i=1,n temp = 1.0 / a(i,i) b(i) = b(i) * temp do 2 j=1,n a(i,j) = a(i,j) * temp 2 continue 1 continue c c Now we perform the iterations. Store max change in x values for c testing against tol. Outer loop limits iterations to niter: c do 3 iter=1,niter xmax = 0. do 4 i=1,n temp = x(i) x(i) = b(i) do 5 j=1,n if(i.ne.j) then x(i) = x(i) - a(i,j)*x(j) endif 5 continue if(abs(x(i)-temp).gt.xmax) xmax = abs(x(i)-temp) 4 continue if(xmax.le.tol) return 3 continue c c Normal exit from the loop means non-convergent solution. Flag the c error and pass control back to the calling routine: c ierr = 1 return end

While the Gauss-Seidel method appears to be the best in this example, this is not always the case. In fact, it is very possible that the solution from either of these methods could be in error. For example, lets look at the following equations:

9 x 4 x 6 x 22. 3

6 x 15 x 4 x 54. 6

12 x 3 x 4 x 48

1 2 3

1 2 3

1 2 3

Then, using a criteria of 0.1 (this is the significant figures for the input values) to stop the iterations, the solution using the Jacobi method can be shown to be:

SOLVING LINEAR EQUATION USING THE JACOBI ITERATION METHOD

The estimated results after each iteration are shown as:

Iteration x(1) x(2) x(3) dx(1) dx(2) dx(3) 1 4.00000 3.64000 3.71667 4.00000 3.64000 3. 2 1.85111 3.03111 .14333 -2.14889 -.60889 -3. ! 13 2.65235 3.05960 1.84979 .04225 -.01281. 14 2.61850 3.07234 1.77788 -.03384 .01274 -.

The same results using the Gauss-Seidel method, same criteria, are:

SOLVING LINEAR EQUATION USING THE GAUSS-SEIDEL ITERATION METHOD

The estimated results after each iteration are shown as:

Iteration x(1) x(2) x(3) dx(1) dx(2) dx(3) 1 4.00000 2.04000 -.92333 -4.00000 -2.04000. 2 3.79778 1.87467 -.73022 .20222 .16533 -. 3 3.77474 1.93538 -.65519 .02304 -.06071 -.

These are markedly different results.