Hexa Scholar

The dot product and the closest point on a line

Which point of a line through 0 is closest to a given point \(b\), and how can you find it without measuring?

Stage 1 asks you to place that point by eye before the page computes anything. The tool that finds it exactly is the dot product \(u \cdot v = u_1v_1 + u_2v_2\), a single number made from two vectors. Stage 3 proves where the closest point is and why there is only one.

Before you start · 3 quick checks on vector arithmetic, length and lines through 0, about 2 min
Setup: the dot product, length and lines through 0

As on the matrix-as-map page, a vector \(x = (x_1, x_2)\) in \(\mathbb{R}^2\) is a point of the plane, or the arrow from 0 to that point.

The dot product of \(u = (u_1, u_2)\) and \(v = (v_1, v_2)\) is the number \(u \cdot v = u_1v_1 + u_2v_2\). The length of \(v\) is \(|v| = \sqrt{v_1^2 + v_2^2} = \sqrt{v \cdot v}\), so \(|v|^2 = v \cdot v\), and the distance between two points \(x\) and \(y\) is \(|x - y|\).

For all vectors \(u, v, w\) and every real number \(c\): \(u \cdot v = v \cdot u\), \((u + w) \cdot v = u \cdot v + w \cdot v\), \((cu) \cdot v = c\,(u \cdot v)\), and \(v \cdot v \ge 0\), with \(v \cdot v = 0\) only for \(v = 0\).

Two vectors \(u\) and \(v\) are orthogonal when \(u \cdot v = 0\). For a vector \(a \ne 0\), the line through 0 along \(a\) is \(L = \{ta : t \text{ a real number}\}\), the set of all multiples of \(a\).

Why a dot product of 0 means a right angle

Take nonzero vectors \(u\) and \(v\). By the rules above,

\[ \begin{aligned} |u - v|^2 &= (u - v) \cdot (u - v) \\ &= |u|^2 - 2\,u \cdot v + |v|^2. \end{aligned} \]

The triangle with corners 0, \(u\) and \(v\) has sides \(|u|\), \(|v|\) and \(|u - v|\). Pythagoras and its converse say that the angle at 0 is a right angle exactly when \(|u - v|^2 = |u|^2 + |v|^2\), that is, exactly when \(u \cdot v = 0\). (If \(u\) and \(v\) lie on one line through 0, the angle is \(0^\circ\) or \(180^\circ\), and \(u \cdot v = \pm|u|\,|v| \ne 0\).) The zero vector is orthogonal to every vector, by the definition.

1 See

Find the point of the line closest to b

Each goal gives a line \(L\) through 0 and a point \(b\) off the line. Move your mark along \(L\) to where you think \(L\) comes closest to \(b\): drag the dot, focus it and use the arrow keys, or type \(t\). Then press Place mark. Only then does the page draw the closest point and give the distances.

How the mark moves and how it is scored
2 Try

What the closest point depends on

Two questions first. The explorer below opens when you have answered both.

The explorer opens when you have answered the two questions above.

3 Prove

Why the closest point is p = t̂a

A straight pole leans up and to the right from a small round peg on a flat road. The sun is straight overhead and its rays fall vertically. A dashed vertical line drops from the tip of the pole to the road and meets it at a right angle, marked with a small square. The shadow of the pole runs along the road from the peg to the foot of the dashed line.
A pole leans from the point 0 on a straight road. The road represents the line \(L\), and the tip of the pole is the point \(b\). With the sun straight overhead, the shadow of the pole ends at the point of the road right below the tip, and that point is \(p\). The dashed ray from the tip meets the road at a right angle.
Theorem

Let \(a \ne 0\) and \(b\) be vectors, and let \(L\) be the line through 0 along \(a\). Put

\[ \hat t = \frac{a \cdot b}{a \cdot a}, \qquad p = \hat t a. \]

Then \((b - p) \cdot a = 0\), and for every real number \(t\),

