On this page
In Class 11 and 12 you solve Ax = b with two or three unknowns, by elimination or Cramer's rule. Engineers doing structural or circuit analysis meet huge versions of it, where almost every entry of A is zero. Jonathan Shewchuk's tutorial calls the conjugate gradient method (CG) "the most prominent iterative method for solving sparse systems of linear equations" (Shewchuk, Carnegie Mellon University, 1994).
What problem does the conjugate gradient method solve?
CG solves Ax = b when A is symmetric (A = Aᵀ) and positive-definite, which means xᵀAx > 0 for every nonzero vector x (Shewchuk, sections 1 and 2).
For such a matrix, solving Ax = b is the same as finding the lowest point of the bowl f(x) = ½xᵀAx − bᵀx, because the gradient of f is Ax − b. In two dimensions its contours are ellipses whose axes follow the eigenvectors of A, as our post on the spectral theorem for symmetric matrices explains.
Why does steepest descent zig-zag?
Steepest descent says: from your current point, walk straight downhill along the residual r = b − Ax, and stop at the lowest point on that line.
Stopping at the lowest point makes the new gradient perpendicular to the direction you just walked, so every step turns through a right angle. Shewchuk notes that the zigzag path "appears because each gradient is orthogonal to the previous gradient" (section 4). In a long, narrow valley the walker keeps crossing the valley floor instead of travelling along it.
What are A-conjugate directions?
Two directions dᵢ and dⱼ are A-conjugate (or A-orthogonal) when dᵢᵀAdⱼ = 0 (Shewchuk, section 7.1).
Stretch the elliptical contours until they become circles, and A-conjugate directions become ordinary perpendicular ones. On circles, one step along each perpendicular direction reaches the centre. So with n unknowns, n conjugate steps reach the exact answer (in exact arithmetic).
Each new CG direction is the new residual plus a multiple of the last direction: dₖ₊₁ = rₖ₊₁ + βdₖ, with β = (rₖ₊₁ᵀrₖ₊₁)/(rₖᵀrₖ). Shewchuk shows that this one correction makes the new direction A-conjugate to all earlier ones, so old directions never need to be stored (section 8). It is Gram–Schmidt with A inside the dot product; our post on Gram–Schmidt orthogonalisation covers the ordinary version.
A worked 2×2 example: the exact answer in two steps
Take the sample system from Shewchuk's section 3: A has rows (3, 2) and (2, 6), b = (2, −8), and the solution is x = (2, −2). Start where his section 4 does, at x₀ = (−2, −2).
Residual: Ax₀ = (−10, −16), so r₀ = b − Ax₀ = (12, 8). The first direction is d₀ = r₀.
Step length: Ad₀ = (52, 72), so α₀ = r₀ᵀr₀ / d₀ᵀAd₀ = 208/1200 = 13/75.
Move: x₁ = x₀ + α₀d₀ = (0.08, −0.613). New residual r₁ = r₀ − α₀Ad₀ = (224/75, −336/75). Check: r₁ · r₀ = 0.
New direction: β = r₁ᵀr₁ / r₀ᵀr₀ = 784/5625, and d₁ = r₁ + βd₀ points along (18, −13).
Conjugacy check: A(18, −13) = (28, −42), and (12, 8) · (28, −42) = 336 − 336 = 0. The ordinary dot product (12, 8) · (18, −13) = 112, so the two directions are conjugate, not perpendicular.
Final step: along (18, −13) the step length is r₁ · (18, −13) / 1050 = 112/1050 = 8/75. So x₂ = (0.08, −0.613) + (8/75)(18, −13) = (2, −2), exactly.
We also ran steepest descent from x₀. Its first step is identical, but after two steps it sits at (226/225, −2), about 1 unit short. Each further pair of steps cuts the error to 56/225 of its size, so it needs nine steps to get within 0.01 of the answer.
If your arithmetic slips, the Fermi Problem Library has a step-by-step solver that explains the reasoning, so you can see where your working went astray.
Why is it used for huge sparse systems?
Three reasons, all from Shewchuk's sections 1 and 10:
Memory. The triangular factors of a sparse A usually have many more nonzero entries than A and may not fit in memory. CG only multiplies by A.
Cost per step. The main work in each iteration is one matrix-vector product, which takes O(m) operations when A has m nonzero entries.
Speed. To shrink the error by a factor ε, the iteration bound for steepest descent is ⌈½κ ln(1/ε)⌉, against ⌈½√κ ln(2/ε)⌉ for CG, where κ is the ratio of A's largest to smallest eigenvalue.
For κ = 10,000 and ε = 10⁻⁶, the bounds in Shewchuk's section 10 work out (our arithmetic) to at most 69,078 iterations for steepest descent and 726 for CG.
For small or dense systems, a direct method such as Cholesky decomposition is usually the better choice.
The method dates to a December 1952 paper by M. R. Hestenes and E. Stiefel in the Journal of Research of the National Bureau of Standards.
If eigenvalues or positive-definiteness feel shaky, the Fermi Concept Library covers every concept in mathematics, physics, chemistry, biology and computing.



