---
title: Preconditioning and multigrid
heading: Why simple iterations stall on smooth errors, and how coarse grids fix it
eyebrow: A13 · Lesson 29 · Multigrid
xp: 40
---

::: hook
Lessons 27 and 28 showed that Jacobi and conjugate gradients slow down as a grid is refined: the model problem needs millions of Jacobi sweeps or thousands of CG steps on a large grid. The reason is specific. Simple iterations remove rough, rapidly varying error almost at once, but smooth error barely moves, because each update only looks at its neighbors. **Multigrid** turns that weakness into a method: smooth error looks rough on a coarser grid, so solve for it there. The result solves the model problem in a fixed number of cycles whatever the grid size, with work proportional to the number of unknowns. The same idea, used as a **preconditioner**, makes CG converge in a handful of steps.
:::

::: play
- The curve shows how much one sweep of weighted Jacobi multiplies each Fourier mode of the error, against the mode's frequency θ (0 is smoothest, π roughest), for the 1D model problem.
- With ω = 1 (plain Jacobi), the roughest mode isn't damped at all: its factor is −1.
- With $\omega = \frac23$, every mode from θ = π/2 to π shrinks by at least a factor of 3 per sweep. The smooth modes near θ = 0 still barely shrink: that's the job left for the coarse grid.
:::

```sim plot
view: graph
x: theta = 0..3.1416
y: factor = -1.05..1.05
slider: w = 0.3..1 step 0.01 = 0.67
plot: 1 - 2*w*sin(theta/2)^2 | label: damping factor per sweep | samples: 300
plot: 0 | dashed
readout: factor at half the highest frequency = 1 - w | digits: 3
readout: factor at the highest frequency = 1 - 2*w | digits: 3
```

::: predict
## A 2D model problem has a million unknowns (1000 × 1000). Roughly how many multigrid V-cycles gain 6 digits?

- [ ] About 1000
- [ ] About 100
- [x] About 6 to 10
- [ ] About 1 million

::: reveal
About 6 to 10. A good V-cycle cuts the error by a factor of about 10 regardless of the grid size, and each cycle costs only a few sweeps' worth of work. On the same problem, Jacobi would need about 2.8 million sweeps and CG about 4600 steps.
:::
:::

::: nameit
## Smoothing

For the 1D model problem, weighted Jacobi multiplies the error mode with frequency θ by

$$g(\theta) = 1 - 2\omega\sin^2\frac\theta2.$$

The smooth modes, θ near 0, have $g \approx 1 - \frac{\omega\theta^2}{2}$, barely below 1. For the rough half, $\frac\pi2 \le \theta \le \pi$, choosing $\omega = \frac23$ makes $\lvert g\rvert \le \frac13$: a few sweeps make the error smooth, though not small. Gauss–Seidel smooths similarly.

## The coarse-grid correction

Once the error e is smooth, it can be represented on a grid with twice the spacing. Compute the residual $r = b - Au$, **restrict** it to the coarse grid, solve $A_ce_c = r_c$ there (where the problem is 2, 4 or 8 times smaller), **interpolate** $e_c$ back and add it to u. Modes that were smooth on the fine grid are only half as smooth on the coarse one, so the coarse grid's own smoothing handles them.

## The V-cycle

Apply the idea recursively: smooth, restrict, recurse down to a tiny grid solved directly, then interpolate and smooth on the way back up. The work is a geometric series. In d dimensions each coarser grid has $2^{-d}$ as many points, so a V-cycle costs about $\frac{1}{1 - 2^{-d}}$ times the work on the finest grid: 2 in 1D, $\frac43$ in 2D, $\frac87$ in 3D. Each cycle cuts the error by a fixed factor, typically 0.1, so the total work is O(n).

## Preconditioning

CG's steps grow as $\sqrt\kappa$. A **preconditioner** M is an approximation to A that's cheap to solve with; CG applied to $M^{-1}A$ converges at a rate set by $\kappa(M^{-1}A)$. Common choices: the diagonal (Jacobi), incomplete Cholesky (Cholesky that drops fill), and one multigrid V-cycle, which makes $\kappa(M^{-1}A)$ bounded independent of the grid.
:::

::: example
## For weighted Jacobi with $\omega = \frac23$ on the 1D model problem with h = 1/64, how fast are the roughest, the middle and the smoothest modes damped?

::: solution
1. **The roughest mode, θ = π.**
   $$\begin{aligned} g &= 1 - \frac43 \\ &= -\frac13 \end{aligned}$$
2. **The middle mode, θ = π/2.**
   $$\begin{aligned} g &= 1 - \frac43 \cdot \frac12 \\ &= \frac13 \end{aligned}$$
