Vector Projection onto a Plane

Interactive 3D reproduction of Figure 3.11 from Mathematics for Machine Learning (Deisenroth, Faisal & Ong, Section 3.8.2). Drag the tip of the vector x (blue) to see how its orthogonal projection onto the 2-D plane U — spanned by the basis vectors b₁ and b₂ — changes. This is exactly what the matrix pseudo-inverse B⁺ = (BᵀB)⁻¹Bᵀ does: it maps a point in ℝ³ to the coordinates of its projection, and multiplying by B recovers the projected point π_U(x) = B B⁺ x. All quantities update live as you drag.

Drag the blue tip of x to move it (constrained to the bounding box). Amber = projection π_U(x); red dashed = residual x − π_U(x), always orthogonal to the plane. Drag to rotate / scroll to zoom the scene.

Coordinates λ (of π in basis b₁,b₂)
(5, −3)
Projection π_U(x)
(5, 2, −1)
Residual x − π_U(x)
(1, −2, 1)
‖residual‖
2.449

Key Formulas (Section 3.8.2)

Let \(U = \mathrm{span}(\mathbf{b}_1, \ldots, \mathbf{b}_m)\) be an \(m\)-dimensional subspace of \(\mathbb{R}^n\), and let \(B = [\mathbf{b}_1, \ldots, \mathbf{b}_m] \in \mathbb{R}^{n \times m}\) collect the basis vectors as columns. Any projection \(\pi_U(\mathbf{x})\) of \(\mathbf{x} \in \mathbb{R}^n\) lies in \(U\), so it is a linear combination of the basis: \(\pi_U(\mathbf{x}) = B\boldsymbol{\lambda}\) with \(\boldsymbol{\lambda} \in \mathbb{R}^m\).

The coordinates \(\boldsymbol{\lambda}\) are found by solving the normal equations \(B^\top B\boldsymbol{\lambda} = B^\top \mathbf{x}\), since the residual \(\mathbf{x} - \pi_U(\mathbf{x})\) must be orthogonal to every basis vector:

Expanding gives the projection directly in terms of the pseudo-inverse \(B^+\):

where \(\mathbf{P}_\pi = B(B^\top B)^{-1}B^\top\) is the projection matrix and \(B^+ = (B^\top B)^{-1}B^\top\) is the Moore–Penrose pseudo-inverse of \(B\). The pseudo-inverse is what makes the picture in Figure 3.11 work: it "undoes" the map from coordinates to points, returning the coefficient vector of the closest point on the plane.

Numerical Example 3.11: \(\mathbf{b}_1 = (1,1,1)^\top,\ \mathbf{b}_2 = (0,1,2)^\top,\ \mathbf{x} = (6,0,0)^\top\). Then \(B^\top B = \begin{pmatrix} 3 & 3 \\ 3 & 5 \end{pmatrix}, \ \boldsymbol{\lambda} = B^+\mathbf{x} = \begin{pmatrix} 5 \\ -3 \end{pmatrix}\), and \(\pi_U(\mathbf{x}) = B\boldsymbol{\lambda} = (5, 2, -1)^\top\). The residual \((1,-2,1)^\top\) is orthogonal to both \(\mathbf{b}_1\) and \(\mathbf{b}_2\).

Comments