Skip to content

Iterative solvers

Solve huge systems by repeated sweeps instead of elimination, see steepest descent zig-zag down a long bowl, and watch conjugate gradients reach the bottom in two steps.

Helps to know firstPositive definite matrices

Helps to know first
This topic

First, the picture

Solving a system can mean walking down to the bottom of a bowl. Watch the steepest path zig-zag, and a smarter one arrive in two steps.

Zig-zag or two steps

Two ways down the bowl ½ x^T A x, seen from above.

The bowl f = ½ x^T A x seen from above, A = [2 1.6; 1.6 2]: each ellipse is one height, the bottom at the origin. The start is (10, −8), height 36.

steepest descent
–
conjugate gradients
–
1.6
0.00 / 13.80 s
Describe this picture

A contour map of the bowl f(x)=12x⊤Axf(\mathbf{x})=\tfrac12\mathbf{x}^\top\mathbf{A}\mathbf{x} for A=[21.61.62]\mathbf{A}=\begin{bmatrix}2&1.6\\1.6&2\end{bmatrix}, seen from above on a grid: long thin ellipses round the bottom, at the origin, each through one of the stops below. Steepest descent starts at (10,−8)(10,-8) and walks (6.4,−8)(6.4,-8), then (6.4,−5.12)(6.4,-5.12), and on, in zig-zag steps that alternate across and up, each cutting the height to 0.64 of what it was; it needs 31 steps to get within a millionth of the bottom. Then conjugate gradients starts from the same point, goes to (6.4,−8)(6.4,-8) and then straight to (0,0)(0,0), and stops. Readouts count the steps of each method and give the height reached. One control after the clip: a slider for the off-diagonal entry ss, from 0 to 1.9 in steps of 0.1. At 0 the bowl is round and both methods need one step; towards 1.9 it stretches, while conjugate gradients still needs two.

Elimination on large sparse systems

Elimination solves Ax=b\mathbf{A}\mathbf{x}=\mathbf{b} exactly. For a simulation of heat in a metal plate, or air over a wing, there may be a million unknowns, and each equation involves only a few neighbours: most entries of A\mathbf{A} are zero. Elimination fills those zeros in, and the work and memory explode. A matrix times a vector, though, costs only one product per nonzero entry. So large systems are solved by making a guess and improving it, again and again, with matrix-vector products only. I write the guess after kk steps as x(k)\mathbf{x}^{(k)}, with the step number in brackets so it never reads as a power.

Sweeps: Jacobi and Gauss–Seidel

Split A\mathbf{A} into its diagonal, the part below it and the part above it: A=Adiag+Alow+Aup\mathbf{A}=\mathbf{A}_\text{diag}+\mathbf{A}_\text{low}+\mathbf{A}_\text{up}. Each equation can be solved for its own diagonal unknown, using the current guess for the others. Doing that for every equation at once is a Jacobi sweep:

x(k+1)=Adiag−1(b−(Alow+Aup) x(k)).\begin{aligned} \mathbf{x}^{(k+1)}&=\mathbf{A}_\text{diag}^{-1}\big(\mathbf{b}\\ &\quad-(\mathbf{A}_\text{low}+\mathbf{A}_\text{up})\,\mathbf{x}^{(k)}\big). \end{aligned}

Gauss–Seidel does the same, one equation at a time, using each new value as soon as it has it.

Do the sweeps converge? The error x(k)−x\mathbf{x}^{(k)}-\mathbf{x} is multiplied by a fixed matrix each sweep, for Jacobi TJacobi=−Adiag−1(Alow+Aup)\mathbf{T}_\text{Jacobi}=-\mathbf{A}_\text{diag}^{-1}(\mathbf{A}_\text{low}+\mathbf{A}_\text{up}). After kk sweeps it is Tk\mathbf{T}^k times the first error. By Diagonalisation and matrix powers, that dies away exactly when every eigenvalue of T\mathbf{T} has size below 1. The largest size is the spectral radius, ρ(T)\rho(\mathbf{T}), with one argument; it is not the correlation ρXY\rho_{XY} of statistics. The sweeps converge when ρ(T)<1\rho(\mathbf{T})<1, and each sweep cuts the error by about that factor.

Sweeps

The largest error after each sweep, for Ax = (1, 0, 1) with A = [2 −1 0; −1 2 −1; 0 −1 2].

The largest error after each sweep, starting from x = 0, where every entry is 1 away from the answer (1, 1, 1).

0.00 / 9.40 s
Describe this picture

