- The projection p lies on the line, so p = x̂a for some number x̂.
- The error e = b − p is perpendicular to a: aᵀ(b − x̂a) = 0.
- Solving for the number: x̂ = aᵀb / aᵀa.
- If b is on the line, p = b. If b is perpendicular to a, p = 0.
The closest point
Another everyday picture: you stand in a field next to a straight road. To reach the road in the fewest steps, you walk straight at it, at a right angle. The road is the line through a, you are at b, and where you arrive is p. Your path e = b − p is perpendicular to the road.
From the right angle to a formula
- p is on the line, so p = x̂a for some number x̂ ("x-hat": the best choice).
- e = b − x̂a is perpendicular to a, so its dot product with a is zero.
Two dot products and one division, in any dimension.
A worked example
Why p is the closest point
Take any other point q on the line. The path from b to q splits into two pieces: b − q = e + (p − q). First the drop e, then the step p − q along the line. Those two pieces are perpendicular, so by Pythagoras ‖b − q‖² = ‖e‖² + ‖p − q‖². That is bigger than ‖e‖² unless q = p. So q is farther than p. Try it with q = 2a = 42:
Two quick checks
If b is already on the line, the stick lies flat and is its own shadow: e = 0 and p = b. If b is perpendicular to a, the stick stands straight up and its shadow is a single point: aᵀb = 0, so p = 0.
Keep reading with the full course
Finish lesson 5 and you will be able to: fit the best line to noisy sensor data.
Still locked here:
- The rest of this explanation
- 4 more examples with step-by-step solutions
- Five extra members-only problems with worked solutions
$20 once, lifetime access. Lessons 1 to 3 stay free.
Examples
Example 1 · Onto the diagonal
Project b = 31 onto the line through a = 11.

Questions and comments
Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.