Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Least Squares and Projection


Project b onto the column space of A: the normal equations AᵀA x̂ = Aᵀb, the projection p = A x̂, and the perpendicular residual.

Preset?When Ax = b has no solution — b is not in the column space of A — the least-squares solution x̂ makes Ax̂ as close to b as possible. The closest point is the projection p of b onto the column space, and the error e = b − p is perpendicular to every column: Aᵀ(b − Ax̂) = 0, the normal equations AᵀA x̂ = Aᵀb. When the columns are independent AᵀA is invertible and P = A(AᵀA)⁻¹Aᵀ is the projection matrix.
Size
A3×2
A and b
|
|
|
A3×2
1
0
1
1
1
2
x =
b
6
0
0
Step 1 / 8

Step explanations

1Least squares: 3 equations, 2 unknowns
Ax = b has 3 equations and 2 unknowns: fitting a line b = C + Dt to 3 data points, one equation per point. It has an exact solution only if b lies in the column space of A. When it does not, the best we can do is make Ax as close to b as possible: minimize |b − Ax|. That minimum is reached at the projection of b onto the column space, and the normal equations find it.
Ax = b has no solution; make Ax as close to b as possible. Learn more about the setup · what least squares is








Key Terms

Least-squares solution — the x^\hat{\mathbf{x}} that minimizes Axb\|A\mathbf{x} - \mathbf{b}\| when Ax=bA\mathbf{x} = \mathbf{b} has no exact solution.

Normal equationsATAx^=ATbA^TA\hat{\mathbf{x}} = A^T\mathbf{b}; always consistent, uniquely solvable when the columns of AA are independent.

Projectionp=Ax^\mathbf{p} = A\hat{\mathbf{x}}, the point of the column space closest to b\mathbf{b}.

Residuale=bp\mathbf{e} = \mathbf{b} - \mathbf{p}, perpendicular to every column of AA: ATe=0A^T\mathbf{e} = \mathbf{0}.

Projection matrixP=A(ATA)1ATP = A(A^TA)^{-1}A^T, with Pb=pP\mathbf{b} = \mathbf{p}; symmetric and idempotent, P2=PP^2 = P.

Design matrix — for fitting b=C+Dtb = C + Dt, the matrix with a column of ones and a column of tt values, one row per data point.

Overdetermined system — more equations than unknowns; generally inconsistent, and the setting for least squares.

Sum of squared errorse2\|\mathbf{e}\|^2, the quantity least squares minimizes.

Orthogonal decompositionb=p+e\mathbf{b} = \mathbf{p} + \mathbf{e} with pC(A)\mathbf{p} \in C(A) and eN(AT)\mathbf{e} \in N(A^T), so b2=p2+e2\|\mathbf{b}\|^2 = \|\mathbf{p}\|^2 + \|\mathbf{e}\|^2.

Getting Started with the Visualizer

Choose a matrix and a right-hand side, then watch b\mathbf{b} get projected onto the column space.

• Use the Preset pills for seven problems: a line through three points, a line through four, a parabola through four, projection onto a single line, projection onto a coordinate plane, a consistent system with zero residual, and a matrix with dependent columns
• Use the two Size steppers for two to four equations and one to three unknowns; Shuffle data builds a polynomial design matrix with random data
• Edit any entry of AA or of b\mathbf{b} directly; b\mathbf{b} is the amber column
• Hover the ? icon for a reminder of why the residual must be perpendicular to the columns
• Press play or step manually; the step log on the right keeps every stage

When AA is a design matrix the captions translate everything into the fit: the fitted line or parabola, the fitted values at the data points, and the errors.

The Scenes in Order

The visualizer follows the textbook procedure exactly.