Two rows of bars for the system [2−10−12−10−12]x=(1,0,1)\begin{bmatrix}2&-1&0\\-1&2&-1\\0&-1&2\end{bmatrix}\mathbf{x}=(1,0,1), whose answer is (1,1,1)(1,1,1). Each bar is the largest error after one more sweep, for sweeps 1 to 6, starting from x(0)=0\mathbf{x}^{(0)}=\mathbf{0}, and is labelled with its value. Jacobi’s row appears first: 1, 0.5, 0.5, 0.25, 0.25, 0.125, halving every two sweeps. Then Gauss–Seidel’s row: 0.75, 0.375, 0.1875, 0.0938, 0.0469, 0.0234, halving every sweep. There is no control.

For this matrix TJacobi=[00.500.500.500.50]\mathbf{T}_\text{Jacobi}=\begin{bmatrix}0&0.5&0\\0.5&0&0.5\\0&0.5&0\end{bmatrix}, with eigenvalues 0 and ±1/2\pm1/\sqrt2, so ρ=0.7071\rho=0.7071: two sweeps halve the error. For Gauss–Seidel ρ=0.5\rho=0.5, the square: one sweep halves it.

The catch is size. On the same pattern with 100 unknowns, ρJacobi=cos⁡(π/101)=0.99952\rho_\text{Jacobi}=\cos(\pi/101)=0.99952, and cutting the error to 10−610^{-6} takes more than 28 000 sweeps.

Down the bowl

There is a better way to think when A\mathbf{A} is symmetric and positive definite. By Positive definite matrices, the function

f(x)=12 x⊤Ax−b⊤xf(\mathbf{x})=\tfrac12\,\mathbf{x}^\top\mathbf{A}\mathbf{x}-\mathbf{b}^\top\mathbf{x}

is a bowl, and its lowest point is where the slope is zero: Ax−b=0\mathbf{A}\mathbf{x}-\mathbf{b}=\mathbf{0}. Solving the system is finding the bottom. The residual r=b−Ax\mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{x} is minus the slope, so it points downhill.

Steepest descent walks downhill along r\mathbf{r}, as far as the ground keeps falling, then looks again. The best step length along r(k)\mathbf{r}^{(k)} is

αk=r(k)⋅r(k)r(k)⋅Ar(k),\alpha_k=\frac{\mathbf{r}^{(k)}\cdot\mathbf{r}^{(k)}}{\mathbf{r}^{(k)}\cdot\mathbf{A}\mathbf{r}^{(k)}},

and x(k+1)=x(k)+αkr(k)\mathbf{x}^{(k+1)}=\mathbf{x}^{(k)}+\alpha_k\mathbf{r}^{(k)}. Each new direction is at a right angle to the last, which is why it zig-zags. In a long thin bowl, downhill points mostly across the valley, not along it. The height is multiplied by at most the factor ((κ−1)/(κ+1))2\big((\kappa-1)/(\kappa+1)\big)^2 per step, where κ\kappa is the condition number of Conditioning: for the bowl in the picture, κ=3.6/0.4=9\kappa=3.6/0.4=9 and the factor is 0.82=0.640.8^2=0.64.

Conjugate gradients

The trouble with steepest descent is that each step spoils some of the progress of the step before. Conjugate gradients chooses each new direction so that it doesn’t. It mixes the new downhill direction with the last direction:

d(k+1)=r(k+1)+βk d(k),βk=r(k+1)⋅r(k+1)r(k)⋅r(k),\begin{aligned} \mathbf{d}^{(k+1)}&=\mathbf{r}^{(k+1)}+\beta_k\,\mathbf{d}^{(k)},\\ \beta_k&=\frac{\mathbf{r}^{(k+1)}\cdot\mathbf{r}^{(k+1)}}{\mathbf{r}^{(k)}\cdot\mathbf{r}^{(k)}}, \end{aligned}

and steps along d\mathbf{d} by the best length. The directions are chosen so that each step finishes off its own direction for good. In nn dimensions there are only nn directions to finish, so, apart from rounding, it reaches the bottom in at most nn steps. Long before that, its error shrinks on average by at least (κ−1)/(κ+1)(\sqrt\kappa-1)/(\sqrt\kappa+1) per step, up to a factor of 2 overall: 0.5 for κ=9\kappa=9, against steepest descent’s 0.8.

In signal processing · adaptive filters

Adaptive filters: LMS, in the signal processing course, tunes a filter by steepest descent, one noisy step per sample, down the bowl of its mean squared error. Its speed depends on the condition number of the input’s correlation matrix, exactly as here.

Worked example

1. Jacobi, from x(0)=0\mathbf{x}^{(0)}=\mathbf{0} on the tridiagonal system: (0.5, 0, 0.5)(0.5,\,0,\,0.5), (0.5, 0.5, 0.5)(0.5,\,0.5,\,0.5), (0.75, 0.5, 0.75)(0.75,\,0.5,\,0.75), (0.75, 0.75, 0.75)(0.75,\,0.75,\,0.75), (0.875, 0.75, 0.875)(0.875,\,0.75,\,0.875). The largest error halves every two sweeps.

