demoAINumerics
Jacobi, SOR and conjugate gradients on the Poisson equation
The five-point Poisson equation on a 15 by 15 grid, solved to a relative residual of four ways. Jacobi takes 948 sweeps, Gauss-Seidel 475, SOR with the optimal factor 60, and conjugate gradients 25 iterations. Each count follows from one number the theory predicts: Jacobi's error shrinks by per sweep once the smoothest mode dominates, Gauss-Seidel's by its square, and SOR and CG depend on . Halve and Jacobi takes four times as many sweeps; SOR and CG take twice as many.
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
The problem is on the unit square with on the edge, on an by grid of interior points with spacing . The five-point stencil turns it into linear equations,
one per grid point, and for short. Direct elimination is possible at this size, but the iterative methods here need only the stencil, so they are what gets used when the grid is large or three-dimensional.
The forcing has the exact solution . The second difference of a quadratic is exact, so the grid solution equals at every grid point, and whatever error remains is the solver's.
Every method starts from and stops when . The Python twin is posts/refs/jacobi-sor-and-cg-on-the-poisson-equation.py
(output beside it).
Jacobi
A Jacobi sweep solves each equation for its own unknown, using the neighbors' values from the previous sweep: . All points update at once, so it is a few whole-array operations.
Its error obeys . The eigenvectors of are the grid sine modes , with eigenvalues , so a sweep multiplies each mode by . The smoothest mode, , is multiplied by , the largest factor, and after enough sweeps it is the only one left: the residual then shrinks by exactly per sweep, at . Getting from 1 to at that rate takes sweeps.
Printed
Tests 4 of 4 pass
- Jacobi takes 948 sweeps at n = 15, as the Python twin does (passes)
- and its residual shrinks by cos(pi h) per sweep at the end, to 1e-7 (passes)
- which predicts the count to within 2 sweeps (passes)
- the answer is the exact solution to 1e-9 (passes)
Gauss-Seidel and SOR
Gauss-Seidel does the same update point by point, in order, and uses each new value as soon as it has it. For this matrix its error factor per sweep is exactly the square of Jacobi's, , so it takes half the sweeps.
SOR (successive over-relaxation) takes the Gauss-Seidel update and goes further in the same direction: $u_{ij} \leftarrow u_{ij} + \omega\,(u^{GS}_{ij}
- u_{ij})$, with . David Young worked out the best factor for
this matrix in 1950: , which makes the error factor . Jacobi's is . The distance from 1 is what sets the number of sweeps, so Jacobi's count grows like and SOR's like . At , .
At the optimal the eigenvalues of the SOR iteration matrix pair up and it cannot be diagonalized, so the residual is not a constant times : the multiple grows, to 111 at sweep 30 and 147 at sweep 60. That is why SOR takes 60 sweeps here, not the the factor alone would give.
Printed
Tests 5 of 5 pass
- Gauss-Seidel takes 475 sweeps, half of Jacobi's 948 (the Python twin agrees) (passes)
- its residual shrinks by cos^2(pi h) per sweep at the end, to 1e-7 (passes)
- SOR at omega = 1.6735 takes 60 sweeps, where the factor alone predicts 47 (passes)
- the SOR residual is 111 times (omega - 1)^k at k = 30, and 147 times at k = 60 (passes)
- SOR's answer is the exact solution to 1e-9 (passes)
Conjugate gradients
CG treats as minimizing and searches along directions that are conjugate, , so no step undoes an earlier one. It needs only products with . Its error bound depends on the condition number , the ratio of the largest eigenvalue of to the smallest: after steps the error, measured in the norm , has shrunk by at least . For the five-point matrix , which is 103 at . The bound promises within iterations. It is a worst case over right-hand sides, and it bounds that error rather than the residual the stopping test measures, so it is a guide to the count, not a prediction of it. This right-hand side needs 25.
Like SOR, CG's count grows like . Unlike SOR, it needs no parameter tuned to the matrix.
Printed
Tests 3 of 3 pass
- CG takes 25 iterations at n = 15 (the Python twin agrees) (passes)
- kappa is 103.09 (numpy: lambda_max / lambda_min of A), and the bound promises 97 iterations (passes)
- CG's answer is the exact solution to 1e-10 (passes)
The four residuals
The figure plots the residual of each method against the iteration count. The dashed lines are the theory's rates: for Jacobi and for Gauss-Seidel. After the first few sweeps each measured line runs parallel to its dashed one.
Halving h
The last cell solves the same problem on 23 by 23 and 31 by 31 grids ( and ) with Jacobi, SOR and CG, and plots the counts against on log axes. Doubling from 16 to 32 multiplies Jacobi's count by 4.01 and SOR's and CG's by 2.00 and 2.08. The dashed lines are the predictions, scaled to pass through the point: for Jacobi, for SOR and for CG.
Printed
Tests 3 of 3 pass
- the counts at h = 1/16, 1/24, 1/32 are the Python twin's (passes)
- halving h multiplies Jacobi's count by 4.01, and SOR's and CG's by 2.00 and 2.08 (passes)
- each count follows its prediction's growth to within 5% (passes)