Consistency[Ab][A \mid \mathbf{b}] row reduced; a pivot in the last column means no exact solution
Normal equationsATAA^TA and ATbA^T\mathbf{b} formed as products, with their meaning as dot products
Dependent columns — if ATAA^TA is singular the run stops with an explanation
Solve[ATAATb][A^TA \mid A^T\mathbf{b}] row reduced to x^\hat{\mathbf{x}}; for a single column this is aTb/aTa\mathbf{a}^T\mathbf{b} / \mathbf{a}^T\mathbf{a}
Projectp=Ax^\mathbf{p} = A\hat{\mathbf{x}} as a combination of the columns, compared with b\mathbf{b}
Residuale=bp\mathbf{e} = \mathbf{b} - \mathbf{p}, its length, ATe=0A^T\mathbf{e} = \mathbf{0}, and the Pythagorean split of b2\|\mathbf{b}\|^2
Projection matrixP=A(ATA)1ATP = A(A^TA)^{-1}A^T with Pb=pP\mathbf{b} = \mathbf{p}, P2=PP^2 = P and trace equal to the rank
Done — the fitted curve when there is one, and the sum of squared errors

No Exact Solution

The frozen picture below shows the default preset's augmented matrix [Ab][A \mid \mathbf{b}] reduced, with a pivot in the last column: one row reads 0=10 = 1, so no line passes through the three points.

That failure is what least squares is for.
[A | b]3×3106110120rref3×3100010001
Inconsistent system, frozen

[A | b] for the points (0, 6), (1, 0), (2, 0), reduced: a pivot in the last column. No line passes through all three points, so least squares takes over.

A pivot in the augmented column means b\mathbf{b} is not a combination of the columns of AA: it lies outside the column space, which here is a plane in R3\mathbb{R}^3. Three points determine a line only if they happen to be collinear; three generic points do not, and three equations in two unknowns generally have no solution.

The consistency check also tells the tool what to expect. When no pivot lands in the last column the least-squares solution will be exact and the residual zero, which the consistent preset shows.

The Normal Equations

The frozen picture below shows ATA=(3335)A^TA = \begin{pmatrix} 3 & 3 \\ 3 & 5 \end{pmatrix} and ATb=(6,0)A^T\mathbf{b} = (6, 0) for the default preset, formed as the two products ATAA^T \cdot A and ATbA^T \cdot \mathbf{b}.

Each entry is a dot product of columns: the 33 in the corner is the column of ones dotted with itself, the count of data points.
Aᵀ2×3111012A3×2101112=AᵀA2×23335,Aᵀ2×3111012b600=Aᵀb60
Normal equations, frozen

AᵀA = [3, 3; 3, 5] and Aᵀb = (6, 0), formed as products. Each entry is a dot product of columns: the 3 in the corner counts the data points.

Multiplying the inconsistent system through by ATA^T produces a square system that is always solvable, and that is not a trick: it is the statement that the residual must be perpendicular to every column. For a line fit the two equations say the errors sum to zero and the errors are uncorrelated with tt.

ATAA^TA is symmetric and positive semidefinite, and positive definite exactly when the columns are independent. Its entries, the dot products of the columns, are why highly correlated predictors make regression unstable: nearly parallel columns make ATAA^TA nearly singular.

Solving for x-hat

The frozen picture below shows [ATAATb][A^TA \mid A^T\mathbf{b}] reduced to x^=(5,3)\hat{\mathbf{x}} = (5, -3): the best line is b=53tb = 5 - 3t.

Two equations, two unknowns, and a unique answer, unlike the original three equations.
[AᵀA | Aᵀb]2×3336350rref2×310501−35−3
Solving for x̂, frozen

[AᵀA | Aᵀb] reduced to x̂ = (5, −3): the best line is b = 5 − 3t. Two equations, two unknowns, one answer.

The normal equations are a small square system, and any method solves them: row reduction as here, the 2×22 \times 2 inverse formula, or Cramer's rule. For larger problems the standard practice is to avoid forming ATAA^TA at all and use a QR factorization of AA, which gives the same x^\hat{\mathbf{x}} with less loss of precision.

The solution has a statistical reading. With a column of ones, DD is the covariance of tt and bb divided by the variance of tt, and CC makes the line pass through the mean point. Here the mean point is (1,2)(1, 2) and indeed 531=25 - 3\cdot 1 = 2.

