Linear Algebra · Orthogonal projection

Orthogonal projection

The orthogonal projection of a vector b onto a subspace is the point p of the subspace closest to b. The error e = b − p is at right angles to the subspace, and every other point q of it is farther away: |b − q|² = |e|² + |p − q|².

Orthogonal projection in this visualization

Orthogonal projection finds the point of a subspace that is closest to a given vector. Here the subspace SS is a plane or a line through the origin O, and the vector is b⃗\vec b. Its projection p⃗\vec p lies in SS, and the error e⃗=b⃗−p⃗\vec e = \vec b - \vec p meets SS at a right angle. You can move b⃗\vec b and reshape SS, compare p⃗\vec p with any other point of SS, and see when the shadows of b⃗\vec b on two axes add up to p⃗\vec p.

Why does the right angle give the closest point? Take any other point qq in SS. The segment from qq to p⃗\vec p lies in SS, so it is perpendicular to e⃗\vec e. The points b⃗\vec b, p⃗\vec p and qq form a right triangle with the right angle at p⃗\vec p, and Pythagoras gives

Every other point of S is farther away
∣b⃗−q∣2=∣e⃗∣2+∣p⃗−q∣2|\vec b - q|^2 = |\vec e|^2 + |\vec p - q|^2

So b⃗\vec b is farther from qq than from p⃗\vec p, unless qq is p⃗\vec p itself. It works the other way too. If e⃗\vec e leaned toward some direction in SS, moving p⃗\vec p slightly that way would bring it closer to b⃗\vec b. In a plane, then, the closest point is the one where e⃗\vec e is perpendicular to both u⃗\vec u and w⃗\vec w. On a line, being perpendicular to u⃗\vec u is enough.

Finding p⃗\vec p comes down to these right angles. On a line, p⃗\vec p is a multiple of u⃗\vec u, and the condition e⃗⋅u⃗=0\vec e\cdot\vec u = 0 fixes the multiple:

Projection onto a line
p⃗=u⃗⋅b⃗u⃗⋅u⃗ u⃗\vec p = \frac{\vec u\cdot\vec b}{\vec u\cdot\vec u}\,\vec u

In a plane, write p⃗=c1u⃗+c2w⃗\vec p = c_1\vec u + c_2\vec w. The two conditions e⃗⋅u⃗=0\vec e\cdot\vec u = 0 and e⃗⋅w⃗=0\vec e\cdot\vec w = 0 become two linear equations for c1c_1 and c2c_2, called the normal equations:

The normal equations
(u⃗⋅u⃗) c1+(u⃗⋅w⃗) c2=u⃗⋅b⃗,(w⃗⋅u⃗) c1+(w⃗⋅w⃗) c2=w⃗⋅b⃗(\vec u\cdot\vec u)\,c_1 + (\vec u\cdot\vec w)\,c_2 = \vec u\cdot\vec b,\qquad (\vec w\cdot\vec u)\,c_1 + (\vec w\cdot\vec w)\,c_2 = \vec w\cdot\vec b

The readout gives p⃗\vec p both as coordinates and as a combination of u⃗\vec u and w⃗\vec w. Drag u⃗\vec u and w⃗\vec w around within the same plane and the coefficients change, but p⃗\vec p does not. The projection depends only on the subspace, not on the vectors used to describe it.

When u⃗\vec u and w⃗\vec w are perpendicular, the two equations come apart. Each coefficient then depends on one axis only, and p⃗\vec p is simply the sum of the shadows of b⃗\vec b on the two axes:

Only on perpendicular axes
p⃗=b⃗⋅u⃗u⃗⋅u⃗ u⃗+b⃗⋅w⃗w⃗⋅w⃗ w⃗\vec p = \frac{\vec b\cdot\vec u}{\vec u\cdot\vec u}\,\vec u + \frac{\vec b\cdot\vec w}{\vec w\cdot\vec w}\,\vec w

On slanted axes this shortcut usually fails. Each shadow is taken as if the other axis were not there, so their sum ss usually lands away from p⃗\vec p. When it does, the readout shows b⃗−s\vec b - s missing at least one of the two right angles. Make w ⟂ u replaces w⃗\vec w with its part perpendicular to u⃗\vec u, which is one step of the Gram–Schmidt process. The plane is still the same plane, but now its two axes are perpendicular, and the shadows add up to p⃗\vec p for every b⃗\vec b.

The diagonal line through (1,1,1)(1, 1, 1) gives a familiar example. Projecting b⃗\vec b onto it replaces every entry of b⃗\vec b with their mean, and ∣e⃗∣2|\vec e|^2 is the sum of squared deviations from that mean. Least-squares regression uses the same kind of projection.

Controls

  • The dot on b⃗\vec b moves it parallel to SS, and p⃗\vec p follows. The small ring beyond the dot moves b⃗\vec b at right angles to SS, and p⃗\vec p stays put.
  • With Plane chosen, u⃗\vec u and w⃗\vec w can only be dragged within their plane. To change the plane, type new coordinates or use the value keys.

Related