Gauss–Seidel method: Difference between revisions

From testwiki
Jump to navigation Jump to search
Reduce first person, stylistic updates
 
(No difference)

Latest revision as of 15:18, 25 September 2024

Template:Short description In numerical linear algebra, the Gauss–Seidel method, also known as the Liebmann method or the method of successive displacement, is an iterative method used to solve a system of linear equations. It is named after the German mathematicians Carl Friedrich Gauss and Philipp Ludwig von Seidel. Though it can be applied to any matrix with non-zero elements on the diagonals, convergence is only guaranteed if the matrix is either strictly diagonally dominant,[1] or symmetric and positive definite. It was only mentioned in a private letter from Gauss to his student Gerling in 1823.[2] A publication was not delivered before 1874 by Seidel.[3]

Description

Let 𝐀𝐱=𝐛 be a square system of Template:Mvar linear equations, where:

𝐀=[a11a12a1na21a22a2nan1an2ann],𝐱=[x1x2xn],𝐛=[b1b2bn].

When 𝐀 and 𝐛 are known, and 𝐱 is unknown, the Gauss–Seidel method can be used to iteratively approximate 𝐱. The vector 𝐱(0) denotes the initial guess for 𝐱, often 𝐱i(0)=0 for i=1,2,...,n. Denote by 𝐱(k) the k-th approximation or iteration of 𝐱, and by 𝐱(k+1) the approximation of 𝐱 at the next (or k+1-th) iteration.

Matrix-based formula

The solution is obtained iteratively via 𝐋𝐱(k+1)=𝐛𝐔𝐱(k), where the matrix 𝐀 is decomposed into a lower triangular component 𝐋, and a strictly upper triangular component 𝐔 such that 𝐀=𝐋+𝐔.[4] More specifically, the decomposition of A into L* and U is given by:

𝐀=[a1100a21a220an1an2ann]𝐋+[0a12a1n00a2n000]𝐔.

Why the matrix-based formula works

The system of linear equations may be rewritten as:

𝐀𝐱=𝐛(𝐋+𝐔)𝐱=𝐛𝐋𝐱+𝐔𝐱=𝐛𝐋𝐱=𝐛𝐔𝐱

The Gauss–Seidel method now solves the left hand side of this expression for 𝐱, using the previous value for 𝐱 on the right hand side. Analytically, this may be written as 𝐱(k+1)=𝐋1(𝐛𝐔𝐱(k)).

Element-based formula

However, by taking advantage of the triangular form of 𝐋, the elements of 𝐱(k+1) can be computed sequentially for each row i using forward substitution:[5] xi(k+1)=1aii(bij=1i1aijxj(k+1)j=i+1naijxj(k)),i=1,2,,n.

Notice that the formula uses two summations per iteration which can be expressed as one summation jiaijxj that uses the most recently calculated iteration of xj. The procedure is generally continued until the changes made by an iteration are below some tolerance, such as a sufficiently small residual.

Discussion

The element-wise formula for the Gauss–Seidel method is related to that of the (iterative) Jacobi method, with an important difference:

In Gauss-Seidel, the computation of 𝐱(k+1) uses the elements of 𝐱(k+1) that have already been computed, and only the elements of 𝐱(k) that have not been computed in the (k+1)-th iteration. This means that, unlike the Jacobi method, only one storage vector is required as elements can be overwritten as they are computed, which can be advantageous for very large problems.

However, unlike the Jacobi method, the computations for each element are generally much harder to implement in parallel, since they can have a very long critical path, and are thus most feasible for sparse matrices. Furthermore, the values at each iteration are dependent on the order of the original equations.

Gauss-Seidel is the same as successive over-relaxation with ω=1.

Convergence

The convergence properties of the Gauss–Seidel method are dependent on the matrix 𝐀. Namely, the procedure is known to converge if either:

The Gauss–Seidel method may converge even if these conditions are not satisfied.

Golub and Van Loan give a theorem for an algorithm that splits 𝐀 into two parts. Suppose 𝐀=𝐌𝐍 is nonsingular. Let r=ρ(𝐌1𝐍) be the spectral radius of 𝐌1𝐍. Then the iterates 𝐱(k) defined by 𝐌𝐱(k+1)=𝐍𝐱(k)+𝐛 converge to 𝐱=𝐀1𝐛 for any starting vector 𝐱(0) if 𝐌 is nonsingular and r<1.[8]

Algorithm

Since elements can be overwritten as they are computed in this algorithm, only one storage vector is needed, and vector indexing is omitted. The algorithm goes as follows:

algorithm Gauss–Seidel method is
    inputs: Template:Var, Template:Var
    Template:Nowrap

    Template:Nowrap
    repeat until convergence
        for Template:Var from 1 until Template:Var do
            Template:Nowrap
            for Template:Var from 1 until Template:Var do
                if Template:VarTemplate:Var then
                    Template:Nowrap
                end if
            end (Template:Var-loop)
            Template:Nowrap
        end (Template:Var-loop)
        check if convergence is reached
    end (repeat)

Examples

An example for the matrix version

A linear system shown as 𝐀𝐱=𝐛 is given by: 𝐀=[163711]and𝐛=[1113].

Use the equation 𝐱(k+1)=𝐋1(𝐛𝐔𝐱(k)) in the form 𝐱(k+1)=𝐓𝐱(k)+𝐜 where:

𝐓=𝐋1𝐔and𝐜=𝐋1𝐛.

Decompose 𝐀 into the sum of a lower triangular component 𝐋 and a strict upper triangular component U: 𝐋=[160711]and𝐔=[0300].

The inverse of 𝐋 is: 𝐋1=[160711]1=[0.06250.00000.03980.0909].

Now find: 𝐓=[0.06250.00000.03980.0909][0300]=[0.0000.18750.0000.1194],𝐜=[0.06250.00000.03980.0909][1113]=[0.68750.7439].

With 𝐓 and 𝐜 the vectors 𝐱 can be obtained iteratively.

First of all, choose 𝐱(0), for example 𝐱(0)=[1.01.0]. The closer the guess to the final solution, the fewer iterations the algorithm will need.

Then calculate: 𝐱(1)=[0.0000.18750.0000.1193][1.01.0]+[0.68750.7443]=[0.50000.8636].𝐱(2)=[0.0000.18750.0000.1193][0.50000.8636]+[0.68750.7443]=[0.84940.6413].𝐱(3)=[0.0000.18750.0000.1193][0.84940.6413]+[0.68750.7443]=[0.80770.6678].𝐱(4)=[0.0000.18750.0000.1193][0.80770.6678]+[0.68750.7443]=[0.81270.6646].𝐱(5)=[0.0000.18750.0000.1193][0.81270.6646]+[0.68750.7443]=[0.81210.6650].𝐱(6)=[0.0000.18750.0000.1193][0.81210.6650]+[0.68750.7443]=[0.81220.6650].𝐱(7)=[0.0000.18750.0000.1193][0.81220.6650]+[0.68750.7443]=[0.81220.6650].

As expected, the algorithm converges to the solution: 𝐱=𝐀1𝐛[0.81220.6650].

In fact, the matrix Template:Mvar is strictly diagonally dominant, but not positive definite.

Another example for the matrix version

Another linear system shown as 𝐀𝐱=𝐛 is given by:

𝐀=[2357]and𝐛=[1113].

Use the equation 𝐱(k+1)=𝐋1(𝐛𝐔𝐱(k)) in the form 𝐱(k+1)=𝐓𝐱(k)+𝐜 where:

𝐓=𝐋1𝐔and𝐜=𝐋1𝐛.

Decompose 𝐀 into the sum of a lower triangular component 𝐋 and a strict upper triangular component 𝐔: 𝐋=[2057]and𝐔=[0300].

The inverse of 𝐋 is: 𝐋1=[2057]1=[0.5000.0000.3570.143].

Now find: 𝐓=[0.5000.0000.3570.143][0300]=[0.0001.5000.0001.071],𝐜=[0.5000.0000.3570.143][1113]=[5.5002.071].

With 𝐓 and 𝐜 the vectors 𝐱 can be obtained iteratively.

First of all, we have to choose 𝐱(0), for example 𝐱(0)=[1.12.3]

Then calculate: 𝐱(1)=[01.50001.071][1.12.3]+[5.5002.071]=[2.0500.393].𝐱(2)=[01.50001.071][2.0500.393]+[5.5002.071]=[4.9111.651].𝐱(3)=.

In a test for convergence we find that the algorithm diverges. In fact, the matrix 𝐀 is neither diagonally dominant nor positive definite. Then, convergence to the exact solution 𝐱=𝐀1𝐛=[3829] is not guaranteed and, in this case, will not occur.

An example for the equation version

Suppose given n equations and a starting point 𝐱0. At any step in a Gauss-Seidel iteration, solve the first equation for x1 in terms of x2,,xn; then solve the second equation for x2 in terms of x1 just found and the remaining x3,,xn; and continue to xn. Then, repeat iterations until convergence is achieved, or break if the divergence in the solutions start to diverge beyond a predefined level.