\[ |b - ta|^2 = |b - p|^2 + (\hat t - t)^2|a|^2. \]

So \(p\) is the only point of \(L\) closest to \(b\). The vector \(b\) may be anything, including 0, a point of \(L\) or a vector orthogonal to \(a\).

Build the proof one line at a time; each line has one correct choice. A wrong choice is a statement that is false for these \(a\) and \(b\), or a claimed general rule that fails for some vectors.

What follows

The orthogonal projection

The point \(p = \dfrac{a \cdot b}{a \cdot a}\,a\) is called the orthogonal projection of \(b\) onto \(L\). It splits \(b = p + (b - p)\) into a part along \(L\) and a part orthogonal to \(L\). If \(|a| = 1\), then \(a \cdot a = 1\) and the formula is simply \(p = (a \cdot b)\,a\).

Pythagoras, and why |p| ≤ |b|

Take \(t = 0\) in the theorem:

\[ \begin{aligned} |b|^2 &= |b - p|^2 + \hat t^2|a|^2 \\ &= |b - p|^2 + |p|^2. \end{aligned} \]

So \(|p| \le |b|\), with equality exactly when \(b - p = 0\), that is, when \(b\) lies on \(L\).

The angle between two vectors

Let \(a\) and \(b\) be nonzero, and let \(\theta\), between \(0^\circ\) and \(180^\circ\), be the angle between them. Then

\[ a \cdot b = |a|\,|b|\cos\theta. \]

The right triangle with corners 0, \(p\) and \(b\) has its right angle at \(p\) and its long side \(|b|\), so \(|p| = |b|\,|\cos\theta|\); and \(p\) is on the side of \(a\) exactly when \(\theta \lt 90^\circ\). Since \(p = \hat t a\), this says \(\hat t\,|a| = |b|\cos\theta\), and multiplying by \(|a|\) gives the formula. (The triangle is flat when \(b\) lies on \(L\), and \(p = 0\) when \(\theta = 90^\circ\); the formula holds in both cases.) As \(|\cos\theta| \le 1\), it also gives \(|a \cdot b| \le |a|\,|b|\), the Cauchy–Schwarz inequality.

Projecting is a linear map

The map \(b \mapsto \dfrac{a \cdot b}{a \cdot a}\,a\) is linear, by the rules for sums and multiples, so it is a 2×2 matrix, as on the matrix-as-map page. It sends \(a\) to \(a\) and every vector orthogonal to \(a\) to 0. So \(a\) and every nonzero vector orthogonal to \(a\) are eigenvectors, with the eigenvalues 1 and 0, and the matrix can be diagonalized with them. Projecting twice changes nothing: the projection of \(p\) is \(p\) itself.

More entries

For vectors with \(n\) entries, \(u \cdot v = u_1v_1 + u_2v_2 + \cdots + u_nv_n\) and \(|v| = \sqrt{v \cdot v}\). The rules of the setup still hold, and \(a \cdot a = a_1^2 + \cdots + a_n^2 \gt 0\) for \(a \ne 0\). The proof above uses nothing else, so the theorem holds with the same proof.

4 Apply

Problems

Each problem has hints and a full solution. Exact answers are graded exactly: type whole numbers, exact decimals such as 0.5 or fractions such as 1/2, without thousands separators, and separate the numbers with commas where a problem asks for several. A value that is only close to an exact answer is not accepted. Where an answer is rounded (P5), the accepted error is stated.

Key point To project \(b\) onto the line along \(a\), compute two dot products and divide: \(p = \dfrac{a \cdot b}{a \cdot a}\,a\). The error \(b - p\) is orthogonal to \(a\), its length is the distance from \(b\) to the line, and the same formula works with any number of entries.
Level Intro Prerequisites A matrix is a function that moves the plane Time ~35 min

Related: Eigenvectors: the directions a matrix only stretches and Diagonalization: what happens when you apply A again and again.

Further reading