Numerical Methods · Learn

Gauss–Seidel & Jacobi

How to solve a big linear system without ever inverting the matrix: guess, sweep through the equations, repeat. Why it works, when it's guaranteed to work, and why swapping two rows can turn divergence into convergence.

▶ Play the lab 📖 Learn the theory

1The problem

Solve $A\mathbf{x}=\mathbf{b}$ for $n$ unknowns. Gaussian elimination does it directly in about $n^3/3$ operations. That's fine for $n=10$ and hopeless for $n=40{,}000$, the size of a modest 200 × 200 finite-difference grid. Elimination also fills in zeros as it goes, so memory explodes too.

But engineering matrices are sparse. A node on a heat-conduction grid talks only to its four neighbours, so each row has five nonzeros out of 40,000. Iterative methods exploit that: each sweep touches only the nonzeros, costing about as much as one matrix–vector product, and you stop as soon as the answer is accurate enough.

2Jacobi: everyone updates at once

Solve equation $i$ for unknown $i$, using the previous sweep's values for everything else:

$$ x_i^{(k+1)} = \frac{1}{a_{ii}}\Big(b_i - \sum_{j\ne i} a_{ij}\,x_j^{(k)}\Big) $$

Split the matrix into its diagonal, strictly lower and strictly upper parts, $A = D + L + U$. Then a whole Jacobi sweep is

$$ \mathbf{x}^{(k+1)} = D^{-1}\big(\mathbf{b} - (L+U)\,\mathbf{x}^{(k)}\big) $$

Every unknown updates from the same old vector, so the updates are independent of one another. That makes Jacobi trivially parallel, which is why it's still used on GPUs.

3Gauss–Seidel: use the news immediately

Same formula, one change: as soon as $x_1^{(k+1)}$ is computed, use it when updating $x_2$, and so on down the list.

$$ x_i^{(k+1)} = \frac{1}{a_{ii}}\Big(b_i - \sum_{ji} a_{ij}\,x_j^{(k)}\Big) \qquad\Longleftrightarrow\qquad (D+L)\,\mathbf{x}^{(k+1)} = \mathbf{b} - U\,\mathbf{x}^{(k)} $$

It needs only one copy of $\mathbf{x}$ in memory, and it usually converges faster because each update already reflects part of the current sweep.

4SOR: deliberately overshoot

Gauss–Seidel's corrections tend to point the right way but fall short. Successive over-relaxation stretches each one by a factor $\omega$:

$$ x_i \leftarrow (1-\omega)\,x_i + \omega\,x_i^{\text{GS}} $$

$\omega = 1$ is plain Gauss–Seidel, $\omega>1$ over-relaxes, and $\omega<1$ under-relaxes, which helps tame a stubborn iteration. SOR can only converge for $0<\omega<2$. For grid problems the best $\omega$ sits close to 2, and getting it right can cut the number of sweeps from thousands to tens.

5When does it converge?

Each method has the form $\mathbf{x}^{(k+1)} = G\,\mathbf{x}^{(k)} + \mathbf{c}$, so the error obeys $\mathbf{e}^{(k+1)} = G\,\mathbf{e}^{(k)}$. The iteration matrices are

$$ G_{\text{J}} = -D^{-1}(L+U), \qquad G_{\text{GS}} = -(D+L)^{-1}U $$

The iteration converges from every starting guess exactly when the spectral radius, the largest eigenvalue magnitude, is below one:

$$ \text{converges for every } \mathbf{x}^{(0)} \iff \rho(G) < 1 $$

That's the true criterion, but eigenvalues are hard to see by eye. These conditions are easy to check, and each one is sufficient:

If A is…then…typical source
strictly diagonally dominant, $|a_{ii}| > \sum_{j\ne i}|a_{ij}|$ in every rowJacobi and Gauss–Seidel both convergeheat balances, circuits
weakly dominant ($\ge$) in every row, strict in at least one, and irreducible (no part of the system is cut off from the rest)Jacobi and Gauss–Seidel both convergefinite-difference grids with a fixed-temperature boundary
symmetric positive definiteGauss–Seidel converges, and SOR too for any $0<\omega<2$energy methods, structures

The middle row matters more than it looks. An interior node of the 5-point Laplacian has $|a_{ii}| = 4$ and four neighbours of $-1$. That's dominance with equality, not strict. Weak dominance alone isn't enough: a fully insulated plate is singular and has no unique answer. The fixed-temperature edges supply the strict rows that make the guarantee work.

▶ Play the game

Dominance depends on which equation sits in which row. Shuffle the rows and a dominant system stops being dominant, and Gauss–Seidel can blow up. Swapping them back fixes it without changing the solution. Play Make It Converge →

6How fast?

Each sweep multiplies the error by roughly $\rho(G)$, so reaching a tolerance $\varepsilon$ takes about

$$ k \approx \frac{\ln \varepsilon}{\ln \rho(G)} \text{ sweeps.} $$

When $\rho$ is close to 1, as it is on fine grids, $\ln\rho$ is tiny and the sweep count balloons. For a large class of matrices, including tridiagonal systems and the standard finite-difference Laplacian, $\rho(G_{\text{GS}}) = \rho(G_{\text{J}})^2$. Gauss–Seidel then needs half as many sweeps as Jacobi, and optimal SOR does dramatically better than both.

MethodUsesMemorySpeedWatch out for
Jacobilast sweep onlytwo vectorsslowestslow when $\rho$ is near 1
Gauss–Seidelnewest valuesone vectoroften ~2× Jacobisequential, depends on equation order
SORGS plus overshoot $\omega$one vectormuch faster at a good $\omega$a bad $\omega$ is slower, or diverges outside $(0,2)$

Key takeaways

Where these systems come from: every sweep of the 2D conduction lab is SOR on a finite-difference grid. The irregular-conduit lab builds its matrix automatically from the mesh, and the Ritz–Galerkin method produces small dense systems that are simplest to solve directly.

EngineeringCandy · Learn · the theory behind the playground