3. **The smoothest mode, θ = πh.**
   $$\begin{aligned} g &= 1 - \frac43\sin^2\frac{\pi}{128} \\ &= 0.9992 \end{aligned}$$
4. **What that means.** Three sweeps cut every rough mode by at least 27, while the smoothest mode needs about 1250 sweeps just to fall by a factor of e.
5. **How multigrid uses it.** That's the signature of every simple iteration on elliptic problems, and it's exactly what multigrid exploits: three sweeps, then hand the smooth remainder to the coarse grid.

**Answer:** each sweep multiplies the roughest mode by $-\frac13$, the middle mode by $\frac13$ and the smoothest by 0.9992.
:::
:::

::: example
## How much work does one V-cycle take on a 2D grid with $1024 \times 1024$ unknowns, with two smoothing sweeps on each level going down and coming up?

::: solution
1. **The levels.** They have $1024^2$, $512^2$, $256^2$, … points, a quarter as many each time, down to a tiny grid.
2. **Sweeps on all levels.** Four sweeps per level (two down, two up) cost
   $$\begin{aligned} &4\left(1 + \frac14 + \frac1{16} + \cdots\right) \\ &= 4 \times \frac43 \\ &= 5.3 \end{aligned}$$
   fine-grid sweeps, plus residual and transfer work of the same order.
3. **The whole cycle.** So a V-cycle costs about as much as 8 to 10 Jacobi sweeps on the fine grid, and gains about a digit.
4. **Compare with Jacobi.** Six digits take some 60 sweep-equivalents, against the 2.9 million sweeps plain Jacobi would need on a grid this size.

**Answer:** about 5.3 fine-grid sweeps plus transfers, roughly the cost of 8 to 10 Jacobi sweeps.
:::
:::

::: example
## Compare the number of iterations Jacobi, CG and multigrid need for 6 digits on the 2D model problem with N = 1000 points per side.

::: solution
1. **Jacobi.** $\rho = \cos\pi h$ with $h = \frac{1}{1001}$, so the count is
   $$\frac{\ln 10^{-6}}{\ln\rho} \approx 2.8 \times 10^6$$
   sweeps.
2. **CG's condition number.**
   $$\kappa \approx \frac{4(N + 1)^2}{\pi^2}$$
   so $\sqrt\kappa = 637$.
3. **CG's steps.**
   $$\frac{\sqrt\kappa}{2}\ln\frac{2}{10^{-6}} \approx 4600$$
4. **Multigrid.** About 6 V-cycles.
5. **Compare the work.** Each step of each method costs O(n) work, so the counts compare total work directly, and only multigrid's count doesn't grow when N does.

**Answer:** about $2.8 \times 10^6$ Jacobi sweeps, 4600 CG steps and 6 multigrid V-cycles.
:::
:::

::: check
kind: parts
given: w = 0.5 | 0.67 | 1; t = 0.5 | 1

## Weighted Jacobi on the 1D model problem with ω = {w}.

::: part
answer: 1 - 2*w*sin(t*pi/2)^2
tolerance: 0.5%

### What does one sweep multiply the mode with θ = {t}π by?

::: misses
- [1 - w*sin(t*pi/2)^2] The factor is $1 - 2\omega\sin^2\frac\theta2$.
- [any] $g(\theta) = 1 - 2\omega\sin^2\frac\theta2$.
:::
:::
:::

::: check
given: d = 1 | 2 | 3
answer: 1/(1 - 2^(-d))
tolerance: 0.5%

## In d = {d} dimensions, each coarser grid has $2^{-d}$ as many points. How many fine-grid sweeps' worth of smoothing does a V-cycle with one sweep per level cost?

::: right
Right. $1 + 2^{-d} + 4^{-d} + \cdots = \frac{1}{1 - 2^{-d}}$.
:::

::: misses
- [d] The levels form a geometric series that converges quickly.
- [any] Sum the geometric series of level sizes.
:::
:::

::: check
given: r = 0.1 | 0.2 | 0.05; d = 6 | 10
answer: ceil(d/(-log(r)))
tolerance: 0

## A V-cycle cuts the error by {r}. How many cycles gain {d} digits?

::: right
Right. Each cycle gains $-\log_{10}r$ digits, independent of the grid size.
:::

::: misses
- [any] Divide the digits needed by the digits per cycle and round up.
:::
:::

::: check
given: K = 10000 | 1000000; k = 10 | 100
answer: sqrt(K/k)
tolerance: 0.5%

## A preconditioner lowers the condition number from {K} to {k}. By about what factor does it cut CG's step count?

