Linear Algebra, Live
Strang §4.2 · Projections

Projecting a vector onto a line

Slide 1 was about exact right angles. Now we use one to answer a practical question: which point on a line is closest to a given point b? Picture the sun straight overhead at noon. A slanted stick casts a shadow on the flat ground, and the tip of the shadow sits right below the tip of the stick. That shadow is the projection p of b onto the line. The gap from the stick's tip down to the shadow, e = b − p, hangs straight down, at a right angle to the ground. That one right angle gives a short formula for the best multiple x̂ ("x-hat") of a: x̂ = aᵀb / aᵀa, so p = x̂a. It also makes p the closest point on the line to b.

Lesson 5 · slide 2Projection
p = x̂ a    x̂ = aᵀb / aᵀa    e = b − p ⟂ a
Key ideas
  1. The projection p lies on the line, so p = x̂a for some number x̂.
  2. The error e = b − p is perpendicular to a: aᵀ(b − x̂a) = 0.
  3. Solving for the number: x̂ = aᵀb / aᵀa.
  4. 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.
aᵀ(b − x̂a) = 0
aᵀb − x̂ aᵀa = 0
x̂ = aᵀb / aᵀa,    p = x̂a

Two dot products and one division, in any dimension.

A worked example

a = 21,   b = 13
aᵀb = 2 + 3 = 5,   aᵀa = 4 + 1 = 5,   x̂ = 1
p = 21,   e = b − p = −12
check: a · e = −2 + 2 = 0 ✓
b = 13 drops straight down onto the line through a = 21. It lands at p = 21, and the drop e = −12 meets the line at a right angle.

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:

b − q = 1 − 43 − 2 = −31,   ‖b − q‖² = 9 + 1 = 10
‖e‖² = 1 + 4 = 5,   p − q = 21 − 42 = −2−1,   ‖p − q‖² = 5,   and 10 = 5 + 5 ✓

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.

on the line: a = 21, b = 42:   x̂ = 10 / 5 = 2,   p = 42 = b
perpendicular: a = 12, b = 2−1:   aᵀb = 2 − 2 = 0,   p = 00
b = 2−1 is already at a right angle to the line through 12. Its shadow shrinks to the origin, so p = 00 and e is all of b.

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.

Show solutionHide solution
aᵀb = 4,   aᵀa = 2,   x̂ = 2
p = 22,   e = 1−1
a · e = 1 − 1 = 0 ✓
Exercise Compute the projection p of b = (4, 3) onto the line through a = (1, 2). Edit the code and press Run.
slide_2.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Questions and comments

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

The full course

From Ax = b to PCA, by running it

Lessons 1 to 3 are free. Finish all eight and you will be able to:

You get: all 8 lessons with Python labs · every worked example and solution · 5 extra problems per lesson · your progress tracked · answers to your questions · every future update.

$20 once · lifetime access
Continue to checkout →

Already a member? Log in to the course