Math

Orthogonal Projection

The projection of u onto a line is the closest point on that line. The residual u − proj(u) is orthogonal to the line, which is the geometric heart of least squares.

6 minLevel 2/3The interactive stage stays ad-free.
u · v1.41Projection length1.41Residual0.89Angle32.3°

The cyan arrow is the closest point to u on the line. The rose residual is perpendicular to the line — that right angle is exactly what makes least squares work. Change the direction or move u to see the projection and residual update.

What you are seeing

1

The projection length is (u · v) / |v|. The projected vector is that scalar times the unit direction of v.

2

The residual r = u − p is perpendicular to v, so the triangle has a right angle at p.

3

Among all points on the line, p minimizes the distance to u. This is why least squares finds the closest fit.

Why it matters

Projection is the geometry behind regression, Fourier coefficients, signal approximation, and solving inconsistent linear systems.

Complexity & details

Projection onto a line costs O(n) for vectors in Rⁿ. Projection onto a subspace solves a normal equation.

Frequently asked questions

Why is the residual orthogonal?+

If the residual had a component along the line, moving the projection slightly would reduce the distance. Minimal distance forces the residual to be perpendicular.

What if the vector is already on the line?+

Then the projection equals the vector and the residual is zero. The point is already the closest possible point.