2. Gauss–Seidel, the same system: (0.5, 0.25, 0.625)(0.5,\,0.25,\,0.625), (0.625, 0.625, 0.8125)(0.625,\,0.625,\,0.8125), (0.8125, 0.8125, 0.90625)(0.8125,\,0.8125,\,0.90625), (0.90625, 0.90625, 0.953125)(0.90625,\,0.90625,\,0.953125). The largest error halves every sweep.

3. Steepest descent on A=[21.61.62]\mathbf{A}=\begin{bmatrix}2&1.6\\1.6&2\end{bmatrix} with b=0\mathbf{b}=\mathbf{0}, from (10,−8)(10,-8): r(0)=−A(10,−8)=(−7.2, 0)\mathbf{r}^{(0)}=-\mathbf{A}(10,-8)=(-7.2,\,0) and α0=0.5\alpha_0=0.5, so x(1)=(6.4,−8)\mathbf{x}^{(1)}=(6.4,-8). Then r(1)=(0, 5.76)\mathbf{r}^{(1)}=(0,\,5.76), α1=0.5\alpha_1=0.5 and x(2)=(6.4,−5.12)\mathbf{x}^{(2)}=(6.4,-5.12). The height goes 36, 23.04, 14.7456: a factor of 0.64 each step, the worst case exactly. After 10 steps 0.0115 of it is left, and reaching 10−610^{-6} takes 31 steps.

4. Conjugate gradients, same start. The first step is the same, to (6.4,−8)(6.4,-8). Then β0=33.1776/51.84=0.64\beta_0=33.1776/51.84=0.64 and

d(1)=(0, 5.76)+0.64 (−7.2, 0)=(−4.608, 5.76).\begin{aligned} \mathbf{d}^{(1)}&=(0,\,5.76)+0.64\,(-7.2,\,0)\\ &=(-4.608,\,5.76). \end{aligned}

With Ad(1)=(0, 4.1472)\mathbf{A}\mathbf{d}^{(1)}=(0,\,4.1472), the step length is α1=33.1776/(5.76×4.1472)=25/18\alpha_1=33.1776/(5.76\times4.1472)=25/18, and

x(2)=(6.4,−8)+2518 (−4.608, 5.76)=(0, 0).\begin{aligned} \mathbf{x}^{(2)}&=(6.4,-8)+\tfrac{25}{18}\,(-4.608,\,5.76)\\ &=(0,\,0). \end{aligned}

The bottom, exactly, in two steps.

Try these before you read the answers.

  • How many conjugate-gradient steps can a 2×22\times2 system need? At most 2.
  • Steepest descent’s factor per step at κ=9\kappa=9? 0.8, so 0.64 for the height.
  • Set the off-diagonal to 0 in the picture. How many steepest-descent steps? One: the bowl is round, and downhill points at the bottom.

Where you’ll meet this

Simulation. Heat, stress and flow simulations with millions of unknowns solve sparse, symmetric positive definite systems by conjugate gradients, usually with a cheap first guess at the inverse to make κ\kappa smaller.

Medical scanners. Building a picture from a scanner’s measurements is a huge least-squares problem, solved by iteration.

Learning machines. Training a model by small downhill steps on its error is steepest descent, and its slow zig-zag in long valleys is the problem this page shows.

Reference card

QuantityFormulaNotes
SplittingA=Adiag+Alow+Aup\mathbf{A}=\mathbf{A}_\text{diag}+\mathbf{A}_\text{low}+\mathbf{A}_\text{up}diagonal, below, above
Jacobix(k+1)=Adiag−1(b−(Alow+Aup)x(k))\mathbf{x}^{(k+1)}=\mathbf{A}_\text{diag}^{-1}\big(\mathbf{b}-(\mathbf{A}_\text{low}+\mathbf{A}_\text{up})\mathbf{x}^{(k)}\big)all at once
Convergenceρ(T)<1\rho(\mathbf{T})<1error shrinks by about ρ\rho per sweep
Bowlf=12x⊤Ax−b⊤xf=\tfrac12\mathbf{x}^\top\mathbf{A}\mathbf{x}-\mathbf{b}^\top\mathbf{x}bottom at Ax=b\mathbf{A}\mathbf{x}=\mathbf{b}
Residualr=b−Ax\mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{x}points downhill
Steepest descentαk=r⋅r/r⋅Ar\alpha_k=\mathbf{r}\cdot\mathbf{r}/\mathbf{r}\cdot\mathbf{A}\mathbf{r}factor (κ−1)/(κ+1)(\kappa-1)/(\kappa+1) per step
Conjugate gradientsd(k+1)=r(k+1)+βkd(k)\mathbf{d}^{(k+1)}=\mathbf{r}^{(k+1)}+\beta_k\mathbf{d}^{(k)}at most nn steps; factor (κ−1)/(κ+1)(\sqrt\kappa-1)/(\sqrt\kappa+1)

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look