Skip to content

Subspaces & Least Squares

Ax = b from two angles, the four fundamental subspaces, and what to do when there is no exact solution

1. Why Ax = b?

Sooner or later most engineering problems turn into a linear system: Kirchhoff’s laws for a circuit, a stiffness equation K u = f for a structure, the grid equations behind the numerical PDE solvers, a data-fitting problem. The Linear Algebra chapter showed what a matrix does to space. This chapter asks the inverse questions:

  • Given b, is there an x with Ax = b? (existence)
  • If so, is it the only one? (uniqueness)
  • If there is none, what is the best x? (least squares)

2. Two pictures of the same system

Read a system by rows and each equation is a line (a plane in 3D): the solution is where they all meet. Read it by columns and the same system says something different — b must be built as a combination of the columns of A:

The column picture

3. Elimination & LU

Gaussian elimination subtracts multiples of one row from the rows below to create zeros under each pivot, ending in an upper-triangular system Ux = c that back-substitution solves. The multipliers you used are not wasted: stored in a lower-triangular matrix L, they give the factorization

LU factorization

Factoring costs about (2/3)n³ operations, but each new right-hand side then costs only n² — which is why you factor once and reuse. Real software also swaps rows to avoid tiny pivots (partial pivoting, PA = LU); the hand example below needs none.

Worked example 1: eliminate a 3 × 3 system

Row 2 − 2·row 1 and row 3 + 1·row 1 clear the first column; then row 3 + 1·row 2 clears the second. The multipliers 2, −1, −1 go into L:

Forward substitution Lc = b gives c = (5, −12, 2); back substitution Ux = c gives x₃ = 2, then x₂ = 1, then x₁ = 1. Check the first equation: 2 + 1 + 2 = 5 ✓.

4. Column space, null space & rank

Every m × n matrix A carries four fundamental subspaces. Two live in the input space ℝⁿ and two in the output space ℝᵐ:

SubspaceLives inMeaningDimension
Column space C(A)ℝᵐall outputs Ax — the reachable br
Null space N(A)ℝⁿinputs sent to 0 (Ax = 0)n − r
Row space C(Aᵀ)ℝⁿcombinations of the rowsr
Left null space N(Aᵀ)ℝᵐvectors y with Aᵀy = 0m − r

Here r is the rank, the number of pivots. The row space is perpendicular to the null space, and the column space is perpendicular to the left null space. Two consequences follow at once: Ax = b is solvable exactly when b is in the column space, and the complete solution is one particular solution plus anything from the null space:

Rank–nullity & the complete solution

Worked example 2: the four subspaces of a 3 × 3 matrix

Row 2 is twice row 1, so elimination finds only two pivots: r = 2. Then dim N(A) = 3 − 2 = 1 and the left null space also has dimension 3 − 2 = 1. Concretely:

  • Null space: spanned by (1, −2, 1). Check: 1 − 4 + 3 = 0, 2 − 8 + 6 = 0, 1 − 2 + 1 = 0.
  • Column space: a plane in ℝ³ spanned by columns (1, 2, 1) and (2, 4, 1).
  • Left null space: spanned by (−2, 1, 0), because −2·row 1 + row 2 = 0.

So Ax = b is solvable only for the b on that plane, and then there is a whole line of solutions.

Try it

A 5 × 3 matrix has rank 2. How many free variables does Ax = 0 have, and what is the dimension of each of the four subspaces?

5. Orthogonality & projection

When b is not in the column space, the best we can do is pick the closest vector that is. “Closest” is measured by distance, and the key geometric fact is: the closest point on a line (or subspace) is the one where the miss is perpendicular. For the line through a:

Projection onto a line

Worked example 3: project b = (1, 3) onto a = (3, 1)

aᵀb = 3 + 3 = 6 and aᵀa = 10, so x̂ = 0.6, p = 0.6·(3, 1) = (1.8, 0.6) and e = b − p = (−0.8, 2.4). Check: aᵀe = −2.4 + 2.4 = 0 ✓, and |p|² + |e|² = 3.6 + 6.4 = 10 = |b|² ✓. The projection matrix and its two defining properties:

P² = P because projecting something already on the line changes nothing. Its eigenvalues are 1 (along a) and 0 (perpendicular to a): in particular P is singular — it throws information away.

6. Least squares

Now take the whole column space. With more equations than unknowns (m > n) and noisy data, Ax = b usually has no solution. Instead minimise the total squared error ‖Ax − b‖². The minimiser makes Ax̂ the projection of b onto the column space, so the residual is perpendicular to every column of A:

Least squares & the normal equations

For fitting a line y = C + Dt through points (ti, yi) the columns of A are the all-ones vector and the t-values. Watch the squares of the misses shrink as the line moves toward the solution of the normal equations:

Worked example 4: the line through (0, 6), (1, 0), (2, 0)

The normal equations 3C + 3D = 6 and 3C + 5D = 0 give D = −3 and C = 5: the best line is y = 5 − 3t. Then

And e is perpendicular to both columns: e·(1,1,1) = 1 − 2 + 1 = 0 and e·(0,1,2) = 0 − 2 + 2 = 0 ✓. This is exactly what the animation’s two readouts measured.

7. Gram–Schmidt & QR

Normal equations work on paper, but forming AᵀA squares the condition number — and with it the sensitivity to round-off (see the Numerical Methods chapter). Orthonormal columns avoid the problem: if QᵀQ = I, the normal equations collapse to x̂ = Qᵀb. Gram–Schmidt manufactures such a Q from any independent columns, one at a time, by subtracting the components along the vectors already built:

Gram–Schmidt and A = QR

Worked example 5: QR of two vectors, and why it matters

Take a₁ = (1, 1, 0) and a₂ = (1, 0, 1). Then

The numerical payoff. Fit a degree-9 polynomial to 20 points in [0, 1]. The Vandermonde matrix has condition number about 3.8 × 10⁶, but AᵀA has about 1.4 × 10¹³: the normal equations leave almost no correct digits in double precision (about 16), while QR works with the original 3.8 × 10⁶.

Try it

If Q has orthonormal columns, what is the least-squares solution of Qx = b, and what is the error?

8. Practice & where it shows up

Try it: is P invertible?

A projection matrix satisfies P² = P. Can a projection other than the identity be invertible?

Try it: how many equations does a unique answer need?

You fit a plane z = c₀ + c₁x + c₂y. What is the minimum number of data points for a unique exact fit, and what happens with 100 noisy points?

Regression & curve fitting

Fitting a line, polynomial or sensor-calibration curve to noisy data is exactly Ax ≈ b. Under Gaussian noise the least-squares fit is also the maximum-likelihood fit.

GPS & navigation

A receiver measures distances to four or more satellites (more equations than unknowns, plus noise) and solves for position and clock error by least squares.

Compression & denoising

Keeping the first k singular vectors is a projection onto a k-dimensional subspace. Projecting a noisy signal onto a smooth subspace removes noise.

Structures & circuits

Kirchhoff’s laws and finite-element stiffness equations are linear systems K u = f. Their solvability and uniqueness are questions about rank and the null space.

9. Resources

This chapter follows the first two units of MIT 18.06 (Ax = b and the four subspaces; least squares) and the linear algebra half of Stanford’s CME 104. Boyd & Vandenberghe’s free textbook is excellent on least squares in practice.

Site soundtrack