Gauss–Seidel method

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, or symmetric and positive definite. It was only mentioned in a private letter from Gauss to his student Gerling in 1823. A publication was not delivered before 1874 by Seidel.

Description
Let $\mathbf A\mathbf x = \mathbf b$ be a square system of $n$ linear equations, where:

$$\mathbf A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix}, \qquad \mathbf{x} = \begin{bmatrix} x_{1} \\ x_2 \\ \vdots \\ x_n \end{bmatrix}, \qquad  \mathbf{b} = \begin{bmatrix} b_{1} \\ b_2 \\ \vdots \\ b_n \end{bmatrix}.$$

When $$\mathbf A$$ and $$\mathbf b$$ are known, and $$\mathbf x$$ is unknown, we can use the Gauss–Seidel method to approximate $$\mathbf x$$. The vector $$\mathbf x^{(0)}$$ denotes our initial guess for $$\mathbf x$$ (often $$\mathbf x^{(0)}_i=0$$ for $$i=1,2,...,n$$). We denote by $$\mathbf{x}^{(k)}$$ the $$k$$-th approximation or iteration of $$\mathbf{x}$$, and by $$\mathbf{x}^{(k+1)}$$ the approximation of $$\mathbf{x}$$ at the next (or $$k+1$$) iteration.

Matrix-based formula
The solution is obtained iteratively via $$ \mathbf L \mathbf{x}^{(k+1)} = \mathbf{b} - \mathbf U \mathbf{x}^{(k)}, $$ where the matrix $$\mathbf A$$ is decomposed into a lower triangular component $$\mathbf L$$, and a strictly upper triangular component $$\mathbf U$$ such that $$ \mathbf {A} = \mathbf L + \mathbf U $$. More specifically, the decomposition of $$A$$ into $$L_*$$ and $$U$$ is given by:

$$\mathbf A = \underbrace{ \begin{bmatrix} a_{11} & 0 & \cdots & 0 \\ a_{21} & a_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix} }_{\textstyle \mathbf L} + \underbrace{ \begin{bmatrix} 0 & a_{12} & \cdots & a_{1n} \\ 0 & 0 & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & 0 \end{bmatrix} }_{\textstyle \mathbf U} . $$

Why the matrix-based formula works
The system of linear equations may be rewritten as:
 * $$\begin{alignat}{1}

\mathbf A \mathbf x &= \mathbf b \\ (\mathbf L + \mathbf U) \mathbf x &= \mathbf b \\ \mathbf L \mathbf x+ \mathbf U \mathbf x &= \mathbf b \\ \mathbf L \mathbf{x} &= \mathbf{b} - \mathbf U \mathbf{x} \end{alignat} $$

The Gauss–Seidel method now solves the left hand side of this expression for $$\mathbf{x}$$, using the previous value for $$\mathbf{x}$$ on the right hand side. Analytically, this may be written as $$ \mathbf {x}^{(k+1)} = \mathbf L^{-1} \left(\mathbf{b} - \mathbf U \mathbf{x}^{(k)}\right). $$

Element-based formula
However, by taking advantage of the triangular form of $$\mathbf L$$, the elements of $$\mathbf{x}^{(k+1)}$$ can be computed sequentially for each row $$i$$ using forward substitution: $$ x^{(k+1)}_i = \frac{1}{a_{ii}} \left(b_i - \sum_{j=1}^{i-1}a_{ij}x^{(k+1)}_j - \sum_{j=i+1}^{n}a_{ij}x^{(k)}_j \right),\quad i=1,2,\dots,n. $$