The Residual Is Perpendicular

The frozen picture below shows bp=e=(1,2,1)\mathbf{b} - \mathbf{p} = \mathbf{e} = (1, -2, 1) for the default preset, and ATe=0A^T\mathbf{e} = \mathbf{0}.

The residual is the part of b\mathbf{b} the columns cannot reach, and it is perpendicular to everything they can.
b600p52−1=e1−21,Aᵀ2×3111012e1−21=Aᵀe00
The residual, frozen

b − p = (1, −2, 1), of length √6, and Aᵀe = 0: the error is perpendicular to both columns. |b|² = 36 = 30 + 6.

Perpendicularity is the defining property of the projection, and it is what makes p\mathbf{p} the closest point: for any other point AxA\mathbf{x} of the column space, bAx=e+(pAx)\mathbf{b} - A\mathbf{x} = \mathbf{e} + (\mathbf{p} - A\mathbf{x}) with the two parts perpendicular, so by Pythagoras its length exceeds e\|\mathbf{e}\|.

The residual lives in the left null space of AA, the orthogonal complement of the column space. Its length is the minimum of Axb\|A\mathbf{x} - \mathbf{b}\|, and its square, 66 here, is the sum of squared errors that the fitted line minimizes.

The Projection Matrix

The frozen picture below shows P=A(ATA)1ATP = A(A^TA)^{-1}A^T for the default preset, a 3×33 \times 3 matrix of sixths, applied to b\mathbf{b} to give p=(5,2,1)\mathbf{p} = (5, 2, -1) directly.

PP projects every vector of R3\mathbb{R}^3 onto the plane spanned by the columns of AA.
P3×35/61/3−1/61/31/31/3−1/61/35/6b600=P b52−1
Projection matrix, frozen

P = A(AᵀA)⁻¹Aᵀ, a matrix of sixths, applied to b gives p = (5, 2, −1) at once. Symmetric, P² = P, trace 2.

PP is symmetric, because projection is its own transpose, and idempotent, P2=PP^2 = P, because projecting a vector already in the plane leaves it alone. Its eigenvalues are 11 on the column space and 00 on the left null space, so its trace, the sum of the eigenvalues, is the dimension of the column space, 22.

IPI - P is the other projection, onto the left null space, and (IP)b=e(I - P)\mathbf{b} = \mathbf{e}. The two together are the orthogonal decomposition of Rm\mathbb{R}^m that the four-subspace picture promises.

Projection onto a Line

With a single column a\mathbf{a} the normal equation is the scalar aTax^=aTb\mathbf{a}^T\mathbf{a}\,\hat{x} = \mathbf{a}^T\mathbf{b}. The frozen picture below shows the one-column preset, a=(1,2,2)\mathbf{a} = (1, 2, 2) and b=(3,3,3)\mathbf{b} = (3, 3, 3): 9x^=159\hat{x} = 15, so x^=5/3\hat{x} = 5/3.

The projection is p=53a\mathbf{p} = \frac{5}{3}\mathbf{a} and the residual (4/3,1/3,1/3)(4/3, -1/3, -1/3) is perpendicular to a\mathbf{a}.
[AᵀA | Aᵀb]1×2915rref1×215/35/3
Projection onto a line, frozen

One column a = (1, 2, 2), b = (3, 3, 3): the normal equation is 9x̂ = 15, x̂ = 5/3. The vector-projection formula aᵀb / aᵀa in matrix clothing.

This is the vector-projection formula p=abaaa\mathbf{p} = \frac{\mathbf{a}\cdot\mathbf{b}}{\mathbf{a}\cdot\mathbf{a}}\mathbf{a}, and the projection matrix is P=aaT/aTaP = \mathbf{a}\mathbf{a}^T / \mathbf{a}^T\mathbf{a}, a rank-one matrix. Everything in the general case is this formula with a matrix in place of the vector: (ATA)1(A^TA)^{-1} replaces division by aTa\mathbf{a}^T\mathbf{a}.

