The direct methods (Gauss--Jordan) for solving linear systems, using elementary row operations, lead to exact solutions. However, given systems of equations are often subject to errors due to roundoff and other factors. The Gaussian elimination is sensitive to roundoff errors, and this sensitivity can lead to inaccurate or even wildly wrong answers. Also, even if Gaussian elimination does not go astray, we cannot improve on a solution once we have found it. For example, if we use Gaussian elimination to calculate a solution to four decimal places, there is no way to obtain the solution to eight decimal places except to start over again and work with increased accuracy (we do not discuss this topic and closely related rate of convergence at this moment, which is related to numerical methods). In contrast, we can achieve additional accuracy with iterative methods simply by doing more iterations.

The methods, starting from an initial guess x(0), iteratively apply some formulas to detect the solution of the system. For this reason, these methods are often indicated as iterative methods. In this section, we explore one-step iterative methods that may be beneficial. When applied, they proceed iteratively by successively generating sequences of vectors that approach a solution to a linear system. In many instances (such as when the coefficient matrix is sparse, that is, contains many zero entries), iterative methods can be faster and more accurate than direct methods. Furthermore, the amount of memory required for a sparse coefficient n×n matrix A is proportional to n. Also, iterative methods can be stopped whenever the approximate solution they generate is sufficiently accurate.

Iterative methods

To demonstrate applications of iterative methods in linear algebra, we consider the system of n linear equations with n unknowns x1, x2, … , xn (here n is a positive integer):
\begin{equation} \label{EqJacobi.1} \begin{split} a_{1,1} x_1 + a_{1,2} x_2 + \cdots + a_{1,n} x_n &= b_1 , \\ a_{2,1} x_1 + a_{2,2} x_2 + \cdots + a_{2,n} x_n &= b_2 , \\ \vdots \qquad \vdots \qquad \vdots & \vdots \\ a_{n,1} x_1 + a_{n,2} x_2 + \cdots + a_{n,n} x_n &= b_n , \end{split} \end{equation}
where coefficients 𝑎i,j of the system and entries bi are given (known) scalars, which we assume to be real numbers. It is convenient to rewrite the system (1) in compact (vector) form:
\begin{equation} \label{EqJacobi.2} {\bf A}\,{\bf x} = {\bf b} , \end{equation}
where
\[ {\bf A} = \begin{bmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n,1} & a_{n,2} & \cdots & a_{n,n} \end{bmatrix} \in \mathbb{R}^{n \times n}, \qquad {\bf b} = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{bmatrix} , \qquad {\bf x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} \in \mathbb{R}^{n \times 1} . \]
The idea of fixed point iteration methods is first to reformulate a given equation to an equivalent fixed point problem:
\begin{equation} \label{EqJacobi.3} {\bf x} = f({\bf x}) , \end{equation}
for some (vector) function f. Any solution of (3) is called a fixed-point for the iteration function f. Starting with initial guess x(0), an one-step iterative method produces an improved approximation x(k+1) from the previous approximation x(k), say
\[ {\bf x}^{(k+1)} = f \left( {\bf x}^{(k)} \right) , \qquad k=0,1,2,\ldots . \]
It is our hope that the iterative formula above provides a sequence of numbers { x(k) } that converges to the true solution x = c. However, we can stop iteration when we want to. There are known some sufficient conditions on function f that guarantee convergence of fixed point iteration procedure.

If we consider a system of linear equations A x = b, the starting solution x(0), and the approximated solution at the k-th step x(k), the solution x = c of the system A x = b, then an approximated method is said to converge to the solution of the system when \[ \lim_{k\to\infty} \left\| {\bf x}^{(k)} - {\bf c} \right\| = {\bf 0} . \] On the contrary, if the above limit does not exist, then the method is said to diverge.

When a sequence of errors ek = x(k)c convergences fast enough to zero, iterative methods are competitive with direct methods, which require much more memory and have a higher computational complexity. There are several references on iterative methods to solve a system of linear equations; for example, P.G. Ciarlet, J.J. Dongarra et al., C.T. Kelley, G. Meurant, and Y. Saad.

There are infinite many ways to introduce an equivalent fixed point problem for the given equation (1) or (2). In order to apply an iterative method for solving system of equations (1), or its equivalent compact form (2), we need to rearrange this equation to the fixed point form (3). This can be achieved by splitting the matrix A = S + M, where S is a nonsingular matrix. S is also called a preconditioner, and its choice is crucial in numerical analysis. Then the equation A x = b is the same as S x = bM x. Application of the inverse matrix S−1 yields the fixed point equation (3), which becomes

\begin{equation} \label{EqJacobi.4} \left( {\bf S} + {\bf M} \right) {\bf x} = {\bf b} \qquad \Longrightarrow \qquad {\bf x} = {\bf S}^{-1} {\bf b} - {\bf S}^{-1} {\bf M} \,{\bf x} . \end{equation}
Therefore, we can try one-step iteration from x(k) to x(k+1):
\begin{equation} \label{EqJacobi.5} {\bf x}^{(k+1)} = {\bf S}^{-1} {\bf b} - {\bf S}^{-1} {\bf M} \,{\bf x}^{(k)} , \qquad k=0,1,2,\ldots . \end{equation}
There is no guarantee that this method will work. A successful splitting A = S + M satisfies two different requirements:
  1. The new vector x(k+1) should be easy to compute. Hence, S should be a simple (and invertible!) matrix; it may be diagonal or triangular.
  2. The sequence { x(k) } should converge to the true solution x = c. If we subtract the iteration in equation (5) from the true equation x = S−1bS−1M x, the result is a formula involving only the errors ek = cx(k):
    \begin{equation} \label{EqJacobi.6} {\bf e}_{k+1} = - {\bf S}^{-1} {\bf M} \,{\bf e}_k = \left( - {\bf S}^{-1} {\bf M} \right)^{k+1} {\bf e}_0 , \qquad k=0,1,2,\ldots . \end{equation}
Equation (6) is a difference equation of first order. It starts with the initial error e₀ and after k steps it produces the new error ek. The question of convergence is exactly the same as the question of stability: x(k)c exactly when ek0.

If ∥ · ∥ denotes some vector norm (this topic is covered in Part 5 of this tutorial) on ℝn and the corresponding of (−S−1M) is less than 1, we claim that x(k)c → 0 as k → ∞. Indeed,

and so on. In general,
\[ {\bf x}^{(k)} - {\bf c} = \left( -{\bf S}^{-1} {\bf M} \right)^k \left( {\bf x}^{(0)} - {\bf c} \right) , \qquad k=0,1,2,\ldots , \]
and hence
\begin{align*} \left\| {\bf x}^{(k)} - {\bf c} \right\| &= \left\|\left( -{\bf S}^{-1} {\bf M} \right)^k \left( {\bf x}^{(0)} - {\bf c} \right) \right\| \\ &\le \left\| \left( -{\bf S}^{-1} {\bf M} \right)^k \right\| \left\| {\bf x}^{(0)} - {\bf c} \right\| \\ & \le \left\| \left( -{\bf S}^{-1} {\bf M} \right) \right\|^k \left\| {\bf x}^{(0)} - {\bf c} \right\| . \end{align*}
Thus, if ∥ (−S−1M) ∥ = ∥ S−1M ∥ < 1, then ∥ x(k)c ∥ → 0 as k → ∞.

The foregoing result holds for any standard norm on ℝn because they all equivalent.

Theorem 1: Let x(0) be an arbitrary vector in ℝn and define x(k+1) = S−1bS−1Mx(k) for k = 0, 1, 2,, …. If x = c is the solution to Eq.(4), then a necessary and sufficient condition for x(k)c is that all eigenvalues of matrix S−1M be less than 1, i.e., | λ | < 1.

In other words, the iterative method in equation (5) is convergent if and only if every eigenvalue of S−1M satisfies | λ | < 1. Its rate of convergence depends on the maximum size of eigenvalues of matrix S−1M. So the largest eigenvalue will eventually be dominant.

.

We will prove the theorem only in the case where B = −S−1M has n linearly independent eigenvectors. The case where B is not diagonalizable is later. If u1, u2, … , un are n linearly independent eigenvectors of B, we can write \[ {\bf x}^{(k)} - {\bf c} = c_1 {\bf u}_1 + c_2 {\bf u}_2 + \cdots + c_n {\bf u}_n \] and it follows from x(k)c = Bk(x(0)c) that \begin{align*} {\bf x}^{(k)} - {\bf c} &= {\bf B}^k \left( c_1 {\bf u}_1 + c_2 {\bf u}_2 + \cdots + c_n {\bf u}_n \right) \\ &= c_1 \lambda_1^k {\bf u}_1 + c_2 \lambda_2^k {\bf u}_2 + \cdots + c_n \lambda_n^k {\bf u}_n . \end{align*} Hence, x(k)c0 if and only if | λi | < 1 for i = 1, 2, … , n.
In this section, we discuss three versions of stationary iterative method: Jacobi's scheme, the Gauss--Seidel method, and the SOR method. Since they converge slowly, stationary iterative methods are no longer used alone, but rather used within other algorithms.

The Jacobi method

Carl Jacobi
The simplest iterative method that was first applied for solving systems of linear equations is known as the Jacobi’s method,
Carl Gustav Jacob Jacobi (1804--1851) was a German mathematician who made fundamental contributions to elliptic functions, dynamics, differential equations, determinants, and number theory. Although much of his work was in applied mathematics, Jacobi believed in the importance of doing mathematics for its own sake. A fine teacher, he held positions at the Universities of Berlin and Königsberg (now it is city of Kaliningrad) and was one of the most famous mathematicians in Europe.

In numerical linear algebra, the Jacobi method (a.k.a. the Jacobi iteration method) is an iterative algorithm for determining the solutions of a strictly diagonally dominant system of linear equations. Each diagonal element is solved for, and an approximate value is plugged in. The method was discovered by Carl Gustav Jacob Jacobi in 1845.

The idea of this iteration method is to extract the main diagonal of the matrix of the given vector equation

\[ {\bf A}\,{\bf x} = {\bf b} , \]
and consider the rest of matrix A as perturbation:
\[ {\bf A} = \Lambda + {\bf M} . \]
Here A is the given square n × n matrix, Λ is the diagonal matrix built from A (it is assumed that the diagonal entries of A do not vanish), and M is the matrix obtained from A by annihilating its diagonal entries. So
\[ \Lambda = \begin{bmatrix} a_{1,1} & 0 & 0 & \cdots & 0 \\ 0 & a_{2,2} & 0 & \cdots & 0 \\ 0 & 0 & a_{3,3} & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \cdots & a_{n,n} \end{bmatrix} , \qquad {\bf M} = \begin{bmatrix} 0 & a_{1,2} & a_{1,3} & \cdots & a_{1,n} \\ a_{2,1} & 0 & a_{2,3} & \cdots & a_{2,n} \\ a_{3,1} & a_{3,2} & 0 & \cdots & a_{3,n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ a_{n,1} & a_{n,2} & a_{n,3} & \cdots & 0 \end{bmatrix} . \]
Then applying the inverse of the diagonal matrix, we get
\[ {\bf x} = \Lambda^{-1} {\bf b} - \Lambda^{-1} {\bf M}\,{\bf x} = {\bf d} + {\bf B}\,{\bf x} , . \]
where
\[ {\bf d} = \Lambda^{-1} {\bf b} = \begin{bmatrix} \frac{b_1}{a_{1,1}} \\ \frac{b_2}{a_{2,2}} \\ \vdots \\ \frac{b_n}{a_{n,n}} \end{bmatrix} , \qquad {\bf B} = - \Lambda^{-1} {\bf M} = - \begin{bmatrix} 0 & \frac{a_{1,2}}{a_{1,1}} & \frac{a_{1,3}}{a_{1,1}} & \cdots & \frac{a_{1,n}}{a_{1,1}} \\ \frac{a_{2,1}}{a_{2,2}} & 0 & \frac{a_{2,3}}{a_{2,2}} & \cdots & \frac{a_{2,n}}{a_{2,2}} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ \frac{a_{n,1}}{a_{n,n}} & \frac{a_{n,2}}{a_{n,n}} & \frac{a_{n,3}}{a_{n,n}} & \cdots & 0 \end{bmatrix} . \]
Since the new equation x = d + B x is of the form x = f(x), it is suitable for iteration procedure: x(k+1) = d + B x(k). Jacobi’s method uses diagonal part of matrix A for converting the given system of equation into fixed point form.

Here is the algorithm for Jacobi iteration method:


Input: initial guess x(0) to the solution, (diagonal dominant) matrix A, right-hand side 
       vector b, convergence criterion
Output: solution when convergence is reached
Comments: pseudocode based on the element-based formula above

k = 0
while convergence not reached do
    for i := 1 step until n do
        σ = 0
        for j := 1 step until n do
            if j ≠ i then
                σ = σ + aij xj(k)
            end
        end
        xi(k+1) = (bi − σ) / aii
    end
    increment k
end
Example 1: Let us consider the system
\[ \begin{split} 6 x_1 - 5 x_2 &= 7 \\ 4 x_1 - 7 x_2 &= 1 . \end{split} \]
We rewrite the given system of equations in vector form A v = b, where \[ {\bf A} = \begin{bmatrix} 6 & -5 \\ 4 & -7 \end{bmatrix} , \qquad {\bf b} = \begin{bmatrix} 7 \\ 1 \end{bmatrix} . \] Then we extract the diagonal matrix from A: \[ {\bf A} = \Lambda + {\bf M}, \qquad \Lambda = \begin{bmatrix} 6 & \phantom{-}0 \\ 0 & -7 \end{bmatrix}, \quad {\bf M} = \begin{bmatrix} 0 & -5 \\ 4 & \phantom{-}0 \end{bmatrix} \]

Jacobi's method begins with solving the first equation for x₁ and the second equation for x₂, to obtain

\[ \begin{split} x_1 &= \frac{5 x_2 + 7}{6} , \\ x_2 &= \frac{4 x_1 -1}{7} . \end{split} \tag{1.1} \]
We rewrite system (1.1) in vector form:
\[ {\bf x} = \Lambda^{-1} {\bf b} - \Lambda^{-1}{\bf M}\,{\bf x} , \qquad \Lambda^{-1} = \begin{bmatrix} \frac{1}{6} &0 \\ 0 & -\frac{1}{7} \end{bmatrix} , \quad \Lambda^{-1}{\bf M} = \begin{bmatrix} 0 & -\frac{5}{6} \\ -\frac{4}{7} & 0 \end{bmatrix} . \]
We now need an initial approximation to the solution. It turns out that it does not matter what this initial approximation is, so we might as well take x₁ = 0, x₂ = 0. We use these values in Equations (1.1) to get new values of x₁ and x₂:
\[ \begin{split} x_1 &= \frac{5\cdot 0 + 7}{6} = \frac{7}{6} \approx 1.16667, \\ x_2 &= \frac{4 \cdot 0 -1}{7} = -\frac{1}{7} \approx -0.142857. \end{split} \]
We repeat this process (using the old values of x₁ and x₂ to get the new values of x₁ and x₂), producing the sequence of approximations given in the following Table

Table of Jacobi iterates
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{7}{6} \) \( \displaystyle \frac{22}{21} \approx 1.04762 \) \( \displaystyle \frac{101}{63} \approx 1.60317 \) \( \displaystyle \frac{682}{441} \approx 1.54649 \) \( \displaystyle \frac{2396}{1323} \approx 1.81104 \)
x 0 \( \displaystyle -\frac{1}{7} \) \( \displaystyle \frac{11}{21} \approx 0.52381 \) \( \displaystyle \frac{67}{147} \approx 0.455782 \) \( \displaystyle \frac{341}{441} \approx 0.773243 \) \( \displaystyle \frac{2287}{3087} \approx 0.740849 \)