Notice that the formula uses two summations per iteration which can be expressed as one summation $$\sum_{j \ne i} a_{ij}x_j$$ that uses the most recently calculated iteration of $$x_j$$. 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 $$\mathbf{x}^{(k+1)}$$ uses the elements of $$\mathbf{x}^{(k+1)}$$ that have already been computed, and only the elements of $$\mathbf{x}^{(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 $$\omega=1$$.

Convergence
The convergence properties of the Gauss–Seidel method are dependent on the matrix $$\mathbf A$$. Namely, the procedure is known to converge if either:
 * $$\mathbf A$$ is symmetric positive-definite, or
 * $$\mathbf A$$ is strictly or irreducibly diagonally dominant.

The Gauss–Seidel method sometimes converges even if these conditions are not satisfied.

Golub and Van Loan give a theorem for an algorithm that splits $$\mathbf A$$ into two parts. Suppose $$\mathbf A = \mathbf M - \mathbf N$$ is nonsingular. Let $$r = \rho (\mathbf M^ {-1} \mathbf N)$$ be the spectral radius of $$\mathbf M^{-1} \mathbf N$$. Then the iterates $$\mathbf x^{(k)}$$ defined by $$\mathbf M \mathbf x^{(k+1)} = \mathbf N \mathbf x^{(k)} + \mathbf b$$ converge to $$\mathbf x = \mathbf A^{-1} \mathbf b$$ for any starting vector $$\mathbf x^{(0)}$$ if $$\mathbf M$$ is nonsingular and $$r < 1$$.

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: A, b    output: φ Choose an initial guess φ to the solution repeat until convergence for i from 1 until n do σ ← 0 for j from 1 until n do if j ≠ i then σ ← σ + aijφj end if end (j-loop) φi ← (bi − σ) / aii end (i-loop) check if convergence is reached end (repeat)

An example for the matrix version
A linear system shown as $$\mathbf A \mathbf{x} = \mathbf{b}$$ is given by: $$\mathbf A=      \begin{bmatrix} 16 &   3 \\            7  & -11 \\           \end{bmatrix} \quad \text{and} \quad \mathbf b = \begin{bmatrix} 11 \\          13           \end{bmatrix}. $$

We want to use the equation $$ \mathbf{x}^{(k+1)} = \mathbf L^{-1} (\mathbf{b} - \mathbf U \mathbf{x}^{(k)}) $$ in the form $$ \mathbf{x}^{(k+1)} = \mathbf T \mathbf{x}^{(k)} + \mathbf c $$ where:
 * $$\mathbf T = - \mathbf L ^{-1} \mathbf U \quad \text{and} \quad \mathbf c = \mathbf L ^{-1} \mathbf{b}.$$

We must decompose $$\mathbf A$$ into the sum of a lower triangular component $$\mathbf L$$ and a strict upper triangular component $$U$$: $$ \mathbf L=     \begin{bmatrix} 16 &  0 \\           7  & -11 \\           \end{bmatrix} \quad \text{and} \quad \mathbf U = \begin{bmatrix} 0 & 3 \\          0 & 0        \end{bmatrix}.$$

The inverse of $$\mathbf L$$ is: $$ \mathbf L^{-1} = \begin{bmatrix} 16 &  0 \\           7  & -11           \end{bmatrix}^{-1} =     \begin{bmatrix} 0.0625 & 0.0000 \\           0.0398 & -0.0909 \\           \end{bmatrix}. $$

Now we can find: $$\begin{align} \mathbf T & = - \begin{bmatrix} 0.0625 & 0.0000 \\           0.0398 & -0.0909      \end{bmatrix} \begin{bmatrix} 0 & 3 \\          0 & 0      \end{bmatrix} =     \begin{bmatrix} 0.000 & -0.1875 \\          0.000 & -0.1194      \end{bmatrix}, \\[1ex] \mathbf c & = \begin{bmatrix} 0.0625 & 0.0000 \\           0.0398 & -0.0909      \end{bmatrix} \begin{bmatrix} 11 \\          13      \end{bmatrix} =     \begin{bmatrix} 0.6875 \\         -0.7439      \end{bmatrix}. \end{align}$$

Now we have $$\mathbf T $$ and $$\mathbf c $$ and we can use them to obtain the vectors $$\mathbf{x}$$ iteratively.

First of all, we have to choose $$\mathbf{x}^{(0)}$$: we can only guess. The better the guess, the quicker the algorithm will perform.

We choose a starting point: $$\mathbf x^{(0)} = \begin{bmatrix} 1.0 \\ 1.0 \end{bmatrix}.$$

We can then calculate: $$\begin{align} \mathbf x^{(1)} &= \begin{bmatrix} 0.000 & -0.1875 \\          0.000 & -0.1193      \end{bmatrix} \begin{bmatrix} 1.0 \\          1.0      \end{bmatrix} +     \begin{bmatrix} 0.6875 \\         -0.7443      \end{bmatrix} =     \begin{bmatrix} 0.5000 \\         -0.8636      \end{bmatrix}. \\[1ex] \mathbf x^{(2)} &= \begin{bmatrix} 0.000 & -0.1875 \\          0.000 & -0.1193      \end{bmatrix} \begin{bmatrix} 0.5000 \\         -0.8636      \end{bmatrix} +     \begin{bmatrix} 0.6875 \\         -0.7443      \end{bmatrix} =     \begin{bmatrix} 0.8494 \\         -0.6413      \end{bmatrix}. \\[1ex] \mathbf x^{(3)} &= \begin{bmatrix} 0.000 & -0.1875 \\          0.000 & -0.1193      \end{bmatrix} \begin{bmatrix} 0.8494 \\         -0.6413 \\      \end{bmatrix} +     \begin{bmatrix} 0.6875 \\         -0.7443      \end{bmatrix} =     \begin{bmatrix} 0.8077 \\         -0.6678      \end{bmatrix}. \\[1ex] \mathbf x^{(4)} &= \begin{bmatrix} 0.000 & -0.1875 \\          0.000 & -0.1193      \end{bmatrix} \begin{bmatrix} 0.8077 \\         -0.6678      \end{bmatrix} +     \begin{bmatrix} 0.6875 \\         -0.7443      \end{bmatrix} =     \begin{bmatrix} 0.8127 \\         -0.6646      \end{bmatrix}. \\[1ex] \mathbf x^{(5)} & = \begin{bmatrix} 0.000 & -0.1875 \\          0.000 & -0.1193      \end{bmatrix} \begin{bmatrix} 0.8127 \\         -0.6646      \end{bmatrix} +     \begin{bmatrix} 0.6875 \\         -0.7443      \end{bmatrix} =     \begin{bmatrix} 0.8121 \\         -0.6650      \end{bmatrix}. \\[1ex] \mathbf x^{(6)} & = \begin{bmatrix} 0.000 & -0.1875 \\          0.000 & -0.1193      \end{bmatrix} \begin{bmatrix} 0.8121 \\         -0.6650      \end{bmatrix} +     \begin{bmatrix} 0.6875 \\         -0.7443      \end{bmatrix} =     \begin{bmatrix} 0.8122 \\         -0.6650      \end{bmatrix}. \\[1ex] \mathbf x^{(7)} & = \begin{bmatrix} 0.000 & -0.1875 \\          0.000 & -0.1193      \end{bmatrix} \begin{bmatrix} 0.8122 \\         -0.6650      \end{bmatrix} +     \begin{bmatrix} 0.6875 \\         -0.7443      \end{bmatrix} =     \begin{bmatrix} 0.8122 \\         -0.6650      \end{bmatrix}. \end{align}$$

As expected, the algorithm converges to the solution: $$ \mathbf{x} = \mathbf A ^{-1} \mathbf{b} \approx \begin{bmatrix} 0.8122\\ -0.6650 \end{bmatrix}. $$

In fact, the matrix $A$ is strictly diagonally dominant (but not positive definite).

Another example for the matrix version
Another linear system shown as $$\mathbf A \mathbf{x} = \mathbf{b}$$ is given by:

$$ \mathbf A=     \begin{bmatrix} 2 & 3 \\          5 & 7 \\           \end{bmatrix} \quad \text{and} \quad \mathbf b = \begin{bmatrix} 11 \\          13 \\           \end{bmatrix}. $$

We want to use the equation $$ \mathbf{x}^{(k+1)} = \mathbf L^{-1} (\mathbf{b} - \mathbf U \mathbf{x}^{(k)}) $$ in the form $$ \mathbf{x}^{(k+1)} = \mathbf T \mathbf{x}^{(k)} + \mathbf c $$ where:
 * $$\mathbf T = - \mathbf L ^{-1} \mathbf U \quad \text{and} \quad \mathbf c = \mathbf L^{-1} \mathbf{b}.$$

We must decompose $$\mathbf A$$ into the sum of a lower triangular component $$\mathbf L$$ and a strict upper triangular component $$\mathbf U$$: $$ \mathbf L = \begin{bmatrix} 2 & 0 \\          5 & 7 \\           \end{bmatrix} \quad \text{and} \quad \mathbf U = \begin{bmatrix} 0 & 3 \\          0 & 0 \\        \end{bmatrix}.$$

The inverse of $$\mathbf L$$ is: $$ \mathbf L ^{-1} = \begin{bmatrix} 2 & 0 \\          5 & 7 \\           \end{bmatrix}^{-1} =     \begin{bmatrix} 0.500 & 0.000 \\         -0.357 & 0.143 \\           \end{bmatrix} .$$

Now we can find: $$\begin{align} \mathbf T & = - \begin{bmatrix} 0.500 & 0.000 \\         -0.357 & 0.143 \\      \end{bmatrix} \begin{bmatrix} 0 & 3 \\          0 & 0 \\      \end{bmatrix} =     \begin{bmatrix} 0.000 & -1.500 \\          0.000 &  1.071 \\      \end{bmatrix}, \\[1ex] \mathbf c & = \begin{bmatrix} 0.500 & 0.000 \\         -0.357 & 0.143 \\      \end{bmatrix} \begin{bmatrix} 11 \\          13 \\      \end{bmatrix} =     \begin{bmatrix} 5.500 \\         -2.071 \\      \end{bmatrix}. \end{align}$$

Now we have $$\mathbf T$$ and $$\mathbf c$$ and we can use them to obtain the vectors $$\mathbf{x}$$ iteratively.

First of all, we have to choose $$\mathbf{x}^{(0)}$$: we can only guess. The better the guess, the quicker will perform the algorithm.

We suppose: $$ \mathbf {x} ^{(0)} = \begin{bmatrix} 1.1 \\ 2.3 \end{bmatrix}.$$

We can then calculate: $$\begin{align} \mathbf {x}^{(1)} &= \begin{bmatrix} 0 & -1.500 \\          0 &  1.071 \\      \end{bmatrix} \begin{bmatrix} 1.1 \\          2.3 \\      \end{bmatrix} +     \begin{bmatrix} 5.500 \\         -2.071 \\      \end{bmatrix} =     \begin{bmatrix} 2.050 \\          0.393 \\      \end{bmatrix}. \\[1ex] \mathbf {x}^{(2)} &= \begin{bmatrix} 0 & -1.500 \\          0 &  1.071 \\      \end{bmatrix} \begin{bmatrix} 2.050 \\          0.393 \\      \end{bmatrix} +     \begin{bmatrix} 5.500 \\         -2.071 \\      \end{bmatrix} =     \begin{bmatrix} 4.911 \\         -1.651      \end{bmatrix}. \\[1ex] \mathbf {x}^{(3)} &= \cdots. \end{align}$$

If we test for convergence we'll find that the algorithm diverges. In fact, the matrix $$\mathbf A$$ is neither diagonally dominant nor positive definite. Then, convergence to the exact solution $$ \mathbf{x} = \mathbf A ^{-1} \mathbf{b} = \begin{bmatrix} -38\\ 29 \end{bmatrix} $$ is not guaranteed and, in this case, will not occur.

An example for the equation version
Suppose given $$n$$ equations and a starting point $$\mathbf {x} _0$$. At any step in a Gauss-Seidel iteration, solve the first equation for $$x_1$$ in terms of $$x_{2}, \dots, x_n$$; then solve the second equation for $$x_2$$ in terms of $$x_1$$ just found and the remaining $$x_{3}, \dots, x_n$$; and continue to $$x_n$$. Then, repeat iterations until (hopefully) converged.

To make it clear, consider an example: $$\begin{array}{rrrrl} 10x_1 &-  x_2 &+  2x_3 &       & = 6, \\ -x_1 &+ 11x_2 &-  x_3 &+ 3x_4 & =  25, \\ 2x_1 &-  x_2 &+ 10x_3 &-  x_4 & =  -11, \\ &  3x_2 &-   x_3 &+ 8x_4 & =  15. \end{array}$$

Solving for $$x_1, x_2, x_3$$ and $$x_4$$ gives: $$\begin{align} x_1 & = x_2/10 - x_3/5 + 3/5, \\ x_2 & = x_1/11 + x_3/11 - 3x_4/11 + 25/11, \\ x_3 & = -x_1/5 + x_2/10 + x_4/10  - 11/10, \\ x_4 & = -3x_2/8 + x_3/8 + 15/8. \end{align}$$

Suppose we choose $(0, 0, 0, 0)$ as the initial approximation, then the first approximate solution is given by $$\begin{align} x_1 & = 3/5 = 0.6, \\ x_2 & = (3/5)/11 + 25/11 = 3/55 + 25/11 = 2.3272, \\ x_3 & = -(3/5)/5 +(2.3272)/10-11/10 = -3/25 + 0.23272-1.1 = -0.9873,\\ x_4 & = -3(2.3272)/8 +(-0.9873)/8+15/8 = 0.8789. \end{align}$$

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.

The exact solution of the system is $(1, 2, −1, 1)$.

An example using Python and NumPy
The following numerical procedure simply iterates to produce the solution vector.

Produces the output:

Program to solve arbitrary no. of equations using Matlab
The following code uses the formula $$x^{(k+1)}_i = \frac{1}{a_{ii}} \left(b_i - \sum_{ji}a_{ij}x^{(k)}_j \right),\quad \begin{array}{l} i=1,2,\ldots,n \\ k=0,1,2,\ldots \end{array}$$