When the columns of AA are orthogonal, the general projection is just the sum of the projections onto each column separately; that is why orthonormal bases make least squares trivial, and why Gram–Schmidt is worth doing first.

When the Columns Are Dependent

The dependent preset has columns (1,2,3)(1, 2, 3) and (2,4,6)(2, 4, 6), one twice the other. The frozen picture below shows ATA=(14282856)A^TA = \begin{pmatrix} 14 & 28 \\ 28 & 56 \end{pmatrix} reducing to a single pivot: singular, and the run stops.

The projection of b\mathbf{b} onto the column space, a line, still exists; a unique x^\hat{\mathbf{x}} does not.
AᵀA2×214282856rref2×21200
Dependent columns, frozen

Columns (1, 2, 3) and (2, 4, 6): AᵀA = [14, 28; 28, 56] has one pivot. The projection exists but x̂ is not unique, and the run stops.

ATAA^TA is invertible exactly when AA has independent columns, because ATAx=0A^TA\mathbf{x} = \mathbf{0} implies Ax2=xTATAx=0\|A\mathbf{x}\|^2 = \mathbf{x}^TA^TA\mathbf{x} = 0, so Ax=0A\mathbf{x} = \mathbf{0}, and the two matrices have the same null space. With dependent columns many x^\hat{\mathbf{x}} give the same p\mathbf{p}, differing by null-space vectors.

The practical fixes are to drop a redundant column, or to use the pseudoinverse, which chooses the x^\hat{\mathbf{x}} of smallest length. In statistics the situation is called collinearity, and the nearly-dependent case is the more common and more dangerous one.

Reading the Scene Player

Each scene shows the matrices for one step with the arithmetic in the caption.

b\mathbf{b} is amber throughout, in the matrices and in the input grid
x^\hat{\mathbf{x}} is accent, p\mathbf{p} is the target, and e\mathbf{e} is secondary
• Products that must come out zero, such as ATeA^T\mathbf{e}, are drawn in grey with the target highlight
• In the consistency scene the pivot that lands in the last column is muted, since it is the mark of an inconsistent system
• The projection matrix's diagonal is highlighted; its trace is the rank of AA
• Fractions appear as fractions: the normal equations of integer problems have rational solutions

Choosing a Problem

The seven presets each make a different point.

Line through 3 points(0,6)(0, 6), (1,0)(1, 0), (2,0)(2, 0); the best line is b=53tb = 5 - 3t with errors (1,2,1)(1, -2, 1), the classic textbook example
Line through 4 points — four data points, one more equation, the same procedure
Parabola through 4 points — three unknowns C,D,EC, D, E and a design matrix with columns 1,t,t21, t, t^2
Projection onto a line — a single column a\mathbf{a}, where the normal equation is the scalar aTax^=aTb\mathbf{a}^T\mathbf{a}\,\hat{x} = \mathbf{a}^T\mathbf{b}
Projection onto a plane — columns e1,e2\mathbf{e}_1, \mathbf{e}_2; the projection keeps the first two coordinates and the residual is the third
b in the column space — a consistent system; least squares returns the exact solution and e=0\mathbf{e} = \mathbf{0}
Dependent columnsATAA^TA is singular and the run stops

Shuffle keeps the design-matrix structure and randomizes the data, so the fitted-curve reading stays available.

What Least Squares Is

A system Ax=bA\mathbf{x} = \mathbf{b} with more equations than unknowns is usually inconsistent: b\mathbf{b} is not in the column space of AA, so no x\mathbf{x} makes AxA\mathbf{x} equal to b\mathbf{b}. Least squares replaces the impossible demand with a possible one: choose x^\hat{\mathbf{x}} to make the error e=bAx^\mathbf{e} = \mathbf{b} - A\hat{\mathbf{x}} as short as possible.

