Professional Documents
Culture Documents
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.
a 1 x1 + a 2 x 2 + L + a n x n = s1
b1 x1 + b 2 x 2 +L + bn x n = s 2
M
d1 x1 + d 2 x 2 +L + d n x n = s n
the problem is one of solving for x1 , x2 , …, 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,
s 1 a 2 ( 0 ) a 3 (0 ) a
x1 = − x2 − x 3 − L − n x (n0 )
a 1 a1 a1 a1
s b b b
x 2 = 2 − 1 x 1(0 ) − 3 x 3(0 ) − L − n x (n0 )
b 12 b 2 b2 b2
M
s d d d
x n = n − 1 x 1(0 ) − 2 x (20 ) − L − n −1 x (n0−)1
an dn dn dn
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.
7x 1 + 3x 2 + x 3 = 18
2 x 1 − 9x 2 + 4 x 3 = 12
x 1 − 4 x 2 + 12 x 3 = 6
Rearrange these equations
18 3 1
x1 = − x2 − x3 ⇒ x 1 = 2.571 − 0.429x 2 − 0.143x 3
7 7 7
12 2 4
x2 = − + x1 + x3 ⇒ x 2 = −1.333 + 0.222x 1 + 0.444 x 3
9 9 9
6 1 4
x3 = − x1 + x 2 ⇒ x 3 = 0.500 − 0.083x 1 + 0.333x 2
12 12 12
Use as the initial estimates: x1 (0) = x2 (0) = x3 (0) = 0. Insert these estimates into these
equations yielding new estimates of the parameters.
Insert these updated estimates back into original equation again, yielding
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.
→ R→ →
0
−7 R
−R2
1 − 4 12 6 7 3 1 18 3 1 0 31 − 83 − 24
1 − 4 12 6 R +4 R 1 0 92 6 R −92 R 1 0 0 2.859
0 1 1 2
1 3 0 1 0 − 0.683
R→ r→
20 0 0 1 20 0
− 20 R 3
0 0 − 703 − 24 3 703 0 0 1 0.034 2 0 0 1 0.034
As one can see, the values using the Jacobi iterative method are very close. Following is
a Fortran program that can be used to use the Jacobi iteration to solve a set of equations.
The limitation now is that it is restricted to only a 3 x 3 matrix, due to formatting
procedures currently used in the program.
c Program Jacobi
c Program to solve a linear equation using the Jacobi Iteration
c method
c
IMPLICIT none
REAL*8 coef(3,4), d, dx(3), x(3,4), xn(3), xnp(3)
INTEGER i, iter, iterate,j, no, nv
DATA iterate /0/
c
c The data are entered into the program using a data file called
c jacobi.dat. It has the following row values
c number of equations
c number of variables
c x(1) x(2) x(3) solution for the first equation
c x(1) x(2) x(3) solution for the second equation
c x(1) x(2) x(3) solution for the third equation
c
OPEN (4, file = 'jacobi.dat')
OPEN (6, file = 'results')
c
c no is the number of equations and nv is the number of variables
c
read(4,*) no
do 5 i=1,no
xn(i) = 0.d0
5 continue
read(4,*) nv
write(6,901)
c
c The coefficients for the variables are read in the matrix x with
c the solution to the equations being the last column
c
do 10 i=1,no
read(4,*)(x(i,j),j=1,no+1)
c
c d is the coefficient for the variable that is being solved for
c it forms the denominator to compute the real number for the
c remaining coefficients
c
d = x(i,i)
do 7 j=1,no+1
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 x1 as in the Jacobi method. When solving for x2 ,
insert the just computed value for x1 . 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:
x1(1) = 2.571 − 0.429 x (20 ) − 0.143 x (30 ) = 2.571 − 0.429 (0 ) − 0.143 (0 ) = 2.571
x (21) = −1.333 + 0.222 x (11 ) + 0.444 x (30 ) = −1.333 + 0.222 (2.571) + 0.444 (0 ) = −0.762
x (31 ) = 0.500 − 0.083 x1(1) + 0.333 x (21 ) = 0.500 − 0.083 (2.571) + 0.333 (− 0.762 ) = 0.033
x (12 ) = 2.571 − 0.429x (21 ) − 0.143x (31 ) = 2.571 − 0.429 (− 0.762) − 0.143 (0.033) = 2.893
x (22 ) = −1.333 + 0.222 x (12 ) + 0.444 x (31) = −1.333 + 0.222 (2.893) + 0.444 (0.033) = −0.676
x (32 ) = 0.500 − 0.083x1( 2 ) + 0.333x (22 ) = 0.500 − 0.083 (2.893) + 0.333 (− 0.676 ) = 0.034
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)
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
The formulas for computing x1 and x2 within the spreadsheet are shown as:
It is a simple process of copying and pasting to add more lines to solve the equations. In
a similar fashion, the Gauss-Seidel method can also be programmed within Excel to
arrive at the same results, as shown in the following figure.
12x1 + 3x 2 + 4x 3 = 48
6 x1 + 15x 2 − 4 x 3 = 54.6
9x 1 − 4 x 2 + 6x 3 = 22.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:
The same results using the Gauss-Seidel method, same criteria, are: