A1328 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A13 · Lesson 28 · Conjugate gradients

Going downhill without zigzagging

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, φ(x)=12xTAx−bTx. 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 κ steps instead of κ. With a good preconditioner it's the standard solver for large sparse symmetric systems.

  • The contours are a bowl φ=12(x2+κy2) 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 κ−1κ+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.

Predict

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

Name it

The quadratic picture

For symmetric positive definite A, the gradient of φ(x)=12xTAx−bTx 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 κ−1κ+1 per step at worst.
  • Conjugate gradients moves along directions pk that are A-conjugate, piTApj=0 for i≠j, so a step along one never spoils the minimization along the earlier ones.

The algorithm

Start with x0, r0=b−Ax0, p0=r0. Repeat:

αk=rkTrkpkTApk,xk+1=xk+αkpk,rk+1=rk−αkApk
βk=rk+1Trk+1rkTrk,pk+1=rk+1+βkpk

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

Convergence

‖x−xk‖A≤2(κ−1κ+1)k‖x−x0‖A

so reaching a relative accuracy ε takes about κ2ln⁡2ε steps. For the model problem κ≈4N2π2, giving about Nπln⁡2ε 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.

Worked example

Run CG on (4113)x=(12) from zero.

Show the solution
  1. First step. r0=p0=(1,2) and Ap0=(6,7): α0=520=0.25 so x1=(0.25,0.5) and r1=(−0.5,0.25).
  2. New direction. β0=0.31255=0.0625 so p1=(−0.4375,0.375).
  3. Second step. α1=0.36364, and x2=(0.09091,0.63636)=(111,711) exact in two steps, with r2=0.

Answer x=(111,711), reached exactly in two steps.

Worked example

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

Show the solution
  1. Steepest descent. ln⁡10−6ln⁡(99/101)=690 steps.
  2. CG, by the bound. ln⁡(2×106)−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.

Worked example

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

Show the 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(λ)=(1−λ)(1−λ2)(1−λ5) 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

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

Check

And CG's convergence factor for κ = 10000?

Check

Using κ2ln⁡2ε, about how many CG steps does κ = 10000 need for ε=10−6?

Check

For the model problem on a 100 × 100 grid, about how many CG steps for ε=10−6? (Use Nπln⁡2ε.)

Check

For (4113)x=(14) from x0=0, what's CG's first step length α0?

Check

Which systems can plain conjugate gradients solve reliably?

Check

The model problem on a 100 × 100 grid.

(a)

About what is its condition number?

(b)

About how many CG steps reach ε=10−6, by κ2ln⁡2ε?

Check

Take one CG step on (a001)x=(11) from zero.

Step 1

What is the step length α=rTrrTAr?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

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

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.