← Visualization Hub
Orthogonality

Projection & Least Squares

Most systems \(A\mathbf x=\mathbf b\) have no exact solution — the data doesn't line up. Instead of giving up, we find the closest answer: project \(\mathbf b\) onto what \(A\) can reach, and solve there. This page builds the tools in order — orthogonal vectors, orthonormal bases, the projection matrix, and finally least squares.

1Orthogonal vectors — the dot product is zero

Perpendicular means no shared direction: \(\mathbf a\cdot\mathbf b=0\).

Two vectors are orthogonal when their dot product vanishes. Drag the tips — the angle badge turns green the instant \(\mathbf a\cdot\mathbf b\) hits 0 (a right angle).

drag the two arrowheads

readout

$$\begin{gathered} \textcolor{#5cd2ff}{\mathbf a}\cdot\textcolor{#ffe66d}{\mathbf b} =a_1b_1+a_2b_2=\lVert\mathbf a\rVert\,\lVert\mathbf b\rVert\cos\theta\\[2pt] \textcolor{#5cd2ff}{\mathbf a}\perp\textcolor{#ffe66d}{\mathbf b} \iff \textcolor{#5cd2ff}{\mathbf a}\cdot\textcolor{#ffe66d}{\mathbf b}=0 \end{gathered}$$

2Orthonormal — orthogonal and unit length

Perpendicular directions, each of length 1. The dream basis.

A set of vectors is orthonormal when they are mutually orthogonal and each has length 1 (\(\mathbf q_i\cdot\mathbf q_j=0\) for \(i\neq j\), \(\mathbf q_i\cdot\mathbf q_i=1\)). Stack them as columns of \(Q\) and something magic happens:

$$Q^{\mathsf T}Q=I\qquad\text{(so for a square }Q,\ Q^{-1}=Q^{\mathsf T}\text{ — the inverse is free)}$$
$$\text{projection onto a unit vector }\mathbf q:\qquad \textcolor{#7ee787}{\mathbf p}=(\textcolor{#5cd2ff}{\mathbf q}^{\mathsf T}\textcolor{#ffe66d}{\mathbf b})\, \textcolor{#5cd2ff}{\mathbf q}\qquad\text{(no division needed)}$$

Why care? Projections and least squares below need \((A^{\mathsf T}A)^{-1}\). If the columns are orthonormal, \(A^{\mathsf T}A=I\) and the whole thing collapses to dot products. Gram–Schmidt is the recipe that turns any independent set into an orthonormal one — see the \(A=QR\) factorization.

3Projection — the closest point in a subspace

Drop \(\mathbf b\) straight down onto the plane \(A\) can reach. The leftover is perpendicular to it.

Project \(\mathbf b\) onto a line through \(\mathbf a\): \(\mathbf p=\dfrac{\mathbf a^{\mathsf T}\mathbf b}{\mathbf a^{\mathsf T}\mathbf a}\,\mathbf a\). Onto a whole subspace (the column space of \(A\)) it becomes the projection matrix:

$$\begin{gathered} P=A\,(A^{\mathsf T}A)^{-1}A^{\mathsf T}\\[2pt] \textcolor{#7ee787}{\mathbf p}=P\,\textcolor{#ffe66d}{\mathbf b},\qquad \textcolor{#ff6b6b}{\mathbf e}=\textcolor{#ffe66d}{\mathbf b}-\textcolor{#7ee787}{\mathbf p},\qquad A^{\mathsf T}\textcolor{#ff6b6b}{\mathbf e}=\mathbf 0 \end{gathered}$$

The error \(\mathbf e\) is orthogonal to the whole column space — that is exactly what "closest" means. Here \(A\) has two columns \(\mathbf a_1,\mathbf a_2\) spanning a plane in \(\mathbb R^3\). Move \(\mathbf b\) with the sliders: its shadow \(\mathbf p\) is the nearest point on the plane, and the red error \(\mathbf e\) always meets the plane at a right angle.

drag with mouse or one finger to orbit · scroll or pinch to zoom · move b with sliders

\(\mathbf b\) in \(\mathbb R^3\)

1.0
1.5
2.5

readout

projection matrix \(P\)

4Least squares — the best answer when there is none

Fit a line to points that don't lie on any line. \(A\mathbf x=\mathbf b\) is unsolvable, so solve the projection instead.

Fit a straight line \(b=C+Dt\) to the data. Stack the points into \(A=\begin{bmatrix}1&t_1\\\vdots&\vdots\\1&t_m\end{bmatrix}\) and \(\mathbf x=\begin{bmatrix}C\\D\end{bmatrix}\). Unless the points are perfectly collinear, \(\mathbf b\) is not in the column space, so \(A\mathbf x=\mathbf b\) has no solution. Project instead: solve the normal equations, which are always solvable.

$$\begin{gathered} A^{\mathsf T}A\,\hat{\mathbf x}=A^{\mathsf T}\textcolor{#ffe66d}{\mathbf b}\\[2pt] \hat{\mathbf x}=(A^{\mathsf T}A)^{-1}A^{\mathsf T}\textcolor{#ffe66d}{\mathbf b},\qquad \textcolor{#7ee787}{\mathbf p}=A\hat{\mathbf x} \end{gathered}$$
$$\hat{\mathbf x}\ \text{minimises}\ \sum_i(\text{residual}_i)^2 =\lVert A\mathbf x-\mathbf b\rVert^2,\qquad \textcolor{#ff6b6b}{\mathbf e}=\textcolor{#ffe66d}{\mathbf b}-\textcolor{#7ee787}{\mathbf p} \perp\ \text{every column of }A$$

Drag the data points. The green best-fit line re-solves the normal equations live; the red bars are the residuals it makes as small as possible (in the squared sense).

drag the points · double-click empty space to add

best-fit line

why it works

\(\mathbf b\) lives in \(\mathbb R^m\) (one axis per data point). The line's reach is only a 2-D plane inside it. Least squares drops \(\mathbf b\) onto that plane — exactly the projection from §3.
Robotics: calibrating a sensor, fitting a motion model, or fusing noisy measurements are all overdetermined systems — more equations than unknowns, no exact fit. Least squares is the workhorse: it returns the parameter vector that makes the leftover error orthogonal to everything the model can explain.