The shortest error is geometric. The set of all AxA\mathbf{x} is the column space, and the point of a subspace closest to b\mathbf{b} is the orthogonal projection p\mathbf{p} of b\mathbf{b} onto it. So Ax^=pA\hat{\mathbf{x}} = \mathbf{p}, and the error e=bp\mathbf{e} = \mathbf{b} - \mathbf{p} is perpendicular to the whole column space, in particular to each column:

AT(bAx^)=0ATAx^=ATbA^T(\mathbf{b} - A\hat{\mathbf{x}}) = \mathbf{0} \qquad\Longleftrightarrow\qquad A^TA\hat{\mathbf{x}} = A^T\mathbf{b}


These are the normal equations. They are always consistent, because ATbA^T\mathbf{b} is always in the column space of ATAA^TA, and when the columns of AA are independent ATAA^TA is invertible and the solution is unique:

x^=(ATA)1ATb,p=A(ATA)1ATb=Pb\hat{\mathbf{x}} = (A^TA)^{-1}A^T\mathbf{b}, \qquad \mathbf{p} = A(A^TA)^{-1}A^T\mathbf{b} = P\mathbf{b}


The matrix PP projects every vector of Rm\mathbb{R}^m onto the column space. It is symmetric, satisfies P2=PP^2 = P, and IPI - P projects onto the orthogonal complement, the left null space, where the residual lives.

Fitting a line b=C+Dtb = C + Dt to data points (ti,bi)(t_i, b_i) is exactly this problem with AA having a column of ones and a column of the tit_i: each data point is one equation C+Dti=biC + Dt_i = b_i, the column space is the set of all lines evaluated at the tit_i, and the least-squares line minimizes the sum of squared vertical errors. Polynomial fits use more columns; the picture is the same. For the theory, see the least squares page and the projections page.

Key Properties

Facts about least squares and projection.

Existence: the normal equations always have a solution; uniqueness needs independent columns
Residual orthogonality: ATe=0A^T\mathbf{e} = \mathbf{0}, so e\mathbf{e} lies in the left null space of AA
Pythagoras: b2=p2+e2\|\mathbf{b}\|^2 = \|\mathbf{p}\|^2 + \|\mathbf{e}\|^2
Exact case: if bC(A)\mathbf{b} \in C(A) then x^\hat{\mathbf{x}} solves Ax=bA\mathbf{x} = \mathbf{b} exactly and e=0\mathbf{e} = \mathbf{0}
Projection matrix: P=A(ATA)1ATP = A(A^TA)^{-1}A^T is symmetric, P2=PP^2 = P, rankP=trP=n\operatorname{rank} P = \operatorname{tr} P = n, and IPI - P is the complementary projection
Single column: x^=aTb/aTa\hat{x} = \mathbf{a}^T\mathbf{b} / \mathbf{a}^T\mathbf{a} and P=aaT/aTaP = \mathbf{a}\mathbf{a}^T / \mathbf{a}^T\mathbf{a}
Orthonormal columns: if ATA=IA^TA = I then x^=ATb\hat{\mathbf{x}} = A^T\mathbf{b} and P=AATP = AA^T, with no inverse to compute
Line fits: with a column of ones, the errors sum to zero and the fitted line passes through the mean point (tˉ,bˉ)(\bar t, \bar b)
Conditioning: ATAA^TA squares the condition number of AA; QR or the SVD solve the same problem more stably
Dependent columns: p\mathbf{p} is still unique, but x^\hat{\mathbf{x}} is not; the pseudoinverse picks the shortest

Why It Matters

Least squares is the most used computation in applied mathematics.

Regression: every linear regression is a least-squares projection of the response onto the span of the predictors
Curve fitting and calibration: polynomial, exponential (after a log) and trigonometric fits are all design-matrix problems
Measurement and surveying: more measurements than unknowns, reconciled by minimizing squared discrepancies; Gauss invented the method for exactly this
Signal processing: filters and predictors are chosen by least squares; the projection theorem is the orthogonality principle
Machine learning: the closed-form solution for linear models, and the inner loop of many iterative methods
Geometry: the projection onto a subspace is the nearest-point map, the foundation of Gram–Schmidt, QR and the four-subspace picture
Statistics: with independent errors of equal variance, the least-squares estimate is the best linear unbiased one

