Conjugate Gradient
Follow conjugate search directions to solve a positive-definite system and watch its residual disappear.
By Riano · Published · Updated · Report a correction
Conjugate Gradient Method
Concept Overview
The Conjugate Gradient (CG) method is an algorithm for the numerical solution of particular systems of linear equations, namely those whose matrix is symmetric and positive-definite (SPD). It is often used as an iterative algorithm for systems that are too large to be handled by a direct implementation or other direct methods such as the Cholesky decomposition.
Worked Example
Consider the symmetric positive-definite system Ax = b with A and b defined as follows, and an initial guess x0 = 0:
- Initial State: x0 = [0, 0]. The initial residual is r0 = b - Ax0 = [2, 1]. The initial search direction is p0 = r0 = [2, 1].
- Step 1:
- Compute Ap0 = [{3, 1}, {1, 2}] [2, 1] = [7, 4].
- Compute α0 = (r0 · r0) / (p0 · Ap0) = (4 + 1) / (14 + 4) = 5 / 18.
- Update x1 = x0 + α0 p0 = [0, 0] + (5/18) [2, 1] = [5/9, 5/18].
- Update r1 = r0 - α0 Ap0 = [2, 1] - (5/18) [7, 4] = [1/18, -1/9].
- Direction Update:
- Compute β0 = (r1 · r1) / (r0 · r0) = (1/324 + 1/81) / 5 = (5/324) / 5 = 1/324.
- Update p1 = r1 + β0 p0 = [1/18, -1/9] + (1/324) [2, 1] = [5/81, -35/324].
- Step 2:
- Compute Ap1. After exact math, we get α1 = 18/25.
- Update x2 = x1 + α1 p1 = [5/9, 5/18] + (18/25) [5/81, -35/324] = [3/5, 1/5].
- Update r2 = r1 - α1 Ap1 = [0, 0].
The exact solution is x = [3/5, 1/5], which we obtained in exactly 2 steps, a characteristic of CG on a 2x2 SPD matrix.
Experiment
Open the Conjugate Gradient module to interact with this concept:
- Set A11 to 3, A12 to 1, and A22 to 2. Set b1 to 2 and b2 to 1.
- Set Updates to 1. The status will show "Finite iterate; not yet converged". The iterate is [5/9, 5/18] and the residual is [1/18, -1/9].
- Increase Updates to 2. The algorithm converges exactly to [3/5, 1/5] with a zero residual.
- Change A11 to 1, A12 to 2, and A22 to 1. This matrix is not positive-definite because its determinant is 1*1 - 2*2 = -3. The module will reject it and display "Outside SPD domain".
- Set b1 to 0 and b2 to 0 with an SPD matrix. The initial residual is zero, so the algorithm stops immediately without dividing by zero.
Limitations
This explorer is designed for 2x2 real symmetric positive-definite (SPD) matrices. The Conjugate Gradient method requires the matrix to be SPD to guarantee convergence and avoid division by zero during the computation of the step sizes (α and β). If a non-SPD matrix is provided, it is rejected using exact principal-minor tests. The visualization only shows standard unpreconditioned linear CG. In a real-world scenario with large matrices, floating-point rounding errors might prevent the residual from becoming exactly zero, requiring a stopping criterion based on a tolerance (e.g., 1e-9).
References
The standard Conjugate Gradient algorithm:
- SciPy sparse.linalg.cg documentation describes the SPD system requirement and the actual residual convergence criterion. The exact rational CG coefficients were independently checked against the 2x2 inverse solution.
Experience it interactively
Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Conjugate Gradient module.
Try Conjugate Gradient on Riano →