The Preset pills load seven problems: lines through 3 and 4 points, a parabola through 4 points, projection onto a line and onto a plane, b in the column space, and dependent columns. Learn more about choosing a problem
The Size steppers (▲ / ▼) set 2 to 4 equations and 1 to 3 unknowns; Shuffle data builds a polynomial design matrix with random data. Learn more about getting started
▶ Play runs the procedure, Next → and ← Back step one scene at a time, Reset returns to the start, and the speed menu sets the pace. Learn more about the controls
The scenes run in a fixed order: consistency check, normal equations, solve for x^, projection p, residual e, projection matrix P, and the fitted curve. Learn more about the scenes in order
b stays amber, x^ is the accent, and products that must come out zero, such as ATe, are grey; the Step explanations log keeps every stage. Learn more about reading a scene
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.
Choose a matrix and a right-hand side, then watch 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 A or of b directly; 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 A 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
DemoLine through 3 points, scene by scene
Step 0 of 5
The visualizer follows the textbook procedure exactly.
• Consistency — [A∣b] row reduced; a pivot in the last column means no exact solution • Normal equations — ATA and ATb formed as products, with their meaning as dot products • Dependent columns — if ATA is singular the run stops with an explanation • Solve — [ATA∣ATb] row reduced to x^; for a single column this is aTb/aTa • Project — p=Ax^ as a combination of the columns, compared with b • Residual — e=b−p, its length, ATe=0, and the Pythagorean split of ∥b∥2 • Projection matrix — P=A(ATA)−1AT with Pb=p, P2=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[A∣b] reduced, with a pivot in the last column: one row reads 0=1, so no line passes through the three points.
That failure is what least squares is for.
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 is not a combination of the columns of A: it lies outside the column space, which here is a plane in R3. 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) and ATb=(6,0) for the default preset, formed as the two products AT⋅A and AT⋅b.
Each entry is a dot product of columns: the 3 in the corner is the column of ones dotted with itself, the count of data points.
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 AT 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 t.
ATA 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 ATA nearly singular.
Solving for x-hat
The frozen picture below shows [ATA∣ATb] reduced to x^=(5,−3): the best line is b=5−3t.
Two equations, two unknowns, and a unique answer, unlike the original three equations.
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×2 inverse formula, or Cramer's rule. For larger problems the standard practice is to avoid forming ATA at all and use a QR factorization of A, which gives the same x^ with less loss of precision.
The solution has a statistical reading. With a column of ones, D is the covariance of t and b divided by the variance of t, and C makes the line pass through the mean point. Here the mean point is (1,2) and indeed 5−3⋅1=2.
The Residual Is Perpendicular
The frozen picture below shows b−p=e=(1,−2,1) for the default preset, and ATe=0.
The residual is the part of b the columns cannot reach, and it is perpendicular to everything they can.
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 the closest point: for any other point Ax of the column space, b−Ax=e+(p−Ax) with the two parts perpendicular, so by Pythagoras its length exceeds ∥e∥.
The residual lives in the left null space of A, the orthogonal complement of the column space. Its length is the minimum of ∥Ax−b∥, and its square, 6 here, is the sum of squared errors that the fitted line minimizes.
The Projection Matrix
The frozen picture below shows P=A(ATA)−1AT for the default preset, a 3×3 matrix of sixths, applied to b to give p=(5,2,−1) directly.
P projects every vector of R3 onto the plane spanned by the columns of A.
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.
P is symmetric, because projection is its own transpose, and idempotent, P2=P, because projecting a vector already in the plane leaves it alone. Its eigenvalues are 1 on the column space and 0 on the left null space, so its trace, the sum of the eigenvalues, is the dimension of the column space, 2.
I−P is the other projection, onto the left null space, and (I−P)b=e. The two together are the orthogonal decomposition of Rm that the four-subspace picture promises.
Projection onto a Line
With a single column a the normal equation is the scalaraTax^=aTb. The frozen picture below shows the one-column preset, a=(1,2,2) and b=(3,3,3): 9x^=15, so x^=5/3.
The projection is p=35a and the residual (4/3,−1/3,−1/3) is perpendicular to a.
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=a⋅aa⋅ba, and the projection matrix is P=aaT/aTa, a rank-one matrix. Everything in the general case is this formula with a matrix in place of the vector: (ATA)−1 replaces division by aTa.
When the columns of A 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) and (2,4,6), one twice the other. The frozen picture below shows ATA=(14282856) reducing to a single pivot: singular, and the run stops.
The projection of b onto the column space, a line, still exists; a unique x^ does not.
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.
ATA is invertible exactly when A has independent columns, because ATAx=0 implies ∥Ax∥2=xTATAx=0, so Ax=0, and the two matrices have the same null space. With dependent columns many x^ give the same p, differing by null-space vectors.
The practical fixes are to drop a redundant column, or to use the pseudoinverse, which chooses the 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 is amber throughout, in the matrices and in the input grid • x^ is accent, p is the target, and e is secondary • Products that must come out zero, such as ATe, 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 A • Fractions appear as fractions: the normal equations of integer problems have rational solutions
Choosing a Problem
DemoFive presets
Step 0 of 5
The seven presets each make a different point.
• Line through 3 points — (0,6), (1,0), (2,0); the best line is b=5−3t with errors (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,E and a design matrix with columns 1,t,t2 • Projection onto a line — a single column a, where the normal equation is the scalar aTax^=aTb • Projection onto a plane — columns e1,e2; 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 • Dependent columns — ATA 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=b with more equations than unknowns is usually inconsistent: b is not in the column space of A, so no x makes Ax equal to b. Least squares replaces the impossible demand with a possible one: choose x^ to make the error e=b−Ax^ as short as possible.
The shortest error is geometric. The set of all Ax is the column space, and the point of a subspace closest to b is the orthogonal projection p of b onto it. So Ax^=p, and the error e=b−p is perpendicular to the whole column space, in particular to each column:
AT(b−Ax^)=0⟺ATAx^=ATb
These are the normal equations. They are always consistent, because ATb is always in the column space of ATA, and when the columns of A are independent ATA is invertible and the solution is unique:
x^=(ATA)−1ATb,p=A(ATA)−1ATb=Pb
The matrix P projects every vector of Rm onto the column space. It is symmetric, satisfies P2=P, and I−P projects onto the orthogonal complement, the left null space, where the residual lives.
Fitting a line b=C+Dt to data points (ti,bi) is exactly this problem with A having a column of ones and a column of the ti: each data point is one equation C+Dti=bi, the column space is the set of all lines evaluated at the ti, 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=0, so e lies in the left null space of A • Pythagoras: ∥b∥2=∥p∥2+∥e∥2 • Exact case: if b∈C(A) then x^ solves Ax=b exactly and e=0 • Projection matrix: P=A(ATA)−1AT is symmetric, P2=P, rankP=trP=n, and I−P is the complementary projection • Single column: x^=aTb/aTa and P=aaT/aTa • Orthonormal columns: if ATA=I then x^=ATb and P=AAT, 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ˉ) • Conditioning: ATA squares the condition number of A; QR or the SVD solve the same problem more stably • Dependent columns: p is still unique, but 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+Dt to the points (0,6), (1,0), (2,0).
Set up. Each point gives one equation: C=6, C+D=0, C+2D=0. So
A=111012,b=600
The three equations are inconsistent: no line passes through all three points.
Normal equations.ATA=(3335) and ATb=(60), so 3C+3D=6 and 3C+5D=0.
Solve. Subtracting, 2D=−6, so D=−3 and C=5. The best line is b=5−3t.
Project.p=Ax^=(5,2,−1), the heights of the line at t=0,1,2.
Residual.e=b−p=(1,−2,1), with ∥e∥2=6. Check: (1,1,1)⋅e=0 and (0,1,2)⋅e=0, so ATe=0. And ∥b∥2=36=30+6=∥p∥2+∥e∥2.
Projection matrix.(ATA)−1=61(5−3−33), so P=A(ATA)−1AT=6152−1222−125, and Pb=(5,2,−1)=p. Its trace is 2, the rank of A.
Common Mistakes
A few mistakes recur.
• Solving Ax=b by row reduction and reading a "solution" — an inconsistent system has none; the least-squares solution comes from the normal equations, not from [A∣b] • Writing AAT instead of ATA — the normal equations use ATA, which is n×n and matches the number of unknowns • Forgetting to multiply b by AT — the right-hand side of the normal equations is ATb, not b • Expecting x^ to satisfy the original equations — it satisfies them only when b is in the column space; otherwise Ax^=p=b • Measuring errors perpendicular to the line — least squares for b=C+Dt minimizes vertical errors in b, not distances to the line • Using dependent columns — ATA is then singular and x^ is not unique, though p still is • Computing (ATA)−1 when the columns are orthonormal — then ATA=I and the inverse is unnecessary
Related Concepts
Vector projection — the single-column case, p=(aTb/aTa)a; the projection tool shows it in R2.