Orthogonality, Gram–Schmidt and Least Squares
Use orthonormal bases and orthogonal matrices, build them with Gram–Schmidt, and fit a best line to data by solving the normal equations.
Welcome back!
Step 1 of 13Learn
Why perpendicular bases are best
Finding coordinates in a general basis means solving a linear system. In an orthogonal basis, where every pair of basis vectors is perpendicular, it's just a dot product.
If u₁, …, uₙ are orthogonal, then any vector x in their span is
x = (x·u₁ / u₁·u₁)u₁ + … + (x·uₙ / uₙ·uₙ)uₙ.
Each coefficient is a projection, computed independently of the others. If the vectors are also unit vectors, the basis is orthonormal and the formula simplifies to x = (x·u₁)u₁ + … + (x·uₙ)uₙ.
Example. u₁ = (1, 1) and u₂ = (1, −1) are orthogonal (their dot product is 0). To write x = (5, 1) in this basis: x·u₁ = 6 and u₁·u₁ = 2, giving 3; x·u₂ = 4 and u₂·u₂ = 2, giving 2. So x = 3(1, 1) + 2(1, −1). No elimination needed.
A square matrix Q whose columns are orthonormal is an orthogonal matrix. It has the wonderful property QᵀQ = I, so Q⁻¹ = Qᵀ: inverting it costs nothing. Orthogonal matrices preserve lengths and angles, so they represent rigid motions: rotations and reflections. Their determinants are always +1 or −1.
This is why numerical software loves orthogonal matrices. Computations with them don't amplify rounding errors, so algorithms are often designed to work with orthonormal bases whenever possible. The JPEG image format, for instance, rewrites each 8 × 8 block of pixels in an orthogonal basis of cosine patterns before compressing it.
Step 2 of 13Practice · Work it out
In the orthogonal basis u₁ = (1, 2), u₂ = (2, −1), what is the coefficient of u₁ when writing x = (4, 3)?
Step 3 of 13Learn
The Gram–Schmidt process
What if your basis isn't orthogonal? The Gram–Schmidt process converts any basis into an orthogonal one spanning the same space. The idea: go through the vectors one at a time, and from each, subtract its projections onto the orthogonal vectors you've already built. What's left is perpendicular to all of them.
Given independent vectors x₁, x₂, x₃, …:
- v₁ = x₁
- v₂ = x₂ − (x₂·v₁ / v₁·v₁) v₁
- v₃ = x₃ − (x₃·v₁ / v₁·v₁) v₁ − (x₃·v₂ / v₂·v₂) v₂
- and so on.
Finally, divide each vᵢ by its length if you want an orthonormal basis.
Example. x₁ = (1, 1, 0), x₂ = (1, 0, 1).
v₁ = (1, 1, 0). Then x₂·v₁ = 1 and v₁·v₁ = 2, so
v₂ = (1, 0, 1) − (1/2)(1, 1, 0) = (1/2, −1/2, 1).
Check: v₁·v₂ = 1/2 − 1/2 + 0 = 0. Orthogonal, as promised. Scaling v₂ by 2 to clear fractions gives (1, −1, 2), which is just as good.
Recording the steps of Gram–Schmidt in matrix form gives the QR factorization, A = QR, with Q orthogonal and R upper triangular. It's one of the workhorse factorizations of numerical linear algebra, used inside eigenvalue algorithms and least squares solvers.
Step 4 of 13Practice · Put in order
Put the steps of Gram–Schmidt for three vectors x₁, x₂, x₃ in order.
Drag the items, or use the arrow buttons, to put them in order.
- Divide each vᵢ by its length to make the basis orthonormal
- Check that the dot products between the vᵢ are zero
- Set v₁ = x₁
- Subtract from x₂ its projection onto v₁ to get v₂
- Subtract from x₃ its projections onto v₁ and v₂ to get v₃
Step 5 of 13Diagram
Gram–Schmidt, one vector at a time
Tap a shape with to see more.
- v₁ = x₁
- v₂ = x₂ − its shadow on v₁
- v₃ = x₃ − shadows on v₁, v₂
- Optional: divide by lengths
Gives an orthonormal basis, the Q in QR factorization.
Step 6 of 13Learn
Least squares: the best answer to an impossible system
A scientist measures three data points: (0, 1), (1, 3) and (2, 4). She wants a line y = c + mx through them. That means solving
c + 0m = 1, c + 1m = 3, c + 2m = 4,
which is Ax = b with A = [1 0; 1 1; 1 2], x = (c, m) and b = (1, 3, 4). Three equations, two unknowns, and no exact solution: the points aren't collinear.
The least squares solution is the x that makes Ax as close as possible to b, minimizing the total squared error |b − Ax|². Geometrically, Ax ranges over the column space of A, a plane in ℝ³. The closest point to b in that plane is the projection of b onto it, and the error vector b − Ax must be orthogonal to the plane, which means orthogonal to every column of A: Aᵀ(b − Ax) = 0. Rearranged, these are the normal equations:
AᵀAx = Aᵀb.
For our data: AᵀA = [3 3; 3 5] and Aᵀb = (8, 11). Solving 3c + 3m = 8 and 3c + 5m = 11 gives m = 1.5 and c = 7/6 ≈ 1.17. The best-fit line is y ≈ 1.17 + 1.5x.
This is linear regression, the most widely used model in statistics and the simplest machine learning algorithm. The same normal equations fit planes, polynomials, and any model that's linear in its unknown coefficients.
Step 7 of 13Chart
The three measurements
No line passes through all three. The least-squares line is y ≈ 1.17 + 1.5x: it misses the points by −0.17, +0.33 and −0.17, and those misses have the smallest possible sum of squares.
| Point | x | y |
|---|---|---|
| (0, 1) | 0 | 1 |
| (1, 3) | 1 | 3 |
| (2, 4) | 2 | 4 |
Step 8 of 13Try the calculator
Least squares line through three points
Enter three data points. The calculator solves the normal equations for the best-fit line y = c + mx and reports the sum of squared errors. Make the points collinear and watch the error drop to zero.
- Slope m
- 1.5
- Intercept c
- 1.1667
Step 9 of 13Note
Step 10 of 13Practice · Choose one
In least squares, why must the error vector b − Ax̂ be orthogonal to the columns of A?
Step 11 of 13Note
Step 12 of 13Practice · Work it out
Fit y = c + mx to the points (0, 0), (1, 1) and (2, 5) by least squares. What is the slope m?
Step 13 of 13Practice · True or false
If Q is a square matrix with orthonormal columns, then Q⁻¹ = Qᵀ.
Lesson complete!