---
title: Conjugate gradients
heading: Going downhill without zigzagging
eyebrow: A13 · Lesson 28 · Conjugate gradients
xp: 40
---

::: hook
For a symmetric positive definite matrix (a resistor network's nodal matrix, a stiffness matrix, a discretized Laplacian), solving $Ax = b$ is the same as finding the bottom of a bowl, $\varphi(x) = \frac12x^TAx - b^Tx$. Rolling straight downhill, steepest descent, zigzags across a long narrow valley and takes forever. The **conjugate gradient method** picks each new direction so it doesn't undo the progress of the earlier ones. It needs no tuning parameter, costs one matrix-vector product per step, and converges in about $\sqrt\kappa$ steps instead of κ. With a good preconditioner it's the standard solver for large sparse symmetric systems.
:::

::: play
- The contours are a bowl $\varphi = \frac12(x^2 + \kappa y^2)$ whose valley is κ times steeper across than along. Both methods start at the same point.
- Steepest descent (orange) goes straight downhill each time, overshoots the valley floor, and zigzags; each step shrinks the error only by $\frac{\kappa - 1}{\kappa + 1}$.
- Conjugate gradients (dashed) takes the same first step, then turns into a direction that's "conjugate" to it and lands exactly on the minimum. In n dimensions it needs at most n steps, in exact arithmetic.
:::
:::

```sim plot
view: flow
x: x = -0.2..1.2
y: y = -0.5..0.5
slider: k = 2..30 step 1 = 10
shade: (x^2 + k*y^2)/2 | contours: 14 | scale: 0.6
path: (((k - 1)/(k + 1))^floor(u)*(1 - (u - floor(u))) + ((k - 1)/(k + 1))^(floor(u) + 1)*(u - floor(u))), (((k - 1)/(k + 1))^floor(u)*cos(pi*floor(u))*(1 - (u - floor(u))) + ((k - 1)/(k + 1))^(floor(u) + 1)*cos(pi*(floor(u) + 1))*(u - floor(u)))/k | over: u = 0..14
path: (1 - step(u - 1))*(1 - u + u*(k - 1)/(k + 1)) + step(u - 1)*(2 - u)*(k - 1)/(k + 1), ((1 - step(u - 1))*(1 - u - u*(k - 1)/(k + 1)) - step(u - 1)*(2 - u)*(k - 1)/(k + 1))/k | over: u = 0..2
dot: 1, 1/k | label: start
dot: 0, 0 | label: minimum
readout: steepest descent: error factor per step = (k - 1)/(k + 1)
readout: steepest descent steps for 10⁻⁶ = ln(1e-6)/ln((k - 1)/(k + 1))
readout: CG bound per step, large systems = (sqrt(k) - 1)/(sqrt(k) + 1)
```

::: predict
## A system has $\kappa(A) = 100$. Steepest descent needs about 690 steps to gain 6 digits. About how many does CG's bound allow?

- [ ] 690
- [ ] 350
- [x] 72
- [ ] 6

::: reveal
About 72: CG's error bound shrinks by $\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1} = \frac{9}{11}$ per step against steepest descent's $\frac{99}{101}$, so it scales with $\sqrt\kappa = 10$ instead of κ = 100.
:::
:::

::: nameit
## The quadratic picture

For symmetric positive definite A, the gradient of $\varphi(x) = \frac12x^TAx - b^Tx$ is $Ax - b = -r$, the negative of the residual, so the minimum of φ is the solution of $Ax = b$.

- **Steepest descent** moves along r, as far as φ keeps falling. Its error (in the A-norm) shrinks by $\frac{\kappa - 1}{\kappa + 1}$ per step at worst.
- **Conjugate gradients** moves along directions $p_k$ that are **A-conjugate**, $p_i^TAp_j = 0$ for $i \ne j$, so a step along one never spoils the minimization along the earlier ones.

## The algorithm

Start with $x_0$, $r_0 = b - Ax_0$, $p_0 = r_0$. Repeat:

$$\alpha_k = \frac{r_k^Tr_k}{p_k^TAp_k}, \quad x_{k+1} = x_k + \alpha_kp_k, \quad r_{k+1} = r_k - \alpha_kAp_k$$

$$\beta_k = \frac{r_{k+1}^Tr_{k+1}}{r_k^Tr_k}, \quad p_{k+1} = r_{k+1} + \beta_kp_k$$

One product $Ap_k$ and a few vector operations per step; only four vectors stored.

## Convergence

$$\lVert x - x_k\rVert_A \le 2\left(\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}\right)^k\lVert x - x_0\rVert_A$$

so reaching a relative accuracy ε takes about $\frac{\sqrt\kappa}{2}\ln\frac2\varepsilon$ steps. For the model problem $\kappa \approx \frac{4N^2}{\pi^2}$, giving about $\frac N\pi\ln\frac2\varepsilon$ steps, like optimal SOR, with no ω to choose.

## Preconditioning

Apply CG to a system with the same solution but a smaller κ, using a matrix M that approximates A and is cheap to solve with: diagonal (Jacobi) scaling, incomplete Cholesky, or a multigrid cycle. For nonsymmetric matrices (circuits with dependent sources) use **GMRES** or **BiCGSTAB**, built on the same ideas.
:::

::: example
## Run CG on $\begin{pmatrix}4 & 1\\ 1 & 3\end{pmatrix}x = \begin{pmatrix}1\\ 2\end{pmatrix}$ from zero.

::: solution
1. **First step.** $r_0 = p_0 = (1, 2)$ and $Ap_0 = (6, 7)$:
   $$\begin{aligned} \alpha_0 &= \frac{5}{20} \\ &= 0.25 \end{aligned}$$
   so $x_1 = (0.25, 0.5)$ and $r_1 = (-0.5, 0.25)$.
2. **New direction.**
   $$\begin{aligned} \beta_0 &= \frac{0.3125}{5} \\ &= 0.0625 \end{aligned}$$
   so $p_1 = (-0.4375, 0.375)$.
3. **Second step.** $\alpha_1 = 0.36364$, and
   $$\begin{aligned} x_2 &= (0.09091, 0.63636) \\ &= \left(\frac{1}{11}, \frac{7}{11}\right) \end{aligned}$$
   exact in two steps, with $r_2 = 0$.

**Answer:** $x = \left(\frac{1}{11}, \frac{7}{11}\right)$, reached exactly in two steps.
:::
:::

::: example
## Compare steepest descent and CG for κ = 100 and 6 digits.

::: solution
1. **Steepest descent.**
   $$\frac{\ln 10^{-6}}{\ln(99/101)} = 690$$
   steps.
2. **CG, by the bound.**
   $$\frac{\ln(2 \times 10^6)}{-\ln(9/11)} = 72$$
   steps.
3. **In practice.** CG often needs fewer, because the bound assumes the worst spread of eigenvalues.

**Answer:** about 690 steps for steepest descent against at most 72 for CG.
:::
:::

::: example
## A = diag(1, 1, 2, 2, 5). In how many steps does conjugate gradients solve Ax = b exactly, in exact arithmetic?

::: solution
1. **What CG minimizes over.** CG's k-th iterate minimizes the error over polynomials of degree k in A.
2. **A polynomial that removes the error.** A has only three distinct eigenvalues, 1, 2 and 5, so the cubic
   $$p(\lambda) = \left(1 - \lambda\right)\left(1 - \frac\lambda2\right)\left(1 - \frac\lambda5\right)$$
   is zero on all of them and equals 1 at λ = 0. That polynomial wipes out the error completely.
3. **The count.** So CG finishes in three steps, though the matrix is 5 × 5.
4. **In general.** CG converges fast when the eigenvalues come in a few tight clusters, which is what a good preconditioner arranges.

**Answer:** three steps.
:::
:::

::: check
given: k = 10 | 100 | 10000
answer: (k - 1)/(k + 1)
tolerance: 0.01%

## A system has κ = {k}. By what factor does steepest descent shrink its error per step, at worst?

::: right
Right. $\frac{\kappa - 1}{\kappa + 1}$ = {answer}.
:::

::: misses
- [(sqrt(k) - 1)/(sqrt(k) + 1)] That's CG's factor; steepest descent uses κ itself.
- [any] Not yet. $\frac{\kappa - 1}{\kappa + 1}$.
:::
:::

::: check
given: k = 100 | 10000 | 1000000
answer: (sqrt(k) - 1)/(sqrt(k) + 1)
tolerance: 0.01%

## And CG's convergence factor for κ = {k}?

::: right
Right. $\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}$ = {answer}.
:::

::: misses
- [(k - 1)/(k + 1)] That's steepest descent's; CG's uses $\sqrt\kappa$.
- [any] Not yet. $\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}$.
:::
:::

::: check
given: k = 100 | 10000 | 1000000; d = 6 | 10
answer: sqrt(k)/2*ln(2*10^d)
tolerance: 2%

## Using $\frac{\sqrt\kappa}{2}\ln\frac2\varepsilon$, about how many CG steps does κ = {k} need for $\varepsilon = 10^{-\given{d}}$?

::: right
Right. About {answer}.
:::

::: misses
- [k/2*ln(2*10^d)] It scales with $\sqrt\kappa$.
- [any] Not yet. $\frac{\sqrt\kappa}{2}\ln\frac{2}{\varepsilon}$.
:::
:::

::: check
given: N = 100 | 1000; d = 6 | 8
answer: N/pi*ln(2*10^d)
tolerance: 2%