The successive vectors \( \displaystyle \quad \begin{bmatrix} x_1^{(k)} \\ x_2^{(k)} \end{bmatrix} \quad \) are called iterates. For instance, when k = 5, the fifth iterate is \( \displaystyle \quad \begin{bmatrix} 1.8110 \\ 0.740849 \end{bmatrix} \quad \)

Here is Python code to produce the result you see above, first using a module, which is used in other examples also.

the 46th iteration: [1.99999996 1. ]
the 47th iteration: [2. 0.99999998]
the 48th iteration: [1.99999998 1. ]
the 49th iteration: [2. 0.99999999]
the 50th iteration: [1.99999999 1. ]

In code above, parameter n_ is for the number of iterations you want - the display is always the last 5 iterations; fp_ is for floating point, the default being "True" - if you input "False" as the last argument, you will get explicit solution, which produces fractions when input coefficients are from ℚ.

Clear[A, M, B, b, diag, d, \[CapitalLambda], x, l, u, x1, x2, x3];
jacMeth[mat_List, b_List, n_ : 5, fp_ : True] := Module[{dia, low, up, len, its}, If[n >= 5, dia = DiagonalMatrix[Diagonal[mat]]; low = LowerTriangularize[mat, -1]; up = UpperTriangularize[mat, 1]; len = ConstantArray[0, Length[Diagonal[mat]]]; its = If[fp == True, Prepend[Take[ FoldList[N[LinearSolve[dia, -(low + up) . # + b]][[All, 1]] &, len, Range[n]], -6], ""], Prepend[Take[ FoldList[LinearSolve[dia, -(low + up) . # + b][[All, 1]] &, len, Range[n]], -6], ""]]; Grid[{{"Matrix", "A", "d", "l", "u", "b", "x0"}, Take[MatrixForm[#] & /@ {"", mat, dia, low, up, b, len}, -7], Join[{"Iterations"}, {"0"}, Take[Range[0, n], -5]], its}, Frame -> All], "Must have n\[GreaterEqual]5"] ];
Now we apply this subroutine to our problem.
jacMeth[({ {6, -5}, {4, -7} }), ({ {7}, {1} }), 5 , False]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 6 & -5 \\ 4 & -7 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 6 & 0 \\ 0 & -7 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0 \\ 4&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-5 \\ 0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 7 \\ 1 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \end{pmatrix} \)
Iterations 0 1 2 3 4 5
\( \displaystyle \begin{pmatrix} 0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{7}{6} & -\frac{1}{7} \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{22}{21} & \frac{11}{21} \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{101}{63} & \frac{67}{147} \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{682}{441} & \frac{341}{441} \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{2396}{1323} & \frac{2287}{3087} \end{pmatrix} \)
Although exact calculations involve rational numbers, their usage in actual computations is entirely impractical because the number of bits required to maintain exactness increases exponentially with the number of iterations. Therefore, next we demonstrate Jacobi's iterative method with floating point arithmetic.

Now we repeat calculations by making 50 iterative steps.

jacMeth[({ {6, -5}, {4, -7} }), ({ {7}, {1} }), 50, False] // Timing
It took less than a second for Mathematica to execute the code with 50 steps. We do not present all output because it is expressed with huge numbers not suitable for human's eyes. For instance,
\[ x_1^{(50)} = \frac{2272544311845448533481444917041002}{1136272165922724266740722458520501} . \]
Last[Prepend[ FoldList[LinearSolve[d, -(l + u) . # + b] &, {0, 0}, Range[50]], {"x1", "x2"}]] // Timing
N[%]

Expressing the rational numbers above as floating point reals, we get the same result, which is more suitable for human eyes. So, we repeat calculations using floating point representation.

jacMeth[({ {6, -5}, {4, -7} }), ({ {7}, {1} }), 50, True] // Timing

Finally, we can obtain the same result as floating point, also in less than a second.

Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 6 & -5 \\ 4 & -7 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 6 & 0 \\ 0 & -7 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0 \\ 4&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-5 \\ 0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 7 \\ 1 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \end{pmatrix} \)
Iterations 0 46 47 48 49 50
(2., 1.) (2., 1.) (2., 1.) (2., 1.) (2., 1.) (2., 1.)

Here are the component parts of the code above, producing the same output in steps.

A = ({ {6, -5}, {4, -7} });
d = DiagonalMatrix[Diagonal[A]];
l = LowerTriangularize[A, -1];
u = UpperTriangularize[A, 1];
b = ({ {7}, {1} });
x[1] = ({ {0}, {0}Sdemo3 });
Grid[{{"A", "d", "l", "u", "b", "x0"}, MatrixForm[#] & /@ {A, d, l, u, b, x[1]}}, Frame -> All]

Starting point does not matter for final destination of Jacobi's ietrating scheme. Let's test that with calculations using different starting points, random floating point numbers and integers. Below, a function is provided that works with a list of starting points.

Last[Prepend[ FoldList[LinearSolve[d, -(l + u) . # + b] &, {#1, #2}, Range[50]], {"x1", "x2"}]] &(*starting point->*)[0., 0.][[All, 1]]
{2., 1.}
stPts = {{0., .5, .25, RandomReal[], RandomInteger[{2, 29}]}, {0., .5, .25, N[RandomReal[]], N[RandomInteger[{31, 49}]]}};
varSt = MapThread[ Last[Prepend[ FoldList[LinearSolve[d, -(l + u) . # + b] &, {#1, #2}, Range[50]], {"x1", "x2"}]] &,(*starting point->*)stPts][[All, All, 1]];
TableForm[ Transpose[{stPts[[1]], varSt[[All, 1]], stPts[[2]], varSt[[All, 2]]}], TableHeadings -> {None, {"Starting\npoint", "\n\!\(\*SubscriptBox[\(x\), \(1\)]\)", "Starting\npoint", "\n\!\(\*SubscriptBox[\(x\), \(2\)]\)"}}]

Starting Starting
Point x Point x
0 2. 0 1.
0.5 2. 0.5 1.
0.25 2. 0.25 1.
0.0013514 2. 0.742626 1.
10 2. 16 1.
End of Example 1
Example 2: Let us consider the system
\[ \begin{split} 5 x_1 - 6 x_2 &= 4 \\ 7 x_1 - 4 x_2 &= 10 . \end{split} \]
We rewrite the given system of equations in vector form A v = b, where \[ {\bf A} = \begin{bmatrix} 5 & -6 \\ 7 & -4 \end{bmatrix} , \qquad {\bf b} = \begin{bmatrix} 4 \\ 10 \end{bmatrix} . \] Then we extract the diagonal matrix from A: \[ {\bf A} = \Lambda + {\bf M}, \qquad \Lambda = \begin{bmatrix} 5 & \phantom{-}0 \\ 0 & -4 \end{bmatrix}, \quad {\bf M} = \begin{bmatrix} 0 & -6 \\ 7 & \phantom{-}0 \end{bmatrix} \] Applying the inverse of the diagonal matrix, we obtain
\[ {\bf x} = \Lambda^{-1} {\bf b} - \Lambda^{-1} {\bf M}\, {\bf x} , \qquad \Lambda^{-1} = \begin{bmatrix} \frac{1}{5} & 0 \\ 0 & -\frac{1}{4} \end{bmatrix} , \quad \Lambda^{-1} {\bf M} = \begin{bmatrix} 0 & - \frac{6}{5} \\ - \frac{7}{4} & 0 \end{bmatrix} . \]
The eigenvalues of matrix −Λ−1M are λ₁ ≅ 1.44914 and λ₂ ≅ −1.44914. Since the magnitudes of these eigenvalues exceed 1, we conclude that the Jacobi's iteration scheme diveges for this matrix.
A = {{0, 6/5}, {7/4, 0}};
N[Eigenvalues[A]]
{-1.44914, 1.44914}
Solving for x₁ and x₂, we get
\[ \begin{split} x_1 &= \frac{6 x_2 + 4}{5} , \\ x_2 &= \frac{7 x_1 -10}{4} . \end{split} \tag{2.1} \]
Initial approximation can be chosen x₁ = 0, x₂ = 0. We use these values in Equations (2.1) to get new values of x₁ and x₂:
\[ \begin{split} x_1 &= \frac{6\cdot 0 + 4}{5} = \frac{4}{5} = 0.8, \\ x_2 &= \frac{7 \cdot 0 -10}{4} = -\frac{10}{4} = 2.5. \end{split} \]
We repeat this process (using the old values of x₁ and x₂ to get the new values of x₁ and x₂), producing the sequence of approximations given in Table

Table of first five Jacobi iterates
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{4}{5} \) \( \displaystyle \frac{11}{5} = 2.2 \) \( \displaystyle \frac{13}{25} = 0.52 \) \( \displaystyle -\frac{341}{50} = -6.82 \) \( \displaystyle -\frac{823}{250} = -3.292 \)
x 0 \( \displaystyle -\frac{5}{2} \) \( \displaystyle \frac{11}{10} =1.1 \) \( \displaystyle -\frac{127}{20} = -6.35 \) \( \displaystyle -\frac{341}{100} = -3.41 \) \( \displaystyle \frac{2887}{200} = -14.435 \)

It is hard to believe that this iteration scheme provides the correct answer: x₁ = 2 and x₂ = 1.

the 48th iteration: [-1.08216395e+08 -5.41081974e+07]
the 49th iteration: [-6.49298361e+07 -1.89378693e+08]
the 50th iteration: [-2.27254431e+08 -1.13627216e+08]
the 51th iteration: [-1.36352658e+08 -3.97695257e+08]
the 52th iteration: [-4.77234308e+08 -2.38617154e+08]
the 53th iteration: [-2.86340584e+08 -8.35160041e+08]
A = {{5, -6}, {7, -4}};
b = {4, 10};
LinearSolve[A, b]
{2, 1}

Here are the last five runs:

Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 5 & -6 \\ 7 & -4 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 & 0 \\ 0 & -4 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0 \\ 7&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-6 \\ 0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 4 \\ 10 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \end{pmatrix} \)
Iterations 48 49 50 51 52 53
\( \displaystyle \left\{ \begin{array}{c} -1.08216 \times 10^8 , \\ -5.41082 \times 10^7 \end{array} \right\} \) \( \displaystyle \{ \begin{array}{c} -6.49298 \times 10^7 , \\ -1.89379 \times 10^8 \end{array} \} \) \( \displaystyle \left\{ \begin{array}{c} -2.27254 \times 10^8 , \\ -3.97695 \times 10^8 \end{array} \right\} \) \( \displaystyle \{ \begin{array}{c}-4.77234 \times 10^8 , \\ -2.38617 \times 10^8 \end{array} \} \) \( \displaystyle \left\{ \begin{array}{c} -2.86341 \times 10^8 , \\ -8.3516 \times 10^8 \end{array} \right\} \) \( \displaystyle \left\{ \begin{array}{c} -2.86341 \times 10^8 , \\ -8.3516 \times 10^8 \end{array} \right\} \)
End of Example 2
Unlike direct methods that converge to the theoretical solution in a finite time, iterative methods provide approximations since they converge, under some conditions, to the exact solution of the system of linear equations in infinite steps.

The Jacobi method to be applied, matrix A should not display zeros on its main diagonal. It is also convenient to organize the unknown values x1, x2, … , xn into a column vector, denoted by x.

To start an iteration method, you need an initial guess vector

\[ {\bf x}^{(0)} = \begin{pmatrix} x_1^{(0)} \\ x_2^{(0)} \\ \vdots \\ x_n^{(0)} \end{pmatrix} . \]
Since convergence of Jacobi's scheme does not depend on your initial choice x(0), it could be any vector, for instance, zero. At the (k + 1)st iteration x(k+1) = d + Bx(k), the vector x(k+1) is calculated by
\begin{equation} \label{EqJacobi.7} {\bf x}^{(k+1)}_j = \frac{1}{a_{j,j}} \left( b_j - \sum_{i\ne j} a_{j,i} x_i^{(k)} \right) , \qquad j=1,2,\ldots , n. \end{equation}
The vector x(k) is used in calculating x(k+1). Consequently, these two vectors must be stored separately.

If the diagonal elements of matrix A are much larger than the off-diagonal elements, the entries of B should all be small and the Jacobi iteration should converge. We introduce a very important definition that reflects diagonally dominant property of a matrix.

A square matrix is said to be diagonally dominant if, for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the n×n matrix A = [𝑎ij] is diagonally dominant if \[ \left\vert a_{ii} \right\vert \ge \sum_{j\ne i} \left\vert a_{ij} \right\vert \qquad \forall i. \]
If A is diagonally dominant, the matrix B = −Λ−1M of the Jacobi iteration will have the property
\[ \sum_{j=1}^n \left\vert b_{i,j} \right\vert = \sum_{j\ne i} \frac{|a_{i,j}|}{|a_{i.i}|} < 1 \qquad \mbox{for} \quad i=1,2,\ldots ,n. \]
Thus,
\[ \| {\bf B} \|_{\infty} = \| \Lambda^{-1} {\bf M} \|_{\infty} = \max_{1 \le i \le n} \left( \sum_{j=1}^n \left\vert b_{i,j} \right\vert \right) = \max_{1 \le i \le n} \left( \sum_{j=1\ne i}^n \left\vert \frac{a_{i,j}}{a_{i,i}} \right\vert \right) < 1 . \]
It follows, then, that if A is diagonally dominant, the Jacobi iteration will converge to the solution of A x = b.

Theorem 2: If a system of n linear equations in n variables has a strictly diagonally dominant coefficient matrix, then it has a unique solution and both the Jacobi and the Gauss--Seidel method converge to it.

Although we don't need to prove this theorem because it is a special (finite-dimensional) case of more general fixed point theorem, we present its proof..

For j = 1, 2, … , n, let \[ \alpha_j = \sum_{i=1}^{j-1} \left\vert a_{j,i} \right\vert , \qquad \beta_j = \sum_{i=j+1}^{n} \left\vert a_{j,i} \right\vert \qquad\mbox{and} \qquad M_j = \frac{\beta_j}{| a_{j,i} | - \alpha_j} . \] Since A is diagonally dominant, it follows that \[ \left\vert a_{j,j} \right\vert > \alpha_j + \beta_j , \qquad j=1,2,\ldots , n. \] Consequently, Mj < 1 for j = 1, 2, … , n. Thus, \[ M = \max_{1 \le j \le n} M_j < 1. \] We will show that \[ \| {\bf B} \|_{\infty} = \max_{{\bf x} \ne 0} \frac{\| {\bf B}\,{\bf x} \|_{\infty}}{\| {\bf x} \|_{\infty}} \le M < 1 . \] Let x be a nonzero vector in ℝn and let y = B x. Choose k so that \[ \| {\bf y} \|_{\infty} = \max_{1 \le i \le n} \, | y_i | = | y_k | . \] It follows from the definition of B that \[ {\bf y} = {\bf B} \,{\bf x} = {\bf S}^{-1} {\bf U}\, {\bf x} , \] and hence \[ {\bf y} = {\bf B}\,{\bf x} = \Lambda^{-1} \left( {\bf L}\,{\bf y} + {\bf U}\, {\bf x} \right) , \] where Λ is the diagonal matrix extracted from A and L = SΛ is strictly lower triangular matrix. Comparing the kth coordinates of each side, we see that \[ y_k = -\frac{1}{a_{k,k}} \left( \sum_{i=1}^{k-1} a_{k,i} y_i + \sum_{i=k+1}^n a_{k,i} x_i \right) , \] and hence \[ \| {\bf y} \|_{\infty} = \left\vert y_k \right\vert \le \frac{1}{| a_{k,k} |} \left( \alpha_k \| {\bf y} \|_{\infty} + \beta_k \| {\bf x} \|_{\infty} \right) . \] From this inequality, it follows \[ \frac{\| {\bf B}\,{\bf x} \|_{\infty}}{\| {\bf x} \|_{\infty}} = \frac{\| {\bf y} \|_{\infty}}{\| {\bf x} \|_{\infty}} \le M_k \le M . \] So \[ \| {\bf B} \|_{\infty} = \max_{{\bf x} \ne 0} \frac{\| {\bf B}\,{\bf x} \|_{\infty}}{\| {\bf x} \|_{\infty}} \le M < 1 , \] and hence the iteration will converge to the solution of A x = b.
Example 3: We consider system of linear equations \[ \begin{split} 5\,x_1 - 4\, x_2 + x_3 &= 5, \\ 5\,x_1 + 7\, x_2 -4\, x_3 &= 21, \\ 3\, x_1 - 7\, x_2 + 8\, x_3 &= -9 , \end{split} \tag{3.1} \] with matrix not being diagonally dominant: \[ {\bf A} = \begin{bmatrix} 5 & -4 & \phantom{-}1 \\ 5 & \phantom{-}7 & -4 \\ 3 & -7 & \phantom{-}8 \end{bmatrix} . \] Indeed, \[ 5 = |-4| +1, \qquad 7 < 5 + |-4| = 9, \qquad 8 < 3 + |-7| = 10 . \] We can check that the given system has the unique solution x₁ = 2, x₂ = 1, x₃ = −1.

A = {{5, -4, 1}, {5, 7, -4}, {3, -7, 8}};
b = {5, 21, -9};
LinearSolve[A, b]
{2, 1, -1}

However, the code above will not work if you define vector b as column vector. To overcome this, use the following modification.

b = ({ {5}, {21}, {-9} });
LinearSolve[A, b][[All,1]]

Upon extracting the diagonal matrix from A, we split the given system (3.1) into the following form: \[ \left( \Lambda + {\bf M} \right) {\bf x} = {\bf b} \qquad \iff \qquad {\bf x} = \Lambda^{-1} {\bf b} - \Lambda^{-1} {\bf M} \,{\bf x} , \] where \[ {\bf d} = \Lambda^{-1} {\bf b} = \begin{bmatrix} \frac{1}{5} & 0 & 0 \\ 0 & \frac{1}{7} & 0 \\ 0 & 0 & \frac{1}{8} \end{bmatrix} \begin{pmatrix} \phantom{-}5 \\ 21 \\ -9 \end{pmatrix} = \begin{bmatrix} 1 \\ 3 \\ - \frac{9}{8} \end{bmatrix} , \qquad {\bf B} = - \Lambda^{-1} {\bf M} = \begin{bmatrix} 0 & \frac{4}{5} & - \frac{1}{5} \\ - \frac{5}{7} & 0 & \frac{4}{7} \\ - \frac{3}{8} & \frac{7}{8} & 0 \end{bmatrix} . \] Matrix B has one real eigenvalue λ₁ ≈ -0.362726, and two complex conjugate eigenvalues λ₂ ≈ 0.181363 + 0.308393j, λ₃ ≈ 0.181363 - 0.308393j, where j is the imaginary unit of the complex plane ℂ, so j² = −1.

B= {{0, 4/5, -1/5},{-5/7,0,4/7},{-3/8,7/8,0}};
Eigenvalues[B]
{Root[13 - # + 280 #^3& , 1, 0], Root[13 - # + 280 #^3& , 3, 0], Root[ 13 - # + 280 #^3& , 2, 0]}
Since all eigenvalues have absolute value less than 1, we expect Jacobi's method to be convergent,
Sqrt[0.181363^2 + 0.308393^2]
0.357769
The code above shows that the absolute value of complex number λ = x + jy, with x = 0.181363 and y = 0.308393, is \( \displaystyle |\lambda | = | x + {\bf j}\,y | = \sqrt{x^2 + y^2} \approx 0.357769 < 1 . \)

Although we know exact solution, x₁ = 2, x₂ = 1, x₃ = −1, of the given system of linear equations (3.1), we know that convergence of Jacobi's iteration scheme does not depend on the initial guess. So we start with zero: \[ {\bf x}^{(0)} = \left[ 0, 0, 0 \right] . \]

\[CapitalLambda] = DiagonalMatrix[Diagonal[A]];
MatrixForm[%]/
\( \displaystyle \begin{pmatrix} 5 &0&0 \\ 0&7&0 \\ 0&0&8 \end{pmatrix} \)
M = A - \[CapitalLambda];
MatrixForm[%]
\( \displaystyle \begin{pmatrix} 0 &-4&1 \\ 5&0&-4 \\ 3&-7&0 \end{pmatrix} \)
Inverse[\[CapitalLambda]] . b[[All, 1]]
{1, 3, -(9/8)}
B = -Inverse[\[CapitalLambda]] . M
Using general formula \[ {\bf x}^{(k+1)} = d + {\bf B}\,{\bf x}^{(k)} , \qquad k=0,1,2,\ldots , \qquad {\bf d} = \begin{bmatrix} 1 \\ 3 \\ - \frac{9}{8} \end{bmatrix} , \qquad {\bf B} = \begin{bmatrix} 0 & \frac{4}{5} & - \frac{1}{5} \\ - \frac{5}{7} & 0 & \frac{4}{7} \\ - \frac{3}{8} & \frac{7}{8} & 0 \end{bmatrix} , \tag{3.2} \] we obtain the first approximation: \[ {\bf x}^{(1)} = {\bf d} + {\bf B}\,{\bf x}^{(0)} = \begin{bmatrix} 1 \\ 3 \\ - \frac{9}{8} \end{bmatrix} = \begin{bmatrix} 1 \\ 3 \\ - 1.125 \end{bmatrix} . \] To find the second approximand, we ask Mathematica for help
d = {1,3,-9/8};
B= {{0, 4/5, -1/5},{-5/7,0,4/7},{-3/8,7/8,0}};
x2 = d + B.d
{29/8, 23/14, 9/8}
or
N[%]
{3.625, 1.64286, 1.125}
\[ {\bf x}^{(2)} = {\bf d} + {\bf B}\, {\bf d} = \begin{bmatrix} \frac{29}{8} \\ \frac{23}{14} \\ \frac{9}{8} \end{bmatrix} = \begin{bmatrix} 3.625 \\ 1.64286 \\ 1.125 \end{bmatrix} . \] The next approximand is
d = {1,3,-9/8};
B= {{0, 4/5, -1/5},{-5/7,0,4/7},{-3/8,7/8,0}};
x3 = d + B.x2
{117/56, 59/56, -(67/64)}
N[%]
{2.08929, 1.05357, -1.04688}
\[ {\bf x}^{(3)} = {\bf d} + {\bf B}\, {\bf x}^{(2)} = \begin{bmatrix} \frac{117}{56} \\ \frac{59}{56} \\ -\frac{67}{64} \end{bmatrix} = \begin{bmatrix} 2.08929 \\ 1.05357 \\ -1.04688 \end{bmatrix} . \] Now it is right time to organize our calculations in a loop.

jacMeth[({ {5, -4, 1}, {5, 7, -4}, {3, -7, 8} }), ({ {5}, {21}, {-9} })]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 5 & -4 & 1 \\ 5 & 7 & -4 \\ 3 & -7 & 8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 & 0& 0 \\ 0 & 7&0 \\ 0&0& 8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ 5&0&0 \\ 3& -7 & 0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-4 & 1 \\ 0&0& -4 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 \\ 21 \\ -9 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 0 1 2 3 4 5
\( \displaystyle \begin{pmatrix} 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 1 & 3 & - \frac{9}{8} \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{29}{8} & \frac{23}{14} & \frac{9}{8} \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{117}{56} & \frac{59}{56} & -\frac{67}{64} \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{4597}{2240} & \frac{713}{784} & -\frac{221}{224} \end{pmatrix} \) \( \displaystyle \begin{pmatrix} \frac{15091}{7840} & \frac{3043}{3136} & -\frac{2813}{2560} \end{pmatrix} \)

We repeat calculations with floating point output.

jacMeth[({ {5, -4, 1}, {5, 7, -4}, {3, -7, 8} }), ({ {5}, {21}, {-9} }), 5]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 5 & -4 & 1 \\ 5 & 7 & -4 \\ 3 & -7 & 8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 & 0& 0 \\ 0 & 7&0 \\ 0&0& 8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ 5&0&0 \\ 3& -7 & 0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-4 & 1 \\ 0&0& -4 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 \\ 21 \\ -9 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 0 1 2 3 4 5
\( \displaystyle \begin{pmatrix} 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{array}{c} 1. \\ 3. \\ - 1.125 \end{array} \) \( \displaystyle \begin{array}{c} 3.625, \\ 1.64286, \\ 1.125 \end{array} \) \( \displaystyle \begin{array}{c} 2.08929, \\ 1.05357, \\ -1.04688 \end{array} \) \( \displaystyle \begin{array}{c} 2.05223, \\ 0.909439, \\ -0.986607 \end{array} \) \( \displaystyle \begin{array}{c} 1.92487, \\ 0.970344, \\ -1.09883 \end{array} \)

We repeat calculations with 20 runs, displaying only the initial and last five:

jacMeth[({ {5, -4, 1}, {5, 7, -4}, {3, -7, 8} }), ({ {5}, {21}, {-9} }), 20]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 5 & -4 & 1 \\ 5 & 7 & -4 \\ 3 & -7 & 8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 & 0& 0 \\ 0 & 7&0 \\ 0&0& 8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ 5&0&0 \\ 3& -7 & 0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-4 & 1 \\ 0&0& -4 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 \\ 21 \\ -9 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 0 16 17 18 19 20
\( \displaystyle \begin{pmatrix} 0&0&0 \end{pmatrix} \) (2., 1., −1.) (2., 1., −1.) (2., 1., −1.) (2., 1., −1.) (2., 1., −1.)
End of Example 3

Theorem 3: If the Jacobi or the Gauss-Seidel method converges for a system of n linear equations in n unknowns, then it must converge to the solution of the system.

The idea of the proof is quite simple: to apply an iterative method, we first transferred the linear algebra problem A x = b to a fixed point problem x = f(x), and only then applied the iterative method to the latter. To prove Theorem 3, we go backward: from the fixed point equation we go to the linear algebra one.

Convergence means that "as iterations increase, the values of the iterates get closer and closer to a limiting value:' This means that x(k) converge to r, As usual, we denote by x = c the true solution of the system A x = b.

We must show that r = c. Recall that the Jacobi's (GS is similar) iterates satisfy the equation \[ \Lambda\,{\bf x}^{(k+1)} = {\bf b} + \left( \Lambda - {\bf A} \right) {\bf x}^{(k)} , \qquad k=0,,2,\ldots . \tag{P3.1} \] The Gauss-Seidel method leads to a similar equation where A is further decomposed into upper and lower triangular parts. Application of limit to both sides of Eq.(P3.1), we obtain \[ \Lambda\,{\bf r} = {\bf b} + \left( \Lambda - {\bf A} \right) {\bf r} \qquad \iff \qquad {\bf 0} = {\bf b} - {\bf A}\,{\bf r} . \] Therefore, vector \( \displaystyle {\bf r} = \lim_{k\to \infty} {\bf x}^{(k)} \) satisfies the equation A x = b, so it must be equal to c.

Example 4: Let us reconsider the system four equations with four unknowns:
\[ \begin{split} 9 x_1 - 8 x_2 + 5 x_3 - 4 x_4 &= 9 , \\ 1 x_1 + 8 x_2 -5 x_3 + 3 x_4 &= 7 , \\ -2 x_1 -4 x_2 + 7 x_3 -6 x_4 &= -11, , \\ 2 x_1 -4 x_2 -5 x_3 + 6 x_4 &= 9 . \end{split} \tag{4.1} \]
The matrix of this system \[ {\bf A} = \begin{bmatrix} \phantom{-}9 & -8 & \phantom{-}5 & -4 \\ \phantom{-}1 & \phantom{-}8 & -5 & \phantom{-}3 \\ -2 & -4 & \phantom{-}7 & -6 \\ \phantom{-}2 & -4 & -5 & \phantom{-}6 \end{bmatrix} \tag{4.2} \] is singular of rank 3:
A = {{9, -8, 5, -4}, {1, 8, -5, 3}, {-2, -4, 7, -6}, {2, -4, -5, 6}};
MatrixRank[A]
3
Although we know one of solutions, x₁ = 2, x₂ = 1, x₃ = 3, x₄ = 4, we demonstrate the iterative method---Jacobi's one. Since the nullspace of A is spanned on the vector [12, 31, 124, 120], the general solution of Eq.(4.1) depends on real parameter t ∈ ℝ: \[ {\bf x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \end{bmatrix} = \begin{bmatrix} 2 \\ 1 \\ 3 \\ 4 \end{bmatrix} + t \begin{bmatrix} 12 \\ 31 \\ 124 \\ 120 \end{bmatrix} \in \mathbb{R}^{4\times 1} . \]
A = {{9, -8, 5, -4}, {1, 8, -5, 3}, {-2, -4, 7, -6}, {2, -4, -5, 6}};
NullSpace[A]
{{12, 31, 124, 120}}
Upon extracting diagonal matrix from A, we split it into sum of two matrices \[ {\bf A} = \Lambda + {\bf M} , \qquad \Lambda = \begin{bmatrix} 9 & 0 & 0 & 0 \\ 0 & 8 & 0 & 0 \\ 0 & 0 & 7 & 0 \\ 0 & 0 & 0 & 6 \end{bmatrix} , \quad {\bf M} = \begin{bmatrix} \phantom{-}0 & -8 & \phantom{-}5 & -4 \\ \phantom{-}1 & \phantom{-}0 & -5 & \phantom{-}3 \\ -2 & -4 & \phantom{-}0 & -6 \\ \phantom{-}2 & -4 & -5 & \phantom{-}0 \end{bmatrix} . \] Then the given system of equations (4.2) can be rewritten in equivalent form \[ {\bf x} = \Lambda^{-1} {\bf b} - \Lambda^{-1} {\bf M}\,{\bf x} = {\bf d} + {\bf B}\,{\bf x} , \] where \[ {\bf d} = \Lambda^{-1} {\bf b} = \begin{bmatrix} 1 \\ \frac{7}{8} \\ -\frac{11}{7} \\ \frac{3}{2} \end{bmatrix} , \quad {\bf B} = - \Lambda^{-1} {\bf M} = \begin{bmatrix} 0 & \frac{8}{9} & - \frac{5}{9} & \frac{4}{9} \\ -\frac{1}{8} & 0 & \frac{5}{8} & - \frac{3}{8} \\ \frac{2}{7} & \frac{4}{7} & 0 & \frac{6}{7} \\ - \frac{1}{3} &\frac{2}{3} & \frac{5}{6} & 0\end{bmatrix} . \]
A = {{9, -8, 5, -4}, {1, 8, -5, 3}, {-2, -4, 7, -6}, {2, -4, -5, 6}};
diag = {{9, 0, 0, 0}, {0, 8, 0, 0}, {0, 0, 7, 0}, {0, 0, 0, 6}};
M = A - diag;
Inverse[diag] . {9, 7, -11, 9}
{1, 7/8, -(11/7), 3/2}
B = -Inverse[diag] . M
{{0, 8/9, -(5/9), 4/9}, {-(1/8), 0, 5/8, -(3/8)}, {2/7, 4/7, 0, 6/7}, {-(1/3), 2/3, 5/6, 0}}
The Jacobi scheme generates approximants according to the formula \[ {\bf x}^{(k+1)} = {\bf d} + {\bf B}\,{\bf x}^{(k)} , \qquad k=0, 1, 2, \ldots . \tag{4.3} \] Eigenvalues of matrix B can be determined with Mathematica
N[Eigenvalues[B]]
{1., -0.576902 + 0.664232 I, -0.576902 - 0.664232 I, 0.153804}
Since one of the eigenvalues λ₁ = 1, we do not expect convergence of Jacobi's iteration procedure. Other eigenvalues λ₂ = 0.153804, λ₃ = -0.576902 + 0.664232j, λ₄ = -0.576902 − 0.664232j, have magnitudes less than 1.
Sqrt[0.5769020858621225^2 + 0.6642324328522395^2]
0.879784
Starting with zero vector x(0) = 0, we obtain the first approximant to be x(1) = d. The second and third approximations become \[ {\bf x}^{(2)} = {\bf d} + {\bf B}\,{\bf x}^{(1)} = {\bf d} + {\bf B}\,{\bf d} = \begin{pmatrix} \frac{209}{63} \\ -\frac{89}{112} \\ \frac{1}{2} \\ \frac{37}{84} \end{pmatrix} \] and \[ {\bf x}^{(3)} = {\bf d} + {\bf B}\,{\bf x}^{(2)} = {\bf d} + {\bf B} \left( {\bf d} + {\bf B}\,{\bf b} \right) = {\bf b} + {\bf B}\,{\bf b} + {\bf B}^2 {\bf b} = \begin{pmatrix} \frac{40}{189} \\ \frac{175}{289} \\ -\frac{1235}{1764} \\ \frac{425}{1512} \end{pmatrix} . \]
diag = {{9, 0, 0, 0}, {0, 8, 0, 0}, {0, 0, 7, 0}, {0, 0, 0, 6}};
M = A - diag ;
d = {1, 7/8, -(11/7), 3/2}; B = -Inverse[diag] . M ;
x1 = d + B . d
{209/63, -(89/112), 1/2, 37/84}
x2 = d + B . d + B . B . d
{40/189, 175/288, -(1235/1764), 425/1512}
Now it is right time to organize our calculations in a loop.

Table of Jacobi iterates
n 0 1 2 3 4 5 10
x 0 \( \displaystyle 1 \) \( \displaystyle \frac{209}{63} \approx 3.31746 \) \( \displaystyle \frac{40}{189} \approx 0.21164 \) \( \displaystyle \approx 2.054 \) \( \displaystyle \approx 2.34034 \) \( \displaystyle \approx 2.31208 \)
x 0 \( \displaystyle \frac{7}{8} \) \( \displaystyle -\frac{89}{112} \approx -0.794643 \) \( \displaystyle \frac{175}{288} \approx 0.607639 \) \( \displaystyle \approx 0.305567 \) \( \displaystyle \approx -0.427675 \) \( \displaystyle \approx -0.128371 \)
x 0 \( \displaystyle -\frac{11}{7} \) \( \displaystyle \frac{1}{2} = 0.5 \) \( \displaystyle -\frac{1235}{1764} \approx -0.700113 \) \( \displaystyle \approx -0.922808 \) \( \displaystyle \approx 0.262426 \) \( \displaystyle \approx -0.257853 \)
x 0 \( \displaystyle \frac{3}{2} \) \( \displaystyle \frac{37}{84} \approx 0.440476 \) \( \displaystyle \frac{425}{1512} \approx 0.281085 \) \( \displaystyle \approx 1.25112 \) \( \displaystyle \approx 0.250038 \) \( \displaystyle \approx 0.729597 \)
The numerical values in the table above confirm that Jacobi's iteration diverges for this system of equations.

We are going to make a numerical experiment and check whether Jacobi's iteration depends on the initial guess, which we take closer to the true values: x(0) = [1.5, 0.5, 2.5, 3.5]. Using iteration (4.3), we calculate Jacobi's approximants.

Table of Jacobi iterates
n 0 1 2 3 4 5 10
x 1.5 1.61111 2.16138 1.84245 1.84634 2.09821 1.99034
x 0.5 0.9375 0.731647 0.810433 0.906534 0.738926 0.815846
x 2.5 2.14286 2.35317 2.355912.35591 2.18519 2.37932 2.2872
x 3.5 3.41667 3.37368 3.22828 3.3894 3.3099 3.35227

Mathematica confirms these calculations.

Last[Prepend[ FoldList[N[d + B . #] &, {0, 0, 0, 0}, Range[5]], {"x1", "x2", "x3", "x4"}]]
{2.34034, -0.427675, 0.262426, 0.250038}

TableForm[ Transpose[ Join[Prepend[ FoldList[N[d + B . #] &, {1.5, 0.5, 2.5, 3.5}, Range[5]], {"x1", "x2", "x3", "x4"}], {Last[ Prepend[FoldList[N[d + B . #] &, {1.5, 0.5, 2.5, 3.5}, Range[10]], {"x1", "x2", "x3", "x4"}]]}]], TableHeadings -> {None, {"n", "0", "1", "2", "3", "4", "5", "10"}}]

This table of iterative values shows that the first coordinate plays around true value x₁ = 2, but we cannot claim convergence of other coordinates, except probably x₂ = 1.

jacMeth[({{9, -8, 5, -4}, {1, 8, -5, 3}, {-2, -4, 7, -6}, {2, -4, -5, 6}} ), ({ {9}, {7}, {-11}, {9} }), 50]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 9 & -8 & 5&4 \\ 1 & 8 & -5&3 \\ =2 & -4 & 7&-6 \\ 2&-4&-5&6 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 9 & 0& 0&0 \\ 0 & 8&0&0 \\ 0&0& 7&0 \\ 0&0&0&6 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 & 0 & 0&0 \\ 1 & 0 & 0&0 \\ =2 & -4 & 0&0 \\ 2&-4&-5&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 & -8 & 5&4 \\ 0 & 0 & -5&3 \\ 0 & 0 & 0&-6 \\ 0&0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 9 \\ 7 \\ -11 \\ 9 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \\ 0\end{pmatrix} \)
Iterations 0 46 47 48 49 50
(0, 0, 0, 0) {1.67103,
0.131896,
-0.455655,
0.651516}
{1.65994,
0.137018,
-0.46018,
0.651207}
{1.66687,
0.135692,
-0.460686,
0.654547}
{1.66746,
0.133257,
-0.4566,
0.650932}
{1.66142,
0.137093,
-0.460923,
0.652518}
End of Example 4

Input: square matrix A and vector b
n is the size of A and vector b
    whle precision condition  do
       for i=1 : n do
           s=0
	   for j=1 : n do
	       if ji then
	   s = s + 𝑎i,jxj
	       endif
	   endfor
	   yi = (bis)/𝑎i,i
	endfor
	   x=y
endwhile	   

Gauss--Seidel method

Philipp Seidel
Carl Gauss
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 (1777--1855) and Philipp Ludwig von Seidel (1821--1896), and is similar to the Jacobi method. 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

Philipp Ludwig von Seidel worked in analysis, probability theory, astronomy, and optics. Unfortunately, he suffered from eye problems and retired at a young age. The paper in which he described the method now known as Gauss­ Seidel was published in 1874.

The Gauss-Seidel method, which is a modification of the Jacobi algorithm, uses each new value as soon as it is available. This method is based on splitting the matrix A of Eq.(2) into three parts:

\[ {\bf A} = {\bf S} + {\bf U} , \]
where
\[ {\bf A} = \begin{bmatrix} a_{1,1} & a_{1,2} & a_{1,3} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & a_{2,3} & \cdots & a_{2,n} \\ a_{3,1} & a_{3,2} & a_{3,3} & \cdots & a_{3,n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ a_{n,1} & a_{n,2} & a_{n,3} & \cdots & a_{n,n} \end{bmatrix} , \qquad {\bf S} = \begin{bmatrix} a_{1,1} & 0 & 0 & \cdots & 0 \\ a_{2,1} & a_{2,2} & 0 & \cdots & 0 \\ a_{3,1} & a_{3,2} & a_{3,3} & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ a_{n,1} & a_{n,2} & a_{n,3} & \cdots & a_{n,n} \end{bmatrix} , \qquad {\bf U} = \begin{bmatrix} 0 & a_{1,2} & a_{1,3} & \cdots & a_{1,n} \\ 0 & 0 & a_{2,3} & \cdots & a_{2,n} \\ 0 & 0 & 0 & \cdots & a_{3,n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0&0&0& \cdots & 0 \end{bmatrix} . \]
Therefore, in general formula (4), M = U is the strictly upper triangular matrix extracted from A, and S is a lower triangular part of A. Then we have an equivalent to Eq.(2) system of equations
\[ {\bf S}\,{\bf x} = {\bf b} - {\bf M}\,{\bf x} = {\bf b} - {\bf U}\,{\bf x} . \]
Upon applying the inverse matrix S−1 to both sides of the latter equation, we reduce the given system (2) to fixed point form (3):
\[ {\bf x} = {\bf S}^{-1} {\bf b} - {\bf S}^{-1} {\bf M}\,{\bf x} = {\bf d} + {\bf B}\,{\bf x} , \]
where
\[ {\bf d} = {\bf S}^{-1} {\bf b} , \qquad {\bf B} = - {\bf S}^{-1} {\bf U} . \]
Since U is a singular matrix, B = S−1U is also singular. The structure of the inverse matrix S−1 becomes clear when we consider its 3×3 version:
S = {{a11, 0, 0}, {a21, a22, 0}, {a31, a32, a33}};
Inverse[S]
{{1/a11, 0, 0}, {-(a21/(a11 a22)), 1/a22, 0}, {(-a22 a31 + a21 a32)/( a11 a22 a33), -(a32/(a22 a33)), 1/a33}}
\[ \begin{bmatrix} a_{1.1} & 0 & 0 \\ a_{2,1} & a_{2,2} & 0 \\ a_{3,1} & a_{3,2} & a_{3,3} \end{bmatrix}^{-1} = \begin{bmatrix} \frac{1}{a_{1,1}} & 0 & 0 \\ - \frac{a_{2,1}}{a_{1,1}\,a_{2,2}} & \frac{1}{a_{2,2}} & 0 \\ \frac{a_{2,1} a_{3,2} - a_{2,2} a_{3,1}}{a_{1,1} a_{2,2} a_{3,3}} & - \frac{a_{3,2}}{a_{2,2} a_{3,3}} & \frac{1}{a_{3,3}} \end{bmatrix} . \]
In case of 4 by 4 lower triangular matrix, we have
S = {{a11, 0, 0, 0}, {a21, a22, 0, 0}, {a31, a32, a33, 0}, {a41, a42, a43, a44}};
Inverse[S]
{{1{{1/a11, 0, 0, 0}, {-(a21/(a11 a22)), 1/a22, 0, 0}, {(-a22 a31 a44 + a21 a32 a44)/( a11 a22 a33 a44), -(a32/(a22 a33)), 1/a33, 0}, {(-a22 a33 a41 + a21 a33 a42 + a22 a31 a43 - a21 a32 a43)/( a11 a22 a33 a44), (-a11 a33 a42 + a11 a32 a43)/( a11 a22 a33 a44), -(a43/(a33 a44)), 1/a44}}/a11, 0, 0, 0}, {-(a21/(a11 a22)), 1/a22, 0, 0}, {(-a22 a31 a44 + a21 a32 a44)/( a11 a22 a33 a44), -(a32/(a22 a33)), 1/a33, 0}, {(-a22 a33 a41 + a21 a33 a42 + a22 a31 a43 - a21 a32 a43)/( a11 a22 a33 a44), (-a11 a33 a42 + a11 a32 a43)/( a11 a22 a33 a44), -(a43/(a33 a44)), 1/a44}}
\[ \begin{bmatrix} a_{1.1} & 0 & 0 &0 \\ a_{2,1} & a_{2,2} & 0&0 \\ a_{3,1} & a_{3,2} & a_{3,3} &0 \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \end{bmatrix}^{-1} = \begin{bmatrix} \frac{1}{a_{1,1}} & 0 & 0&0 \\ - \frac{a_{2,1}}{a_{1,1}\,a_{2,2}} & \frac{1}{a_{2,2}} & 0&0 \\ \frac{a_{2,1} a_{3,2} - a_{2,2} a_{3,1}}{a_{1,1} a_{2,2} a_{3,3}} & - \frac{a_{3,2}}{a_{2,2} a_{3,3}} & \frac{1}{a_{3,3}} &0 \\ \frac{a_{2,1} a_{3,3} a_{4,2} - a_{2,2} a_{3,3} a_{4,1} - a_{2,1} a_{3,2} a_{4,3} + a_{2,2} a_{3,1} a_{4,3}}{a_{1,1} a_{2,2} a_{3,3} a_{4,4}} & \frac{a_{3,2} a_{4,3} - a_{3,3} a_{4,2}}{a_{2,2} a_{3,3} a_{4,4}} & - \frac{a_{4,3}}{a_{3,3} a_{4,4}} & \frac{1}{a_{4,4}} \end{bmatrix} . \]

Although structure of the inverse to the lower triangular matrix S = Λ + L can be written explicitly, its practical implementation becomes problematic with size of matrix growth. The n × n matrix S−1 contains n divisions, and its entries contain alternated series, which could be a nightmare for numerical evaluations.

Instead of standard approach (5), the Gauss--Seidel method suggests to extract the strictly lower triangular matrix from S,

\[ {\bf S} = \Lambda + {\bf L} = \begin{bmatrix} a_{1,1} & 0 & 0 & \cdots & 0 \\ 0 & a_{2,2} & 0 & \cdots & 0 \\ 0 & 0 & a_{3,3} & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \dots & a_{n,n} \end{bmatrix} + \begin{bmatrix} 0 & 0 & 0 & \cdots & 0 \\ a_{2,1} & 0 & 0 & \cdots & 0 \\ a_{3,1} & a_{3,2} & 0 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ a_{n,1} & a_{n,2} & a_{n,3} & \cdots & 0 \end{bmatrix} . \]
Then the given vector equation A x = b can be written as
\[ \Lambda\,{\bf x} = {\bf b} - {\bf L}\,{\bf x} - {\bf U}\,{\bf x} \qquad \iff \qquad {\bf x} = \Lambda^{-1} {\bf b} - \Lambda^{-1} {\bf L}\,{\bf x} - \Lambda^{-1} {\bf U}\,{\bf x} . \]
The latter leads to the Gauss-Seidel iteration procedure in matrix form:
\[ {\bf x}^{(k+1)} = \Lambda^{-1} \left[ {\bf b} - {\bf L}\,{\bf x}^{(k+1)} - {\bf U}\,{\bf x}^{(k)} \right] . \]

Now we demonstrate the iterative procedure above in more detail by showing step by step its application for solving the system of linear equations (2). The first coordinate of x(k+1) is given by Jacobi's formula

\[ x_1^{(k+1)} = \frac{1}{a_{1,1}} \left( b_1 - \sum_{i=2}^n a_{1,i} x_i^{(k)} \right) . \]
The second coordinate of x(i+1) can be solved for in terms of the first coordinate and the last n − 2 coordinates of x(i):
\[ x_2^{(k+1)} = \frac{1}{a_{2,2}} \left( b_2 - a_{2,1} x_1^{(k+1)} - \sum_{i=3}^n a_{j,i} x_i^{(k)} \right) . \]
In general,
\begin{equation} \label{EqSeidel.8} x_j^{(k+1)} = \frac{1}{a_{j,j}} \left( b_j - \sum_{i=1}^{j-1} a_{j,i} x_i^{(k+1)} - \sum_{i=j+1}^n a_{1,i} x_i^{(k)} \right) . \end{equation}

It is interesting to compare the Jacobi and Gauss–Seidel iterations noting that the latter is using the coordinates of x(k+1) as soon as they are calculated rather than in the next iteration. The program for the Gauss–Seidel iteration is actually simpler than the program for the Jacobi iteration. The vectors x(k) and x(k+1) are both stored in the same vector, x. As a coordinate of x(k+1) is calculated, it replaces the corresponding coordinate of x(k).

Here is algorithm for Gauss-Seidel iteration scheme:

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
                    σ ← σ + 𝑎i,jφj
                end if
            end (j-loop)
            φi ← (bi − σ) / 𝑎i,i
        end (i-loop)
        check if convergence is reached
end (repeat
Another algorithm using while loop:

input A and b
n is the size of A
while precision conditions do
   for i = 1 : n do
      s = 0
      for j = 1 : n do
         if ji then
             s = s + 𝑎i,j xj
         end if
      end for
      xi = (1/𝑎i,i) (bis)
    end for
end while
Example 5: We consider a linear system with three unknowns: \[ \begin{bmatrix} \phantom{-}4 & 2 & -2 \\ -2 & 5 & \phantom{-}2 \\ \phantom{-}4 & 3 & -2 \end{bmatrix} \begin{pmatrix} x \\ y \\ z \end{pmatrix} = \begin{pmatrix} 12 \\ 4 \\ 14 \end{pmatrix} \tag{5.1} \] that has the true solution x = 1, y = 2, z = −2.
A = {{4, 2, -2}, {-2, 5, 2}, {4, 3, -2}};
b = {12, 4, 14}; LinearSolve[A, b]
{1, 2, -2}

If you use column vector b instead of row vector, LinearSolve command should be applied as follows.

LinearSolve[({ {4, 2, -2}, {-2, 5, 2}, {4, 3, -2} }), ({ {12}, {4}, {14} })][[All, 1]]

Jacobi's algorithm requires extraction of the diagonal matrix A = λ + M, where \[ {\bf A} = \begin{bmatrix} \phantom{-}4 & 2 & -2 \\ -2 & 5 & \phantom{-}2 \\ \phantom{-}4 & 3 & -2 \end{bmatrix} , \qquad \Lambda = \begin{bmatrix} 4 & 0 & \phantom{-}0 \\ 0 & 5 & \phantom{-}0 \\ 0 & 0 & -2 \end{bmatrix} , \quad {\bf M} = \begin{bmatrix} \phantom{-}0 & 2 & -2 \\ -2 & 0 & \phantom{-}2 \\ \phantom{-}4 & 3 & \phantom{-}0 \end{bmatrix} . \]

A = {{4, 2, -2}, {-2, 5, 2}, {4, 3, -2}};
diag = DiagonalMatrix[{4, 5, -2}]
{{4, 0, 0}, {0, 5, 0}, {0, 0, -2}}
We rewrite the given system of equations in equivalent form \[ \Lambda\,{\bf x} = {\bf b} - {\bf M}\,{\bf x} \qquad \iff \qquad {\bf x} = {\bf d} + {\bf B}\,{\bf x} , \tag{5.2} \] where \[ {\bf d} = \Lambda^{-1} {\bf b} = \begin{pmatrix} 3 \\ \frac{4}{5} \\ -7 \end{pmatrix} , \qquad {\bf B} = -\Lambda^{-1} {\bf M} = \begin{bmatrix} 0 & - \frac{1}{2} & \frac{1}{2} \\ \frac{2}{5} & 0 & - \frac{2}{5} \\ 2 & \frac{3}{2} & 0 \end{bmatrix} . \]
M = A - diag;
B = -Inverse[diag] . M
{{0, -(1/2), 1/2}, {2/5, 0, -(2/5)}, {2, 3/2, 0}}
Although matrix B is not diagonally dominant, its eigenvalues have magnitude less than 1. So we expect that Jacobi's iteration scheme converges.

Upon choosing the initial guess to be zero, we obtain the first approximation to be \[ {\bf x}^{(1)} = {\bf d} = \begin{pmatrix} 3 \\ \frac{4}{5} \\ -7 \end{pmatrix} . \] The second iteration step yields \[ {\bf x}^{(2)} = {\bf d} + {\bf B}\,{\bf x}^{(1)} = \begin{pmatrix} - \frac{9}{10} \\ \frac{24}{5} \\ \frac{1}{5} \end{pmatrix} = \begin{pmatrix} -0.9 \\ 4.8 \\ 0.2 \end{pmatrix} . \]

d = {3, 4/5, -7};
x2 = d + B . d
{-(9/10), 24/5, 1/5}
With third iteration, we have \[ {\bf x}^{(3)} = {\bf d} + {\bf B}\,{\bf x}^{(2)} = {\bf d} + {\bf B}\,{\bf d} + {\bf B}^2 {\bf d} = \begin{pmatrix} \frac{7}{10} \\ \frac{9}{25} \\ -\frac{8}{5} \end{pmatrix} = \begin{pmatrix} 0.7 \\ 0.36 \\ -1.6 \end{pmatrix} . \] Application of jacMeth code from Example 1 yields
jacMeth[({ {4, 2, -2}, {-2, 5, 2}, {4, 3, -2} }), ({ {12}, {4}, {14} }), 5, False]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 4 & 2 & -2 \\ -2 & 5 & 2 \\ 4 & 3 & -2 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 4 & 0& 0 \\ 0 & 5&0 \\ 0&0& -2 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ -2&0&0 \\ 4& 3 & 0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&2 & -2 \\ 0&0& 2 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 12 \\ 4 \\ 14 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 0 1 2 3 4 5
\( \displaystyle \begin{pmatrix} 0&0&0 \end{pmatrix} \) \( \displaystyle \left( 3, \ \frac{4}{5}, \ -7 \right) \) \( \displaystyle \left( -\frac{9}{10}, \ \frac{24}{5},\ \frac{1}{5} \right) \) \( \displaystyle \left( \frac{7}{10},\ \frac{9}{25},\ -\frac{8}{5} \right) \) \( \displaystyle \left( \frac{101}{50},\ \frac{43}{25},\ -\frac{253}{50} \right) \) \( \displaystyle \left( -\frac{39}{100},\ \frac{454}{125}, \ -\frac{19}{50} \right) \)

The errors of Jacobi's iterations at 50-th run are \[ \left\vert x_1^{(50)} -1 \right\vert = 0.039291 , \qquad \left\vert x_2^{(50)} -2 \right\vert = 0.0101124 , \qquad \left\vert x_3^{(50)} +2 \right\vert = 0.069383 . \]

We repeat calculations with 50 runs. It took less than a second to execute the code.

jacMeth[({ {4, 2, -2}, {-2, 5, 2}, {4, 3, -2} }), ({ {12}, {4}, {14} }), 50, True] // Timing
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 4 & 2 & -2 \\ -2 & 5 & 2 \\ 4 & 3 & -2 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 4 & 0& 0 \\ 0 & 5&0 \\ 0&0& -2 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ -2&0&0 \\ 4& 3 & 0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&2 & -2 \\ 0&0& 2 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 12 \\ 4 \\ 14 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 45 46 47 48 49 50
{0.950528,
2.01566,
-2.08244}
{0.95095,
2.01319,
-2.07545}
{0.955682,
2.01056,
-2.07832}
{0.95556,
2.0136,
-2.0728}
{0.956802,
2.01134,
-2.06848}
{0.960089,
2.01011,
-2.06938}

Now we apply the Gauss--Seidel method with the same initial choice---the origin. At the first step, we have \begin{align*} x_1^{(1)} &= 3 - \frac{1}{2}\, x_2^{(0)} + \frac{1}{2}\, x_3^{(0)} = 3, \\ x_2^{(1)} &= \frac{4}{5} + \frac{2}{5}\, x_1^{(1)} - \frac{2}{5}\, x_3^{(0)} = 2 \\ x_3^{(1)} &= -7 + 2\,x_1^{(1)} + \frac{3}{2}\,x_2^{(1)} = 2 . \end{align*} Next iteration gives \begin{align*} x_1^{(2)} &= 3 - \frac{1}{2}\, x_2^{(1)} + \frac{1}{2}\, x_3^{(1)} = 3, \\ x_2^{(2)} &= \frac{4}{5} + \frac{2}{5}\, x_1^{(2)} - \frac{2}{5}\, x_3^{(1)} = \frac{6}{5} , \\ x_3^{(2)} &= -7 + 2\,x_1^{(2)} + \frac{3}{2}\,x_2^{(2)} = \frac{4}{5} \end{align*} We manually repeat a few iterations before application of looping procedure. \begin{align*} x_1^{(3)} &= 3 - \frac{1}{2}\, x_2^{(2)} + \frac{1}{2}\, x_3^{(2)} = \frac{14}{5} , \\ x_2^{(3)} &= \frac{4}{5} + \frac{2}{5}\, x_1^{(3)} - \frac{2}{5}\, x_3^{(2)} = \frac{8}{5} , \\ x_3^{(3)} &= -7 + 2\,x_1^{(3)} + \frac{3}{2}\,x_2^{(3)} = 1 , \end{align*} and \begin{align*} x_1^{(4)} &= 3 - \frac{1}{2}\, x_2^{(3)} + \frac{1}{2}\, x_3^{(3)} = \frac{27}{10} , \\ x_2^{(4)} &= \frac{4}{5} + \frac{2}{5}\, x_1^{(3)} - \frac{2}{5}\, x_3^{(3)} = \frac{37}{25} , \\ x_3^{(4)} &= -7 + 2\,x_1^{(4)} + \frac{3}{2}\,x_2^{(4)} = \frac{31}{50} ; \end{align*} \begin{align*} x_1^{(5)} &= 3 - \frac{1}{2}\, x_2^{(4)} + \frac{1}{2}\, x_3^{(4)} = \frac{257}{100} = 2.57 , \\ x_2^{(5)} &= \frac{4}{5} + \frac{2}{5}\, x_1^{(5)} - \frac{2}{5}\, x_3^{(4)} = \frac{79}{50} = 1.58 , \\ x_3^{(5)} &= -7 + 2\,x_1^{(5)} + \frac{3}{2}\,x_2^{(5)} = \frac{51}{100} = 0.51 . \end{align*} Sequential iterates are generated with the following Mathematica code:

gsMeth[mat_List, b_List, n_ : 5, fp_ : True] := Module[{dia, low, up, len, its}, If[n >= 5, dia = DiagonalMatrix[Diagonal[mat]]; low = LowerTriangularize[mat, -1]; up = UpperTriangularize[mat, 1]; len = ConstantArray[0, Length[Diagonal[mat]]]; its = If[fp == True, Prepend[Take[ FoldList[N[LinearSolve[dia + low, -up . # + b]][[All, 1]] &, len, Range[n]], -6], ""], Prepend[Take[ FoldList[LinearSolve[dia, -(low + up) . # + b][[All, 1]] &, len, Range[n]], -6], ""]](*end second IF*); Grid[{{"Matrix", "A", "d", "l", "u", "b", "x0"}, Take[MatrixForm[#] & /@ {"", mat, dia, low, up, b, len}, -7], Join[{"Iterations"}, {"0"}, Take[Range[0, n], -5]], its}, Frame -> All], "Must have n\[GreaterEqual]5"](*end first If*) ];
gsMeth[({ {4, 2, -2}, {-2, 5, 2}, {4, 3, -2} }), ({ {12}, {4}, {14} }), 5, False]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 4 & 2 & -2 \\ -2 & 5 & 2 \\ 4&3&-2 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 4 & 0&0 \\ 0 & 5&0 \\ 0&0& -2 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ -2&0&0 \\ 4&3&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&2&-2 \\ 0&0&2 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 12 \\ 4 \\ 14 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 0 1 2 3 4 5
{0, 0, 0} \( \displaystyle \left\{ 3, \ 2,\ 2 \right\} \) \( \displaystyle \left\{ 3,\ \frac{6}{5},\ \frac{4}{5} \right\} \) \( \displaystyle \left\{ \frac{!4}{5},\ \frac{8}{5}, \ 1 \right\} \) \( \displaystyle \left\{ \frac{27}{10},\ \frac{37}{25},\ \frac{31}{50} \right\} \) \( \displaystyle \left\{ \frac{257}{100},\ \frac{79}{50},\ \frac{51}{100} \right\} \)

We repeat GS-iterative procedure with 50 runs. It tAkes less than a second to execute 50 runs.

gsMeth[({ {4, 2, -2}, {-2, 5, 2}, {4, 3, -2} }), ({ {12}, {4}, {14} }), 50]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 4 & 2 & -2 \\ -2 & 5 & 2 \\ 4&3&-2 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 4 & 0&0 \\ 0 & 5&0 \\ 0&0& -2 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ -2&0&0 \\ 4&3&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&2&-2 \\ 0&0&2 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 12 \\ 4 \\ 14 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 45 46 47 48 49 50
{1.08661,
1.97577,
-1.86312}
1.08055,
1.97747,
-1.87269}
{1.07492,
1.97904,
-1.88159}
{1.06968,
1.98051,
-1.88987}

The errors of the Gauss--Seidel iterations at 50-th run are \[ \left\vert x_1^{(50)} -1 \right\vert = 0.0602767 , \qquad \left\vert x_2^{(50)} -2 \right\vert = 0.0181273 , \qquad \left\vert x_3^{(50)} +2 \right\vert = 0.095263 . \] As a result of this numerical experiment we conclude that both methods, Jacobi and GS, are very fast and provide approximately the same accuracy, but Jacobi's iteration scheme gives slightly better approximation. Also, a usage of floating foint instead of exact operations with rational numbers does not effect the time of execution.

End of Example 5
Example 6: Let us reconsider the system from Example 3: \[ \begin{split} 5\,x_1 - 4\, x_2 + x_3 &= 5, \\ 5\,x_1 + 7\, x_2 -4\, x_3 &= 21, \\ 3\, x_1 - 7\, x_2 + 8\, x_3 &= -9 , \end{split} \tag{3.1} \] with matrix not being diagonally dominant: \[ {\bf A} = \begin{bmatrix} 5 & -4 & \phantom{-}1 \\ 5 & \phantom{-}7 & -4 \\ 3 & -7 & \phantom{-}8 \end{bmatrix} . \] Now it is right time to organize our calculations in a loop.

gsMeth[({ {5, -4, -1}, {5, 7, -4}, {3, -7, 8} }), ({ {5}, {21}, {-9} }), 5, False]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 5 & -4 & 1 \\ 5 & 7 & -4 \\ 3&-7&8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 & 0&0 \\ 0 &7&0 \\ 0&0& 8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ 5&0&0 \\ 3&-7&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-4&1 \\ 0&0&-4 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 \\ 21 \\ -9 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 0 1 2 3 4 5
{0, 0, 0} \( \displaystyle \left\{ 1, \ 3,\ -\frac{9}{8} \right\} \) \( \displaystyle \left\{ \frac{29}{8},\ \frac{23}{14},\ \frac{9}{8} \right\} \) \( \displaystyle \left\{ \frac{!17}{56},\ \frac{59}{56}, \ -\frac{67}{64} \right\} \) \( \displaystyle \left\{ \frac{4597}{2240},\ \frac{713}{784},\ -\frac{221}{224} \right\} \) \( \displaystyle \left\{ \frac{15091}{7840},\ \frac{3043}{3136},\ -\frac{2813}{2560} \right\} \)

We repeat calculations with floating point arithmetic for 50 runs.

gsMeth[({ {5, -4, -1}, {5, 7, -4}, {3, -7, 8} }), ({ {5}, {21}, {-9} }), 50, True]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 5 & -4 & 1 \\ 5 & 7 & -4 \\ 3&-7&8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 & 0&0 \\ 0 &7&0 \\ 0&0& 8 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0 \\ 5&0&0 \\ 3&-7&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-4&1 \\ 0&0&-4 \\ 0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 5 \\ 21 \\ -9 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 0 46 47 48 49 50
{1.86139,
1.25743,
-0.722772}
{1.86139,
1.25743,
-0.722772}
{1.86139,
1.25743,
-0.722772}
{1.86139,
1.25743,
-0.722772}
{1.86139,
1.25743,
-0.722772}
1.86139,
1.25743,
-0.722772}

This example shows that the Gauss--Seidel method converges very slowly: after 50 runs it gives the answer with about 25% accuracy. So Jacobi's method performs much better.

End of Example 6
Example 7: Let us reconsider the system four equations with four unknowns:
\[ \begin{split} 9 x_1 - 8 x_2 + 5 x_3 - 4 x_4 &= 9 , \\ 1 x_1 + 8 x_2 -5 x_3 + 3 x_4 &= 7 , \\ -2 x_1 -4 x_2 + 7 x_3 -6 x_4 &= -11, , \\ 2 x_1 -4 x_2 -5 x_3 + 6 x_4 &= 9 . \end{split} \tag{6.1} \]
The matrix of this system \[ {\bf A} = \begin{bmatrix} \phantom{-}9 & -8 & \phantom{-}5 & -4 \\ \phantom{-}1 & \phantom{-}8 & -5 & \phantom{-}3 \\ -2 & -4 & \phantom{-}7 & -6 \\ \phantom{-}2 & -4 & -5 & \phantom{-}6 \end{bmatrix} \tag{6.2} \] is singular of rank 3. We split the matrix into lower triangular and upper triangular parts:
\[ {\bf A} = {\bf L} + {\bf U} , \] where \[ {\bf L} = \begin{bmatrix} \phantom{-}9 & 0 & \phantom{-}0 & 0 \\ \phantom{-}1 & \phantom{-}8 & \phantom{-}0 & 0 \\ -2 & -4 & \phantom{-}7 & 0 \\ \phantom{-}2 & -4 & -5 & 6 \end{bmatrix} , \qquad {\bf U} = \begin{bmatrix} 0 & -8 & \phantom{-}5 & -4 \\ 0 & \phantom{-}0 & -5 & \phantom{-}3 \\ 0 & \phantom{-}0 & \phantom{-}0 & -6 \\ 0 & \phantom{-}0 & \phantom{-}0 & \phantom{-}0 \end{bmatrix} . \] This allows us to rewrite equation (6.2) in fixed point form: \[ {\bf L}\,{\bf x} = {\bf b} - {\bf U}\,{\bf x} \qquad \iff \qquad {\bf x} = {\bf L}^{-1} {\bf b} - {\bf L}^{-1} {\bf U}\,{\bf x} . \tag{6.3} \] Upon introducing auxiliary matrices, \[ {\bf d} = {\bf L}^{-1} {\bf b} = \begin{pmatrix} 1 \\ \frac{3}{4} \\ -\frac{6}{7} \\ \frac{20}{21} \end{pmatrix} , \qquad {\bf B} = - {\bf L}^{-1} {\bf U} = \begin{bmatrix} 0 & \frac{8}{9} & - \frac{5}{9} & \frac{4}{9} \\ 0 & - \frac{1}{9} &\frac{25}{36} & -\frac{31}{72} \\ 0 & \frac{4}{21} & \frac{5}{21} & \frac{31}{42} \\ 0 & - \frac{40}{189} & \frac{160}{189} & \frac{34}{189} \end{bmatrix} , \]
b = {9, 7, -11, 9};
U =U = {{0, -8, 5, -4}, {0, 0, -5, 3}, {0, 0, 0, -6}, {0, 0, 0, 0}} {{0, -8, 5, -4}, {0, 0, -5, 3}, {0, 0, 0, -6}, {0, 0, 0, 0}};
L = A - U
we rewrite Eq.(6.3) in compact form: \[ {\bf x} = {\bf d} + {\bf B}\, {\bf x} . \]
d = Inverse[L] . b
{1, 3/4, -(6/7), 20/21}
B = -Inverse[L] . U
{{0, 8/9, -(5/9), 4/9}, {0, -(1/9), 25/36, -(31/72)}, {0, 4/21, 5/21, 31/42}, {0, -(40/189), 160/189, 34/189}}

Eq.(6.3) is used to generate Gauss-Seidel iteration: \[ {\bf x}^{(k+1)} = {\bf L}^{-1} {\bf b} - {\bf L}^{-1} {\bf U}\,{\bf x}^{(k)} \qquad \iff \qquad {\bf x}^{(k+1)} = {\bf d} + {\bf B}\,{\bf x}^{(k)} , \qquad k=0,1,2,\ldots . \tag{6.4} \]

Matrix B has rank 3 and all its eigenvalues are real numbers: λ₁ = 1, λ₂ = 0, λ₃ ≈ -0.874609, λ₄ ≈ 0.181487. Therefore, we do not expect convergence of Gauss-Seidel iterations.

MatrixRank[B]
3
Eigenvalues[B]
{1, 1/378 (-131 - Sqrt[39841]), 1/378 (-131 + Sqrt[39841]), 0}
Upon choosing initial guess to be zero, we perform iterations according to the difference equation (6.4). Now it is right time to organize our calculations in a loop.

Table of Jacobi iterates
n 0 1 2 3 4 5 10
x 0 1 \( \displaystyle \frac{485}{189} \approx 2.56614 \) 0.925058 2.31112 1.08992 1.94983
x 0 \( \displaystyle -\frac{3}{4} \) \( \displaystyle -\frac{64}{189} \approx -0.338624 \) 0.534972 -0.243334 0.434793 -0.0439163
x 0 \( \displaystyle -\frac{6}{7} \) \( \displaystyle -\frac{95}{441} \approx -0.21542 \) -0.291808 -0.291808 -0.733657 -0.422114
x 0 \( \displaystyle \frac{20}{21} \) \( \displaystyle \frac{950}{3969} \approx .239355 \) 0.88474 0.324231 0.815174 0.469016

Running GS-iterations 50 times yields

gsMeth[({ {9, -8,5, -4}, {1, 8, -5, 3}, {-2, -4, 7, -6}, {2, -4, -5, 6} }), ({ {9}, {7}, {-11}, {9} }), 50, True]
Matrix A Λ L U b x0
\( \displaystyle \begin{pmatrix} 9 & -8 & 5& -4 \\ 1 & 8 & -5 &3 \\ -2&-4&7 & -6 \\ 2 & -4 &-5& 6 \\ 2& -4& -5& 6 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 9 & 0&0&0 \\ 0 &8&0 &0 \\ 0&0& 7& 0 \\ 0&0&0&6 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&0&0&0 \\ 1&0&0&0 \\ -2&-4&0&0 \\ 2& -4& -5&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0&-8&5&-4 \\ 0&0&-5&3 \\ 0&0&0&-6 \\ 0&0&0&0 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 9 \\ 7 \\ -11 \\ 9 \end{pmatrix} \) \( \displaystyle \begin{pmatrix} 0 \\ 0 \\ 0 \\ 0 \end{pmatrix} \)
Iterations 0 46 47 48 49 50
{1.65594,
0.119593,
-0.528556,
0.587284}
{1.66096,
0.116801,
-0.526738,
0.585265}
{1.65657,
0.119243,
-0.528328,
0.587031}
{1.66041,
0.117107,
-0.526937,
0.585486}
{1.65705,
0.118975,
-0.528154,
0.586837}
{1.65999,
0.117341,
-0.52709,
0.585656}

Therefore, we conclude that except the first coordinates, all other entries do not converge.

The following table presents Gauss--Seidel iterations with different initial guess.

x0 = {1.5, 0.5, 2.5, 3.5} {1.5, 0.5, 2.5, 3.5}
{1.5, 0.5, 2.5, 3.5}
x1 = d + B . x0
{1.61111, 0.923611, 2.41667, 3.59259}

Table of Jacobi iterates
n 0 1 2 3 4 5 10
x 1.5 1.61111 2.0751 1.81082 2.06765 1.84769 2.00357
x 0.5 0.923611 0.778807 0.946414 0.807257 0.930314 0.843734
x 2.5 2.41667 2.54586 2.44308 2.53482 2.45492 2.51133
x 3.5 3.59259 3.44905 3.56324 3.46131 3.550083.55008 3.48741

We repeat calculations with floating point arithmetic for 50 runs.

End of Example 7

Successive overrelaxation or SOR method

In numerical linear algebra, the method of successive over-relaxation (SOR) is a variant of the Gauss–Seidel method for solving a linear system of equations, resulting in faster convergence. It was devised simultaneously by David M. Young Jr. (1923--2008) and by Stanley P. Frankel (1919--1978) in 1950 for the purpose of automatically solving linear systems on digital computers.

When SOR method is applied to the traditional fixed point equation (3), it generates a sequence of approximations according to the formula

\[ {\bf x}^{(k+1)}_{SOR} = \left( 1 - \omega \right) {\bf x}^{(k)}_{SOR} + \omega\,f\left( {\bf x}^{(k)}_{SOR} \right) , \qquad k=0,1,2,\ldots , \]
where values of overrelaxation factor ω > 1 are used to speed up convergence of a slow-converging process, while values of ω < 1 are often used to help establish convergence of a diverging iterative process or speed up the convergence of an overshooting process. With ω = 1, we recover Gauss-Seidel method. The optimal choice of ω never exceeds 2. It is often in the neighborhood of 1.9.

To describe application of the overrelaxation for solving system of linear equations (1) or (2), we follow Gauss-Seidel approach and decompose matrix A of coefficients into sum of three matrices:

\[ {\bf A} = \Lambda + {\bf L} + {\bf U} , \]
where Λ is the diagonal matrix extracted from A, L is the strictly lower triangular part of A, and U is the strictly upper triangular part of A:
\[ \Lambda = \begin{bmatrix} a_{1,1} & 0 & 0 &\cdots & 0 \\ 0 & a_{2,2} & 0 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \cdots & a_{n,n}\end{bmatrix} , \qquad {\bf L} = \begin{bmatrix} 0 & 0 & 0 & \cdots & 0 \\ a_{2,1} & 0 & 0 & \cdots & 0 \\ a_{3,1} & a_{3,2} & 0 & \cdots & 0 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ a_{n,1} & a_{n,2} & a_{n,3} & \cdots & 0 \end{bmatrix} , \qquad {\bf U} = \begin{bmatrix} 0 & a_{1,2} & a_{1,3} & \cdots & a_{1n} \\ 0 & 0 & a_{2,3} & \cdots & a_{2,n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & 0 & \cdots & 0 \end{bmatrix} . \]
More specifically, if x(k) is the solution at the step k and xGS(k+1) is the update to the step k + 1 according to the Gauss--Siedel’s method, the update formula of the SOR method is:
\[ {\bf x}_{SOR}^{(k+1)} = \omega\,{\bf x}_{GS}^{(k+1)} + \left( 1 - \omega \right) {\bf x}^{(k)} , \]
where ω is a real parameter to be set. Obviously if ω = 1, the SOR algorithm degenerates into the Gauss-Siedel’s method. The explicit update formula of the SOR method can be simply obtained from that of Gauss-Seidel’s method by adding the contribution due to x(k):
\begin{align*} x_1^{(k+1)} &= \frac{\omega}{a_{1,1}} \left( b_1 - \sum_{j=2}^n a_{1,j} x_j^{(k)} \right) + \left( 1 - \omega \right) x_1^{(k)} , \\ x_2^{(k+1)} &= \frac{\omega}{a_{2,2}} \left( b_2 - \sum_{j=3}^n a_{2,j} x_j^{(k)} - a_{2,1} x_1^{(k+1)}\right) + \left( 1 - \omega \right) x_2^{(k)} , \\ \cdots & \qquad \cdots \\ x_i^{(k+1)} &= \frac{\omega}{a_{i,i}} \left( b_i - \sum_{j=i+1}^n a_{i,j} x_j^{(k)} - \sum_{j=1}^{i-1} a_{i,j} x_j^{(k+1)}\right) + \left( 1 - \omega \right) x_i^{(k)} , \tag{9} \\ \cdots & \qquad \cdots \\ x_n^{(k+1)} &= \frac{\omega}{a_{n,n}} \left( b_n - \sum_{j=1}^{n-1} a_{n,j} x_j^{(k+1)}\right) + \left( 1 - \omega \right) x_n^{(k)} . \end{align*}


Inputs: A, b, ω
Output: φ

Choose an initial guess φ to the solution
repeat until convergence
    for i from 1 until n do
        set σ to 0
        for j from 1 until n do
            if j ≠ i then
                set σ to σ + 𝑎i,j φj
            end if
        end (j-loop)
        set φi to (1 − ω)φi + ω(bi − σ) / 𝑎i,i
    end (i-loop)
    check if convergence is reached
end (repeat)    
Another algorithm using while loop:

input A and b
input ω
n is the size of A
while precision conditions do
   for i = 1 : n do
      s = 0
      for j = 1 : n do
         if j ne; i then
            s = s + 𝑎i,j xj
         end if
       end for
       xi = ω (bi − s)/ 𝑎i,i + (1 − ω ) xi
    end for
end while
Matlab algorithm:

function [x,r,k]=RM(A,b,Omega,kmax,eps1)
% kmax maximum number of iterations;
% eps1 given tolerance;
% Omega: relaxation parameter
% stopping criteria: norm(r)/norm(b)<=eps1 or k>kmax
% Output: x the kth iterate, r the kth residual
% k the number of iterations
% Initial guess is x=0; r=b
n=length(b);r=b;x=zeros(n,1);nb=norm(b);
E=tril(A,-1);D=diag(diag(A));M=(1/Omega)*D-E;
k=1;mr=1;mx=1;e=1;
while e>eps1 & k<=kmax
      dx=M\r;
      x=x+dx;% saxpy operation
      r=r-A*dx;% GAXPY operation
      e=norm(r)/nb;
      k=k+1;
end

Theorem 4 (Kahan, 1958): A necessary condition for the SOR method to converge is ω ∈ (0, 2).

Example 8: Let us reconsider the system from Example 3: \[ \begin{split} 5\,x_1 - 4\, x_2 + x_3 &= 5, \\ 5\,x_1 + 7\, x_2 -4\, x_3 &= 21, \\ 3\, x_1 - 7\, x_2 + 8\, x_3 &= -9 , \end{split} \tag{3.1} \] with matrix not being diagonally dominant: \[ {\bf A} = \begin{bmatrix} 5 & -4 & \phantom{-}1 \\ 5 & \phantom{-}7 & -4 \\ 3 & -7 & \phantom{-}8 \end{bmatrix} . \]
The SOR scheme yields \begin{align*} x_1^{(k+1)} &= \frac{\omega}{5} \left( 5 + 4\,x_2^{(k)} - x_3^{(k)} \right) + \left( 1 - \omega \right) x_1^{(k)} , \\ x_2^{(k+1)} &= \frac{\omega}{7} \left( 21 + 4\, x_3^{(k)} - 5 \, x_1^{(k+1)} \right) + \left( 1 - \omega \right) x_2^{(k)} , \\ x_3^{(k+1)} &= \frac{\omega}{8} \left( -9 - 3\,x_1^{(k+1)} +7 \, x_2{(k+1)} \right) + \left( 1 - \omega \right) x_3^{(k)} , \end{align*} Now it is right time to organize our calculations in a loop.

Table of SOR iterates with ω = 0.7
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{1}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)

Table of SOR iterates with ω = 1.7
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{1}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
End of Example 8
Example 9: Let us reconsider the system four equations with four unknowns:
\[ \begin{split} 9 x_1 - 8 x_2 + 5 x_3 - 4 x_4 &= 9 , \\ 1 x_1 + 8 x_2 -5 x_3 + 3 x_4 &= 7 , \\ -2 x_1 -4 x_2 + 7 x_3 -6 x_4 &= -11, , \\ 2 x_1 -4 x_2 -5 x_3 + 6 x_4 &= 9 . \end{split} \]
The matrix of this system \[ {\bf A} = \begin{bmatrix} \phantom{-}9 & -8 & \phantom{-}5 & -4 \\ \phantom{-}1 & \phantom{-}8 & -5 & \phantom{-}3 \\ -2 & -4 & \phantom{-}7 & -6 \\ \phantom{-}2 & -4 & -5 & \phantom{-}6 \end{bmatrix} \tag{4.2} \] is singular of rank 3:
The SOR scheme yields \begin{align*} x_1^{(k+1)} &= \frac{\omega}{9} \left( 9 + 8\,x_2^{(k)} -5\, x_3^{(k)} + 4\, x_4^{(k)} \right) + \left( 1 - \omega \right) x_1^{(k)} , \\ x_2^{(k+1)} &= \frac{\omega}{8} \left( 7 + 5\, x_3^{(k)} - 3\, x_4^{(k)} - x_1^{(k+1)} \right) + \left( 1 - \omega \right) x_2^{(k)} , \\ x_3^{(k+1)} &= \frac{\omega}{7} \left( -11 + 6\,x_4^{(k)} +2\, x_1^{(k+1)} +4 \, x_2{(k+1)} \right) + \left( 1 - \omega \right) x_3^{(k)} , \\ x_4^{(k+1)} &= \frac{\omega}{6} \left( 9 - 2\,x_1^{(k+1)} +4 \, x_2{(k+1)} + 5\,x_3^{(k+1)} \right) + \left( 1 - \omega \right) x_4^{(k)} , \end{align*} Now it is right time to organize our calculations in a loop.

Table of SOR iterates with ω = 0.8
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{1}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)

Table of SOR iterates with ω = 1.8
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{1}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
End of Example 9
Example 10: We demonstrate performance of three iterating methods for solving the following system of equations: \[ {\bf A}\,{\bf x} = {\bf b} , \tag{10.1} \] where \[ {\bf A} = \begin{bmatrix} \phantom{-}4 & -1 & 2 \\ -2 & \phantom{-}4 & 5 \\ \phantom{-}1 & \phantom{-}2 & 5 \end{bmatrix}, \qquad {\bf b} = \begin{pmatrix} -2 \\ -4 \\ -5 \end{pmatrix} . \] This system has the true solution x = [1, 2, −2]. We start with Jacobi's scheme by extracting the diagonal matrix from A: \[ \Lambda = \begin{bmatrix} 4 & 0 & 0 \\ 0 & 4 & 0 \\ 0 & 0 & 5 \end{bmatrix} , \qquad {\bf M} = \begin{bmatrix} \phantom{-}0 & -1 & 2 \\ -2 & \phantom{-}0 & 5 \\ \phantom{-}1 & \phantom{-}2 & 0 \end{bmatrix} . \] Although the given system has matrix A that is not diagonally dominant, Jacobi's scheme converges because eigenvalues of matrix M do not exceed 1.
A = {{4, -1, 2}, {-2, 4, 5}, {1, 2, 5}};
diag = DiagonalMatrix[{4, 4, 5}];
B = Inverse[diag] . (A - diag)
{{0, -(1/4), 1/2}, {-(1/2), 0, 5/4}, {1/5, 2/5, 0}}
N[Eigenvalues[B]]
{-0.946897, 0.702665, 0.244232}
We write Jacobi's iterations in matrix form: \[ {\bf x}^{(k+1)} = {\bf d} + {\bf B}\,{\bf x}^{(k)} , \qquad k=0,1,2,\ldots ; \] where \[ {\bf d} = \Lambda^{-1} {\bf b} = \begin{pmatrix} -\frac{1}{2} \\ -1 \\ -1 \end{pmatrix} , \qquad {\bf B} = \Lambda^{-1} {\bf M} = \begin{bmatrix} \phantom{-}0 & -\frac{1}{4} & \frac{1}{2} \\ -\frac{1}{2} & \phantom{-}0 & \frac{5}{4} \\ \phantom{-}\frac{1}{5} & \phantom{-}\frac{2}{5} & 0 \end{bmatrix} . \]

Table of Jacobi iterates
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{1}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
Application of the Gauss--Seidel method to system (10.1) of equations gives \begin{align*} x_1^{(k+1)} &= \frac{1}{4} \left( -2 + x_2^{(k)} -2\, x_2^{(k)} \right) , \\ x_2^{(k+1)} &= \frac{1}{4} \left( -4 + 2\,x_1^{(k+1)} -5\, x_3^{(k)} \right) , \\ x_3^{(k+1)} &= \frac{1}{5} \left( -5 - x_1^{(k+1)} -2\,x_2^{(k+1)} \right) . \end{align*}

Table of Jacobi iterates
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{1}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
The SOR scheme yields \begin{align*} x_1^{(k+1)} &= \frac{\omega}{4} \left( -2 + x_2^{(k)} -2\, x_3^{(k)} \right) + \left( 1 - \omega \right) x_1^{(k)} , \\ x_2^{(k+1)} &= \frac{\omega}{4} \left( -4 - 5\, x_3^{(k)} + 2 \, x_1^{(k+1)} \right) + \left( 1 - \omega \right) x_2^{(k)} , \\ x_3^{(k+1)} &= \frac{\omega}{5} \left( -5 - x_1^{(k+1)} -2 \, x_2{(k+1)} \right) + \left( 1 - \omega \right) x_3^{(k)} , \end{align*}

Table of SOR iterates with ω = 0.9
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{1}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
when ω = 1.9, we have

Table of SOR iterates with ω = 1.9
n 0 1 2 3 4 5
x 0 \( \displaystyle \frac{}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{1}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
x 0 \( \displaystyle -\frac{1}{} \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \) \( \displaystyle \frac{}{} \approx \)
End of Example 10

 

  1. Let \[ {\bf A} = \begin{bmatrix} 5 & 1 \\ 2 & 6 \end{bmatrix} , \qquad {\bf b} = \begin{bmatrix} \phantom{-}2 \\ -16 \end{bmatrix} , \qquad {\bf x}^{(0)} = \begin{bmatrix} 0 \\ 0 \end{bmatrix} . \] Use Jacobi iteration to compute x(1) and x(2).
  2. In this exercise, you will see that the order of the equations in a system can affect the performance of an iterative solution method. Upon swapping the order of equations in the previous exercise, we get \[ \begin{split} 2\,x_1 + 6\, x_2 &= -16 , \\ 5\, x_1 + x_2 &= 2 . \end{split} \] Compute five iterations with both the Jacobi and Gauss--Seidel methods using zero as an initial guess.
  3. Show that if a 2 x 2 coefficient matrix \[ {\bf A} = \begin{bmatrix} a_{1,1} & a_{1,2} \\ a_{2,1} & a_{2,2} \end{bmatrix} \] is strictly diagonally dominant, then the eigenvalues of the iteration matrices B corresponding to the Jacobi and Gauss-Seidel methods are of magnitude < 1 (which guarantees convergence of iteration methods).
  4. Apply Jacobi's iteration scheme to the system \[ \begin{split} x_1 + x_2 & = 0 , \\ x_1 - x_2 &= 2 . \end{split} \] Do you think that the iteration method converges to the true solution x₁ = 1, x₂ = −1?
  5. Apply Jacobi's iteration scheme for solving \[ \begin{bmatrix} 2 & 1 \\ 2 & 3 \end{bmatrix} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix} = \begin{pmatrix} \phantom{-}1 \\ -1 \end{pmatrix} . \] Recall that norm of the error is ∥e(k)∥ = ∥Be(k-1)∥ = |λmax| ∥e(k-1)∥, where λmax is the largest (in magnitude) eigenvalue of iteration matrix B. In general (for larger systems), this approximate relationship often is not the case for the first few iterations). Show that the error for this system tends to zero.
  6. Use the SOR method to solve the system \[ \begin{bmatrix} 2 & 1 \\ 3 & 4 \end{bmatrix} \begin{pmatrix} x_1 \\ x_2 \end{pmatrix} = \begin{pmatrix} \phantom{-}1 \\ -1 \end{pmatrix} . \]
  7. In the previous exercise, find the characteristic equation for the corresponding matrix B depending on parameter ω. What are its eigenvalues?
  8. Solve the system of equations with a direct method: \[ \begin{cases} 5 x - 2 y + 3 z &= 8, \\ 3 x + 8 y + 2 z &= 23, \\ 2 x + y + 6 z &= 2. \end{cases} \] In order to understand differences and relative advantages among st the three iterative methods, perform 8 iterations using Jacobi's scheme, the Gauss--Seidel method, and SOR iteration procedure.
  9. Let us consider the following system of linear equations: \[ \begin{cases} 5 x - 2 y + z &= 2, \\ 9 x -3 y + 2 z &= 7, \\ 4 x + y + z &= 9. \end{cases} \] Show that this system cannot be tackled by means of Jacobi’s nor Gauss-Seidel’s methods. On the other hand, a tuning of ω can lead to the detection of a good approximation of the solution.
  10. Consider the system of equations \[ \begin{bmatrix} 1 & -5 & 2 \\ 7 & 1 & 3 \\ 2 & 1 & 8 \end{bmatrix} \begin{pmatrix} x \\ y \\ z \end{pmatrix} = \begin{pmatrix} -17 \\ 11 \\ -9 \end{pmatrix} . \] Show that the given system is not strictly diagonally dominant. Nonetheless, if we swap the first and second equations, we obtain an equivalent system whose associated matrix is suitable for Jacobi's iteration scheme. Show this by performing 10 iterations.

 

  1. Ciarlet, P.G., Introduction to Numerical Linear Algebra and Optimization. Cambridge University Press, Cambridge, 1989.
  2. Dongarra, J.J., Duff, I.S., Sorensen, D.C., and H. van der Vorst, H., Numerical Linear Algebra for High-Performance Computers. SIAM, 1998.
  3. Gauss, Carl, (1903), Werke (in German), vol. 9, Göttingen: Köninglichen Gesellschaft der Wissenschaften. direct link
  4. Golub, Gene H.; Van Loan, Charles F. (1996), Matrix Computations (3rd ed.), Baltimore: Johns Hopkins, ISBN 978-0-8018-5414-9.
  5. Kahan, W., Gauss–Seidel methods of solving large systems of linear equations, Doctoral Thesis, University of Toronto, Toronto, Canada, 1958.
  6. Karunanithi, S., Gajalakshmi, N., Malarvizhi, M., Saileshwari, M., A Study on Comparison of Jacobi, Gauss-Seidel and SOR Methods for the Solution in System of Linear Equations, International Journal of Mathematics Trends and Technology (IJMTT) – Volume 56 Issue 4- April 2018.
  7. Kelley, C.T., Iterative Methods for Linear and Nonlinear Equations. SIAM, USA, 1995.
  8. Meurant, G., Computer solution of large linear systems. North Holland, Amsterdam, 1999.
  9. Saad, Y., Iterative Methods for Sparse Linear Systems, 2nd edition. SIAM, Philadelphia, PA, 2003.
  10. Seidel, Ludwig (1874). "Über ein Verfahren, die Gleichungen, auf welche die Methode der kleinsten Quadrate führt, sowie lineäre Gleichungen überhaupt, durch successive Annäherung aufzulösen" [On a process for solving by successive approximation the equations to which the method of least squares leads as well as linear equations generally]. Abhandlungen der Mathematisch-Physikalischen Klasse der Königlich Bayerischen Akademie der Wissenschaften (in German). 11 (3): 81–108.
  11. Strang, G., Introduction to Linear Algebra, ‎ Wellesley-Cambridge Press, 5-th edition, 2016.
  12. Strong, D.M., Iterative Methods for Solving Ax = b - Gauss--Seidel Method, MAA.
  13. Strong, D.M., Iterative Methods for Solving Ax = b - The SOR Method, MAA.