TExcelTAD
Home › Numerical Methods › Jacobi and Gauss-Seidel

Jacobi and Gauss-Seidel Iterations

For very large sparse systems, direct factorization becomes too expensive in memory and time. Iterative methods start from a guess and improve it using only cheap operations with AA. The oldest are the Jacobi and Gauss-Seidel methods: solve each equation for its own unknown, using the current values of the others. They are slow on their own, but they are the smoothers inside multigrid and a good introduction to convergence of iterations.

The methods

Split A=D+L+UA = D + L + U into diagonal, strictly lower and strictly upper parts.

  • Jacobi: xi(k+1)=1aii(bi−∑j≠iaijxj(k))x_i^{(k+1)} = \frac{1}{a_{ii}}\left(b_i - \sum_{j \ne i}a_{ij}x_j^{(k)}\right), i.e. x(k+1)=D−1(b−(L+U)x(k))\mathbf{x}^{(k+1)} = D^{-1}\left(\mathbf{b} - (L + U)\mathbf{x}^{(k)}\right). All updates use old values, so they can be done in parallel.
  • Gauss-Seidel: use each new value as soon as it is computed: (D+L)x(k+1)=b−Ux(k)(D + L)\mathbf{x}^{(k+1)} = \mathbf{b} - U\mathbf{x}^{(k)}.
  • SOR (successive over-relaxation): move further in the Gauss-Seidel direction by a factor ω∈(1,2)\omega \in (1, 2).

Convergence

Both are splitting methods Mx(k+1)=Nx(k)+bM\mathbf{x}^{(k+1)} = N\mathbf{x}^{(k)} + \mathbf{b} with A=M−NA = M - N. The error obeys e(k+1)=M−1Ne(k)\mathbf{e}^{(k+1)} = M^{-1}N\mathbf{e}^{(k)}, so the method converges for every start exactly when the spectral radius ρ(M−1N)<1\rho(M^{-1}N) < 1, and the error shrinks by about ρ\rho per iteration (Fixed-Point Iteration). Sufficient conditions: AA strictly diagonally dominant (both methods), or AA symmetric positive definite (Gauss-Seidel and SOR for 0<ω<20 < \omega < 2).

python · try it yourself
import numpy as np

def jacobi(A, b, x, iters):
    D = np.diag(A); R = A - np.diag(D)
    for _ in range(iters):
        x = (b - R @ x) / D
    return x

def gauss_seidel(A, b, x, iters, omega=1.0):
    x = x.copy(); n = len(b)
    for _ in range(iters):
        for i in range(n):
            gs = (b[i] - A[i, :i] @ x[:i] - A[i, i + 1:] @ x[i + 1:]) / A[i, i]
            x[i] = (1 - omega) * x[i] + omega * gs
    return x

n = 30
A = 2 * np.eye(n) - np.eye(n, k=1) - np.eye(n, k=-1)          # 1D Laplacian
x_true = np.sin(np.linspace(0, 3, n))
b = A @ x_true
x0 = np.zeros(n)
for iters in (100, 400, 1600):
    ej = np.linalg.norm(jacobi(A, b, x0, iters) - x_true)
    eg = np.linalg.norm(gauss_seidel(A, b, x0, iters) - x_true)
    es = np.linalg.norm(gauss_seidel(A, b, x0, iters, omega=1.81) - x_true)
    print(f"{iters:4d} iterations: Jacobi {ej:.2e}, Gauss-Seidel {eg:.2e}, SOR(1.81) {es:.2e}")
D = np.diag(np.diag(A)); L = np.tril(A, -1)
rho_j = max(abs(np.linalg.eigvals(np.linalg.solve(D, D - A))))
rho_gs = max(abs(np.linalg.eigvals(np.linalg.solve(D + L, -(A - D - L)))))
print(f"spectral radii: Jacobi {rho_j:.5f}, Gauss-Seidel {rho_gs:.5f} (= Jacobi squared {rho_j**2:.5f})")
 100 iterations: Jacobi 2.32e+00, Gauss-Seidel 1.41e+00, SOR(1.81) 2.66e-06
 400 iterations: Jacobi 4.97e-01, Gauss-Seidel 6.42e-02, SOR(1.81) 5.46e-15
1600 iterations: Jacobi 1.04e-03, Gauss-Seidel 2.79e-07, SOR(1.81) 3.24e-15
spectral radii: Jacobi 0.99487, Gauss-Seidel 0.98976 (= Jacobi squared 0.98976)

Why they are slow for PDEs