## For the model problem on an {N} × {N} grid, about how many CG steps for $\varepsilon = 10^{-\given{d}}$? (Use $\frac N\pi\ln\frac2\varepsilon$.)

::: right
Right. About {answer}: proportional to N, with no ω to tune.
:::

::: misses
- [N^2/pi*ln(2*10^d)] CG's count goes as $\sqrt\kappa \propto N$.
- [any] Not yet. $\frac N\pi\ln\frac2\varepsilon$.
:::
:::

::: check
given: a = 4 | 5; b1 = 1 | 3; b2 = 2 | 4
answer: (b1^2 + b2^2)/(a*b1^2 + 2*b1*b2 + 3*b2^2)
tolerance: 0.1%

## For $\begin{pmatrix}\given{a} & 1\\ 1 & 3\end{pmatrix}x = \begin{pmatrix}\given{b1}\\ \given{b2}\end{pmatrix}$ from $x_0 = 0$, what's CG's first step length $\alpha_0$?

::: right
Right. $\frac{r_0^Tr_0}{p_0^TAp_0}$ = {answer}.
:::

::: misses
- [any] Not yet. With $r_0 = p_0 = b$: $\alpha_0 = \frac{b^Tb}{b^TAb}$.
:::
:::

::: check
kind: choice

## Which systems can plain conjugate gradients solve reliably?

- [ ] Any square system
- [x] Symmetric positive definite ones
- [ ] Only diagonal ones
- [ ] Only those with κ below 100

::: misses
- [1] For nonsymmetric matrices, use GMRES or BiCGSTAB.
- [3] It works for any SPD matrix; diagonal ones are trivial anyway.
- [4] Large κ only makes it slower; preconditioning helps.
:::
:::

::: check
kind: parts
given: N = 50 | 100 | 200

## The model problem on an {N} × {N} grid.

::: part
answer: 4*(N + 1)^2/pi^2
tolerance: 1%

### About what is its condition number?

::: misses
- [N^2] Close in scaling; the constant is $\frac{4}{\pi^2}(N + 1)^2$.
- [any] $\lambda_\max/\lambda_\min \approx \frac{4(N + 1)^2}{\pi^2}$.
:::
:::

::: part
answer: sqrt(4*(N + 1)^2/pi^2)/2*ln(2e6)
tolerance: 1%

### About how many CG steps reach $\varepsilon = 10^{-6}$, by $\frac{\sqrt\kappa}{2}\ln\frac2\varepsilon$?

::: misses
- [any] Put the condition number into the bound.
:::
:::
:::

::: check
kind: derivation
vars: a = 1.5..10

## Take one CG step on $\begin{pmatrix}a & 0\\ 0 & 1\end{pmatrix}x = \begin{pmatrix}1\\ 1\end{pmatrix}$ from zero.

::: step
answer: 2/(a + 1)

### What is the step length $\alpha = \frac{r^Tr}{r^TAr}$?
:::

::: step
answer: (1 - a)/(1 + a)

### What is the first component of the new residual, $1 - a\alpha$?
:::

::: right
Right. The second step will finish the job: a 2 × 2 system needs at most two.
:::
:::

::: explain
## Why does steepest descent zigzag in a long narrow valley, and how do conjugate directions avoid it?

::: model
In a narrow valley the downhill direction points mostly across the valley, toward the nearest wall, rather than along its floor toward the minimum. Steepest descent follows it to the lowest point on that line, which is on the far side of the floor, and the next downhill direction points back across, so the path zigzags while creeping along the valley. Each step also partly undoes the minimization done by the previous ones. Conjugate gradients chooses directions that are conjugate with respect to A, so that minimizing along a new direction leaves the earlier directions still minimized. Progress is never undone, the method reaches the exact minimum in at most n steps, and its convergence depends on $\sqrt\kappa$ instead of κ.
:::
:::

::: practice

::: check
given: k = 10 | 50 | 100 | 500 | 1000 | 10000
answer: (k - 1)/(k + 1)
tolerance: 0.01%

## Steepest-descent factor, κ = {k}?

::: misses
- [any] $\frac{\kappa - 1}{\kappa + 1}$.
:::
:::

::: check
given: k = 100 | 400 | 2500 | 10000 | 90000 | 1000000
answer: (sqrt(k) - 1)/(sqrt(k) + 1)
tolerance: 0.01%

## CG factor, κ = {k}?

::: misses
- [any] $\frac{\sqrt\kappa - 1}{\sqrt\kappa + 1}$.
:::
:::

::: check
given: k = 2500 | 40000; d = 8 | 12
answer: sqrt(k)/2*ln(2*10^d)
tolerance: 2%

## CG steps, κ = {k}, $10^{-\given{d}}$?

::: misses
- [any] $\frac{\sqrt\kappa}{2}\ln\frac2\varepsilon$.
:::
:::
:::
