---
title: "Iterative linear solvers: Jacobi, Gauss–Seidel and SOR"
heading: Solving big sparse systems by repeated cheap sweeps
eyebrow: "A13 · Lesson 27 · Iterative linear solvers: Jacobi, Gauss–Seidel and SOR"
xp: 40
---

::: hook
A field solver on a 1000 × 1000 grid has a million unknowns. Gaussian elimination on the dense matrix would need $\frac23 \times 10^{18}$ operations and 8 TB of memory. But each equation involves only five unknowns, and an **iterative method** exploits that: it starts from a guess and repeatedly updates each unknown from its neighbors, at a cost proportional to the number of nonzeros per sweep. You did this in A9 and A12's relaxation labs. This lesson explains when such sweeps converge, why plain relaxation is so slow, and how one parameter, the over-relaxation factor ω, can make it a hundred times faster.
:::

::: play
- The curve shows how many sweeps SOR needs to cut the error by $10^6$ on the model problem (Laplace's equation on an N × N grid), against ω.
- ω = 1 is Gauss–Seidel. Raise ω and the count drops steeply to a sharp minimum at $\omega_{opt} = \frac{2}{1 + \sin(\pi/N)}$, then rises again; past 2 it diverges.
- Raise N: the minimum moves toward 2 and grows only in proportion to N, while Gauss–Seidel's count grows as $N^2$.
:::
:::

```sim plot
view: graph
x: w = 1..1.995
y: sweeps = 10..1000000 log
slider: N = 10..200 step 10 = 50
plot: 6*ln(10)/(-ln(step(2/(1 + sin(pi/N)) - w)*((w*cos(pi/N) + sqrt(abs(w^2*cos(pi/N)^2 - 4*(w - 1))))/2)^2 + step(w - 2/(1 + sin(pi/N)))*(w - 1))) | samples: 1200 | label: SOR sweeps for 10⁶
marker: w = 2/(1 + sin(pi/N)) | label: ω_opt
readout: Jacobi sweeps = 6*ln(10)/(-ln(cos(pi/N)))
readout: Gauss–Seidel sweeps = 6*ln(10)/(-2*ln(cos(pi/N)))
readout: SOR sweeps at ω_opt = 6*ln(10)/(-ln(2/(1 + sin(pi/N)) - 1))
```

::: predict
## On a 100 × 100 grid, Gauss–Seidel needs about 14 000 sweeps to gain 6 digits. About how many does SOR need at the best ω?

- [ ] 7000
- [ ] 2000
- [x] 220
- [ ] 6

::: reveal
About 220. With $\omega_{opt} = 1.939$, the error shrinks by $\omega - 1 = 0.939$ per sweep, against Gauss–Seidel's 0.999. Each sweep costs the same, so the right ω is a 64-fold speedup for free.
:::
:::

::: nameit
## Splittings

Write $A = M - N$ with M easy to solve, and iterate

$$Mx^{(k+1)} = Nx^{(k)} + b$$

The error is multiplied by $G = M^{-1}N$ each step, so the method converges from every start exactly when the **spectral radius** $\rho(G)$, its largest eigenvalue in size, is below 1. Each sweep gains $\log_{10}(1/\rho)$ digits.

With $A = D + L + U$ (diagonal, strictly lower, strictly upper parts):

| Method | M | Update |
|---|---|---|
| Jacobi | D | every $x_i$ from the old values |
| Gauss–Seidel | D + L | each $x_i$ uses the newest values as they're computed |
| SOR | $\frac1\omega D + L$ | Gauss–Seidel's change, multiplied by ω |

## When they converge

If A is **strictly diagonally dominant** ($\lvert a_{ii}\rvert > \sum_{j\ne i}\lvert a_{ij}\rvert$ in every row), Jacobi and Gauss–Seidel converge. If A is symmetric positive definite, Gauss–Seidel converges, and SOR does for every $0 < \omega < 2$. Nodal matrices of resistor networks are both.

## The model problem

For the five-point Laplacian on an N × N grid, $\rho_J = \cos\frac\pi N \approx 1 - \frac{\pi^2}{2N^2}$, Gauss–Seidel has $\rho_J^2$, and SOR at

$$\omega_{opt} = \frac{2}{1 + \sin(\pi/N)}$$

has $\rho = \omega_{opt} - 1 \approx 1 - \frac{2\pi}{N}$. Sweeps needed: about $N^2$ for Jacobi and Gauss–Seidel, about N for optimal SOR. Multigrid (A12's relaxation done on a hierarchy of grids) gets it down to a fixed number, independent of N.

## Stopping

Stop when the residual $\lVert b - Ax\rVert$ is small relative to $\lVert b\rVert$. With ρ near 1, a small change between sweeps doesn't mean a small error: the remaining error is about $\frac{\rho}{1 - \rho}$ times the last change.
:::

::: example
## Do two Jacobi sweeps and one Gauss–Seidel sweep on $4x_1 - x_2 = 3$, $-x_1 + 4x_2 - x_3 = 2$, $-x_2 + 4x_3 = 3$ from zero. (The solution is 1, 1, 1.)

::: solution
1. **First Jacobi sweep.** From zero each equation gives its unknown directly:
   $$x = (0.75, 0.5, 0.75)$$
2. **Second Jacobi sweep.**
   $$\begin{aligned} x_1 &= \frac{3 + 0.5}{4} \\ &= 0.875 \\ x_2 &= \frac{2 + 0.75 + 0.75}{4} \\ &= 0.875 \\ x_3 &= \frac{3 + 0.5}{4} \\ &= 0.875 \end{aligned}$$
3. **One Gauss–Seidel sweep.** It uses each new value at once:
   $$\begin{aligned} x_1 &= 0.75 \\ x_2 &= \frac{2 + 0.75}{4} \\ &= 0.6875 \\ x_3 &= \frac{3 + 0.6875}{4} \\ &= 0.9219 \end{aligned}$$

**Answer:** Jacobi gives (0.75, 0.5, 0.75) and then (0.875, 0.875, 0.875); one Gauss–Seidel sweep gives (0.75, 0.6875, 0.9219).
:::
:::

::: example
## How many sweeps does each method need on a 100 × 100 grid to gain 6 digits?

::: solution
1. **Jacobi's rate.**
   $$\begin{aligned} \rho_J &= \cos(\pi/100) \\ &= 0.999507 \end{aligned}$$
2. **Jacobi's sweeps.**
   $$\frac{6\ln 10}{-\ln 0.999507} = 28\,000$$
3. **Gauss–Seidel.** It needs half that, 14 000.
4. **SOR.** With $\omega_{opt} = 1.9391$ it has ρ = 0.9391 and needs 220.

**Answer:** about 28 000 sweeps for Jacobi, 14 000 for Gauss–Seidel and 220 for SOR.
:::
:::

::: example
## For $\begin{pmatrix}4 & 1\\ 2 & 5\end{pmatrix}x = b$, how fast do Jacobi and Gauss–Seidel converge?

::: solution
1. **Jacobi's iteration matrix.**
   $$\begin{aligned} &-D^{-1}(L + U) \\ &= \begin{pmatrix}0 & -1/4\\ -2/5 & 0\end{pmatrix} \end{aligned}$$
2. **Its eigenvalues.**
   $$\pm\sqrt{\frac14\cdot\frac25} = \pm0.316$$
   So each sweep shrinks the error by 0.316, about half a digit.
3. **Gauss–Seidel.** For a 2 × 2 matrix like this one, its spectral radius is the square of Jacobi's, 0.1: a full digit per sweep.
4. **The general pattern.** The same relation holds for the model problems of this lesson, which is why Gauss–Seidel needs half as many sweeps.

**Answer:** Jacobi shrinks the error by 0.316 per sweep and Gauss–Seidel by 0.1.
:::
:::

::: check
given: N = 20 | 50 | 200
answer: 2/(1 + sin(pi/N))
tolerance: 0.01%

## What's the optimal SOR factor for the model problem on a {N} × {N} grid?

::: right
Right. $\frac{2}{1 + \sin(\pi/N)}$ = {answer}.
:::

::: misses
- [2/(1 + cos(pi/N))] It uses $\sin(\pi/N)$, which is small; ω must come out close to 2.
- [any] Not yet. $\omega_{opt} = \frac{2}{1 + \sin(\pi/N)}$.
:::
:::

::: check
given: N = 20 | 50 | 200; d = 6 | 10
answer: d*ln(10)/(-ln(cos(pi/N)))
tolerance: 1%

## About how many Jacobi sweeps does the model problem on a {N} × {N} grid need to gain {d} digits?

::: right
Right. $\frac{d\ln 10}{-\ln\cos(\pi/N)}$ = {answer}.
:::

::: misses
- [d*ln(10)/(-2*ln(cos(pi/N)))] That's Gauss–Seidel, which is twice as fast.
- [any] Not yet. Each sweep multiplies the error by $\rho_J = \cos(\pi/N)$.
:::
:::

::: check
given: N = 20 | 50 | 200; d = 6 | 10
answer: d*ln(10)/(-ln(2/(1 + sin(pi/N)) - 1))
tolerance: 1%

## And how many SOR sweeps at $\omega_{opt}$?

::: right
Right. $\frac{d\ln 10}{-\ln(\omega_{opt} - 1)}$ = {answer}.
:::

::: misses
- [any] Not yet. At $\omega_{opt}$ the spectral radius is $\omega_{opt} - 1$.
:::
:::

::: check
given: r = 0.6 | 0.9 | 0.99
answer: r^2
tolerance: 0.5%

## For the model problem Jacobi has $\rho_J$ = {r}. What's Gauss–Seidel's spectral radius?

::: right
Right. $\rho_J^2$ = {answer}: twice the digits per sweep.
:::

::: misses
- [r/2] The radius is squared, not halved.
- [any] Not yet. For this matrix class, $\rho_{GS} = \rho_J^2$.
:::
:::

::: check
given: a = 4 | 5 | 10; b1 = 6 | 9; b2 = 3 | 8
answer: (b1 - 1)/a
tolerance: 0.1%

## One Jacobi sweep on $\given{a}x_1 + x_2 = \given{b1}$, $x_1 + \given{a}x_2 = \given{b2}$ from $x = (1, 1)$: what's the new $x_1$?

::: right
Right. $(b_1 - x_2)/a$ = {answer}.
:::

::: misses
- [b1/a] Use the old $x_2 = 1$, not 0.
- [any] Not yet. Solve the first equation for $x_1$ with the old $x_2$.
:::
:::

::: check
kind: choice

## Which of these matrices is guaranteed to make Jacobi converge?

- [ ] $\begin{pmatrix}1 & 2\\ 2 & 1\end{pmatrix}$
- [x] $\begin{pmatrix}5 & -2\\ 1 & 4\end{pmatrix}$
- [ ] $\begin{pmatrix}1 & 1\\ 1 & 1\end{pmatrix}$
- [ ] $\begin{pmatrix}0 & 1\\ 1 & 0\end{pmatrix}$

::: misses
- [1] Each diagonal entry is smaller than the off-diagonal one: not dominant, and Jacobi diverges here.
- [3] Singular: there's no unique solution to converge to.
- [4] A zero diagonal means Jacobi can't even start.
:::
:::

::: check
kind: parts
given: a = 2 | 4; b = 3 | 5

## Solve $\begin{pmatrix}\given{a} & 1\\ 1 & \given{b}\end{pmatrix}x = c$ iteratively.

::: part
answer: 1/sqrt(a*b)
tolerance: 0.5%

### What is Jacobi's spectral radius?

::: misses
- [1/(a*b)] That's Gauss–Seidel's.
- [any] The eigenvalues of $\begin{pmatrix}0 & -1/a\\ -1/b & 0\end{pmatrix}$.
:::
:::

::: part
answer: 1/(a*b)
tolerance: 0.5%

### What is Gauss–Seidel's?

::: misses
- [any] The square of Jacobi's, here.
:::
:::
:::

::: check
kind: derivation
vars: r = 0.5..0.99

## For the model problem, Jacobi's spectral radius is r.

::: step
answer: 2/(1 + sqrt(1 - r^2))

### What is the optimal SOR factor?
:::

::: step
answer: 2/(1 + sqrt(1 - r^2)) - 1

### What is SOR's spectral radius at that factor, $\omega - 1$?
:::

::: right
Right. As r approaches 1, $\omega - 1$ falls much more slowly than r does, which is SOR's big gain.
:::
:::

::: explain
## Why does over-relaxation (ω > 1) speed up Gauss–Seidel so much on smooth problems?

::: model
On problems like Laplace's equation, the error that survives longest is smooth: it varies slowly across the grid. Each Gauss–Seidel update moves a value toward the average of its neighbors, but for a smooth error the neighbors are almost as wrong as the point itself, so each sweep changes things only slightly, always in the same direction. Over-relaxation multiplies each change by ω, pushing past the Gauss–Seidel value in the direction it was already heading, which is the right direction for smooth errors. At the optimal ω the slowest error shrinks by about $1 - 2\pi/N$ per sweep instead of $1 - \pi^2/N^2$, turning a cost proportional to $N^2$ into one proportional to N. Too large an ω overshoots and the iteration oscillates, and ω ≥ 2 diverges.
:::
:::

::: practice

::: check
given: N = 16 | 32 | 64 | 100 | 256 | 500
answer: 2/(1 + sin(pi/N))
tolerance: 0.01%

## $\omega_{opt}$ for N = {N}?

::: misses
- [any] $\frac{2}{1 + \sin(\pi/N)}$.
:::
:::

::: check
given: N = 30 | 300; d = 8 | 12
answer: d*ln(10)/(-ln(cos(pi/N)))
tolerance: 1%

## Jacobi sweeps, N = {N}, {d} digits?

::: misses
- [any] $\frac{d\ln 10}{-\ln\cos(\pi/N)}$.
:::
:::

::: check
given: r = 0.5 | 0.7 | 0.8 | 0.9 | 0.95 | 0.99
answer: r^2
tolerance: 0.5%

## Gauss–Seidel radius from $\rho_J$ = {r}?

::: misses
- [any] $\rho_J^2$.
:::
:::
:::