For the Laplacian on a grid with spacing hh, ρJacobi=cos⁡(πh)≈1−π2h22\rho_{\text{Jacobi}} = \cos(\pi h) \approx 1 - \frac{\pi^2h^2}{2}. Reducing the error by a fixed factor takes O(h−2)O(h^{-2}) iterations, which grows rapidly as the grid is refined. Gauss-Seidel is twice as fast; optimal SOR needs only O(h−1)O(h^{-1}) iterations.

Yet these methods remove high-frequency (oscillatory) error components very quickly; only the smooth components are slow. Multigrid exploits this: a few Gauss-Seidel sweeps smooth the error, which is then corrected on a coarser grid where it is no longer smooth. The result is an O(n)O(n) solver.

python · try it yourself
import numpy as np

n = 63
A = 2 * np.eye(n) - np.eye(n, k=1) - np.eye(n, k=-1)
x = np.arange(1, n + 1) / (n + 1)
for k in (1, 31):                               # smooth mode and oscillatory mode
    e = np.sin(k * np.pi * x)                   # error vector (solve A e = 0 from this start)
    D = np.diag(A); R = A - np.diag(D)
    for _ in range(10):
        e = (0 - R @ e) / D * 2 / 3 + e / 3     # weighted Jacobi (omega = 2/3), a classic smoother
    print(f"mode k = {k:2d}: error reduced to {np.linalg.norm(e) / np.linalg.norm(np.sin(k * np.pi * x)):.3f} of its size after 10 sweeps")
mode k =  1: error reduced to 0.992 of its size after 10 sweeps
mode k = 31: error reduced to 0.000 of its size after 10 sweeps

In software

  • Plain Jacobi and Gauss-Seidel are rarely used as solvers; they appear as smoothers in multigrid (PyAMG, hypre) and as simple preconditioners (Preconditioning and GMRES).
  • Jacobi iterations parallelize perfectly and are used on GPUs and in distributed settings; asynchronous variants tolerate stale data.

Exercises

Exercise 1: One Jacobi step by hand

For (4125)x=(913)\begin{pmatrix} 4 & 1 \\ 2 & 5\end{pmatrix}\mathbf{x} = \begin{pmatrix} 9 \\ 13\end{pmatrix} from x=(0,0)\mathbf{x} = (0, 0), take one Jacobi step and one Gauss-Seidel step.

Answer

Jacobi (2.25,2.6)(2.25, 2.6); Gauss-Seidel (2.25,1.7)(2.25, 1.7). (Exact (2,1)(2, 1).)

Solution

Jacobi: x1=9/4x_1 = 9/4, x2=13/5x_2 = 13/5. Gauss-Seidel uses the new x1x_1: x2=(13−2⋅2.25)/5=1.7x_2 = (13 - 2 \cdot 2.25)/5 = 1.7.

Exercise 2: Iterations needed

If ρ=0.999\rho = 0.999, how many iterations reduce the error by 10−610^{-6}?

Answer

About 13,800.

Solution

k=ln⁡10−6ln⁡0.999≈13,809k = \frac{\ln 10^{-6}}{\ln 0.999} \approx 13{,}809.

Exercise 3: Diagonal dominance (code)

For a random 50×5050 \times 50 matrix with diagonal 6060 and off-diagonal entries uniform in [−1,1][-1, 1] (seed 102), compute the Jacobi spectral radius and run 30 Jacobi iterations.

Answer

ρ\rho is well below 1 (strict diagonal dominance), and the residual falls to about rounding level.

Solution
python · try it yourself
import numpy as np
rng = np.random.default_rng(102)
A = rng.uniform(-1, 1, (50, 50)); np.fill_diagonal(A, 60.0)
b = rng.normal(size=50)
D = np.diag(A); R = A - np.diag(D)
print(f"rho = {max(abs(np.linalg.eigvals(R / D[:, None]))):.4f}")
x = np.zeros(50)
for _ in range(30):
    x = (b - R @ x) / D
print(f"residual {np.linalg.norm(b - A @ x):.1e}")
rho = 0.0732
residual 1.5e-15

Test yourself

A stationary iteration converges for every starting vector exactly when…

  1. AA is symmetric
  2. the spectral radius of the iteration matrix is below 1
  3. det⁡A≠0\det A \ne 0
  4. the diagonal is positive

Gauss-Seidel differs from Jacobi by…

  1. using new values as soon as they are computed
  2. using the transpose
  3. being a direct method
  4. needing no diagonal

Jacobi and Gauss-Seidel are effective multigrid smoothers because they…

  1. solve exactly
  2. quickly damp oscillatory error components
  3. remove smooth errors fast
  4. need no matrix