Worked Example

Fit a line b=C+Dtb = C + Dt to the points (0,6)(0, 6), (1,0)(1, 0), (2,0)(2, 0).

Set up. Each point gives one equation: C=6C = 6, C+D=0C + D = 0, C+2D=0C + 2D = 0. So

A=(101112),b=(600)A = \begin{pmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{pmatrix}, \qquad \mathbf{b} = \begin{pmatrix} 6 \\ 0 \\ 0 \end{pmatrix}


The three equations are inconsistent: no line passes through all three points.

Normal equations. ATA=(3335)A^TA = \begin{pmatrix} 3 & 3 \\ 3 & 5 \end{pmatrix} and ATb=(60)A^T\mathbf{b} = \begin{pmatrix} 6 \\ 0 \end{pmatrix}, so 3C+3D=63C + 3D = 6 and 3C+5D=03C + 5D = 0.

Solve. Subtracting, 2D=62D = -6, so D=3D = -3 and C=5C = 5. The best line is b=53tb = 5 - 3t.

Project. p=Ax^=(5,2,1)\mathbf{p} = A\hat{\mathbf{x}} = (5, 2, -1), the heights of the line at t=0,1,2t = 0, 1, 2.

Residual. e=bp=(1,2,1)\mathbf{e} = \mathbf{b} - \mathbf{p} = (1, -2, 1), with e2=6\|\mathbf{e}\|^2 = 6. Check: (1,1,1)e=0(1, 1, 1)\cdot\mathbf{e} = 0 and (0,1,2)e=0(0, 1, 2)\cdot\mathbf{e} = 0, so ATe=0A^T\mathbf{e} = \mathbf{0}. And b2=36=30+6=p2+e2\|\mathbf{b}\|^2 = 36 = 30 + 6 = \|\mathbf{p}\|^2 + \|\mathbf{e}\|^2.

Projection matrix. (ATA)1=16(5333)(A^TA)^{-1} = \frac{1}{6}\begin{pmatrix} 5 & -3 \\ -3 & 3 \end{pmatrix}, so P=A(ATA)1AT=16(521222125)P = A(A^TA)^{-1}A^T = \frac{1}{6}\begin{pmatrix} 5 & 2 & -1 \\ 2 & 2 & 2 \\ -1 & 2 & 5 \end{pmatrix}, and Pb=(5,2,1)=pP\mathbf{b} = (5, 2, -1) = \mathbf{p}. Its trace is 22, the rank of AA.

Common Mistakes

A few mistakes recur.

Solving Ax=bA\mathbf{x} = \mathbf{b} by row reduction and reading a "solution" — an inconsistent system has none; the least-squares solution comes from the normal equations, not from [Ab][A \mid \mathbf{b}]
Writing AATAA^T instead of ATAA^TA — the normal equations use ATAA^TA, which is n×nn \times n and matches the number of unknowns
Forgetting to multiply b\mathbf{b} by ATA^T — the right-hand side of the normal equations is ATbA^T\mathbf{b}, not b\mathbf{b}
Expecting x^\hat{\mathbf{x}} to satisfy the original equations — it satisfies them only when b\mathbf{b} is in the column space; otherwise Ax^=pbA\hat{\mathbf{x}} = \mathbf{p} \neq \mathbf{b}
Measuring errors perpendicular to the line — least squares for b=C+Dtb = C + Dt minimizes vertical errors in bb, not distances to the line
Using dependent columnsATAA^TA is then singular and x^\hat{\mathbf{x}} is not unique, though p\mathbf{p} still is
Computing (ATA)1(A^TA)^{-1} when the columns are orthonormal — then ATA=IA^TA = I and the inverse is unnecessary