Consider an example: 10x1x2+2x3=6,x1+11x2x3+3x4=25,2x1x2+10x3x4=11,3x2x3+8x4=15.

Solving for x1,x2,x3 and x4 gives: x1=x2/10x3/5+3/5,x2=x1/11+x3/113x4/11+25/11,x3=x1/5+x2/10+x4/1011/10,x4=3x2/8+x3/8+15/8.

Suppose Template:Math is the initial approximation, then the first approximate solution is given by: x1=3/5=0.6,x2=(3/5)/11+25/11=3/55+25/11=2.3272,x3=(3/5)/5+(2.3272)/1011/10=3/25+0.232721.1=0.9873,x4=3(2.3272)/8+(0.9873)/8+15/8=0.8789.

Using the approximations obtained, the iterative procedure is repeated until the desired accuracy has been reached. The following are the approximated solutions after four iterations.

x1 x2 x3 x4
0.6 2.32727 −0.987273 0.878864
1.03018 2.03694 −1.01446 0.984341
1.00659 2.00356 −1.00253 0.998351
1.00086 2.0003 −1.00031 0.99985

The exact solution of the system is Template:Math.

An example using Python and NumPy

The following iterative procedure produces the solution vector of a linear system of equations:

import numpy as np

ITERATION_LIMIT = 1000

# initialize the matrix
A = np.array(
    [
        [10.0, -1.0, 2.0, 0.0],
        [-1.0, 11.0, -1.0, 3.0],
        [2.0, -1.0, 10.0, -1.0],
        [0.0, 3.0, -1.0, 8.0],
    ]
)
# initialize the RHS vector
b = np.array([6.0, 25.0, -11.0, 15.0])

print("System of equations:")
for i in range(A.shape[0]):
    row = [f"{A[i,j]:3g}*x{j+1}" for j in range(A.shape[1])]
    print("[{0}] = [{1:3g}]".format(" + ".join(row), b[i]))

x = np.zeros_like(b, np.float_)
for it_count in range(1, ITERATION_LIMIT):
    x_new = np.zeros_like(x, dtype=np.float_)
    print(f"Iteration {it_count}: {x}")
    for i in range(A.shape[0]):
        s1 = np.dot(A[i, :i], x_new[:i])
        s2 = np.dot(A[i, i + 1 :], x[i + 1 :])
        x_new[i] = (b[i] - s1 - s2) / A[i, i]
    if np.allclose(x, x_new, rtol=1e-8):
        break
    x = x_new

print(f"Solution: {x}")
error = np.dot(A, x) - b
print(f"Error: {error}")

Produces the output:

System of equations:
[ 10*x1 +  -1*x2 +   2*x3 +   0*x4] = [  6]
[ -1*x1 +  11*x2 +  -1*x3 +   3*x4] = [ 25]
[  2*x1 +  -1*x2 +  10*x3 +  -1*x4] = [-11]
[  0*x1 +   3*x2 +  -1*x3 +   8*x4] = [ 15]
Iteration 1: [ 0.  0.  0.  0.]
Iteration 2: [ 0.6         2.32727273 -0.98727273  0.87886364]
Iteration 3: [ 1.03018182  2.03693802 -1.0144562   0.98434122]
Iteration 4: [ 1.00658504  2.00355502 -1.00252738  0.99835095]
Iteration 5: [ 1.00086098  2.00029825 -1.00030728  0.99984975]
Iteration 6: [ 1.00009128  2.00002134 -1.00003115  0.9999881 ]
Iteration 7: [ 1.00000836  2.00000117 -1.00000275  0.99999922]
Iteration 8: [ 1.00000067  2.00000002 -1.00000021  0.99999996]
Iteration 9: [ 1.00000004  1.99999999 -1.00000001  1.        ]
Iteration 10: [ 1.  2. -1.  1.]
Solution: [ 1.  2. -1.  1.]
Error: [  2.06480930e-08  -1.25551054e-08   3.61417563e-11   0.00000000e+00]

Program to solve arbitrary number of equations using Matlab

The following code uses the formula xi(k+1)=1aii(bij<iaijxj(k+1)j>iaijxj(k)),i=1,2,,nk=0,1,2,

function x = gauss_seidel(A, b, x, iters)
    for i = 1:iters
        for j = 1:size(A,1)
            x(j) = (b(j) - sum(A(j,:)'.*x) + A(j,j)*x(j)) / A(j,j);
        end
    end
end

See also

Notes

Template:Reflist

References

Template:CFDWiki

Template:Numerical linear algebra Template:Authority control