::: right
Right. CG's steps go as $\sqrt\kappa$.
:::

::: misses
- [K/k] Steps scale with the square root of κ.
- [any] $\sqrt{K/k}$.
:::
:::

::: check
given: N = 256 | 1024 | 4096
answer: log(N/4)/log(2) + 1
tolerance: 0.5%

## A multigrid hierarchy halves an $N \times N$ grid until it's 4 × 4. With N = {N}, how many levels are there, counting both ends?

::: right
Right. $\log_2\frac N4$ halvings, plus the finest level.
:::

::: misses
- [log(N/4)/log(2)] Count the finest grid too.
- [any] Halve repeatedly from N to 4.
:::
:::

::: check
kind: choice

## Why do Jacobi and Gauss–Seidel need so many sweeps on fine grids?

- [ ] Rounding errors build up on large grids
- [x] Each update looks only at neighbors, so smooth error components shrink very slowly
- [ ] They diverge for large matrices
- [ ] The matrices become indefinite as h shrinks

::: right
Right. Information spreads one grid point per sweep; a smooth error spanning N points needs on the order of $N^2$ sweeps to diffuse away.
:::

::: misses
- [1] The slowdown happens in exact arithmetic too.
- [3] They still converge; just slowly.
- [4] The model problem's matrix stays symmetric positive definite.
:::
:::

::: check
kind: derivation
vars: w = 0.3..1, t = 0..3.1

## Weighted Jacobi on the 1D model problem, with θ written as t and ω as w.

::: step
answer: 1 - 2*w*sin(t/2)^2

### What factor multiplies the error mode with frequency θ?
:::

::: step
answer: 1 - 2*w

### What is it for the roughest mode, θ = π?
:::

::: right
Right. Plain Jacobi (ω = 1) gives −1 there, no damping at all, which is why multigrid smoothers are underrelaxed.
:::
:::

::: check
kind: python

## On the 1D model problem with N = 31 interior points, start with the error $e = \sin\pi x + \sin 31\pi x$ and apply 3 sweeps of weighted Jacobi with $\omega = \frac23$ to $Ae = 0$: $e \leftarrow e + \frac\omega2(e_{i+1} - 2e_i + e_{i-1})$, with zero ends. Set `smooth` and `rough` to the remaining amplitudes of the two modes (project onto each sine).

```python starter
import numpy as np

smooth = rough = 1.0
print(smooth, rough)
```

```python solution
import numpy as np

N = 31
x = np.arange(1, N + 1) / (N + 1)
e = np.sin(np.pi * x) + np.sin(31 * np.pi * x)
for _ in range(3):
    e = e + (2 / 3) / 2 * (np.r_[e[1:], 0] - 2 * e + np.r_[0, e[:-1]])
s1, s31 = np.sin(np.pi * x), np.sin(31 * np.pi * x)
smooth = np.sum(e * s1) / np.sum(s1 * s1)
rough = np.sum(e * s31) / np.sum(s31 * s31)
print(smooth, rough)
```

```python tests
assert abs(smooth - 0.99040) < 1e-4, f"The smooth mode keeps 99% of itself; got {smooth}."
assert abs(rough + 0.035977) < 1e-4, f"The rough mode falls to -0.036; got {rough}."
```

::: right
Right. The smooth mode keeps 99% and the rough one drops to −3.6%: three sweeps make the error smooth, and the coarse grid has to do the rest.
:::
:::

::: explain
## Why does a coarse grid solve the problem that slows simple iterations?

::: model
Simple iterations change each value using only its neighbors, so they quickly remove error that varies from point to point but hardly touch error that varies slowly across many points. Slowly varying error, though, can be represented accurately with far fewer points. On a grid with twice the spacing, the same error varies twice as fast relative to the grid, so the coarse grid's own smoothing attacks it efficiently, and the coarse problem is several times smaller. Doing this recursively hands each band of frequencies to the grid where it looks rough, so every component of the error is reduced quickly, and the total work stays proportional to the number of unknowns.
:::
:::

::: practice

::: check
given: w = 0.6 | 0.8; t = 0.75 | 0.25
answer: 1 - 2*w*sin(t*pi/2)^2
tolerance: 0.5%

## Damping factor for ω = {w}, θ = {t}π?

::: misses
- [any] $1 - 2\omega\sin^2\frac\theta2$.
:::
:::

::: check
given: K = 400 | 90000; k = 4 | 9
answer: sqrt(K/k)
tolerance: 0.5%

## CG step reduction from κ = {K} to {k}?

::: misses
- [any] $\sqrt{K/k}$.
:::
:::
:::
