This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · posts/jacobi-sor-and-cg-on-the-poisson-equation

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.

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).

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 32–97

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.

[2]lines 117–168

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.

[3]lines 193–252

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.

[4]lines 274–333

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.

[5]lines 344–371

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.

[6]lines 385–456