Lesson 15 - Orthogonality, Gram–Schmidt, and Projection¶
Learning goals. By the end of this lecture, students will be able to:
interpret dot products as both weighted sums and geometric measures of alignment,
determine when vectors are orthogonal and explain why orthogonality is useful computationally,
project one vector onto another vector or onto a subspace,
construct an orthonormal basis using the Gram–Schmidt process,
connect projections and orthonormal bases to least-squares approximation and model reduction ideas.
Key terms. orthogonality, projection, orthonormal basis, Gram–Schmidt process, least squares.
From operations to geometry¶
Lesson 11 introduced the dot product as a weighted sum,
That interpretation is still important, but the dot product also carries geometric information. It tells us how much two vectors point in the same direction.
For nonzero vectors \(\mathbf{u}\) and \(\mathbf{v}\), the angle \(\theta\) between them satisfies
If the dot product is positive, the vectors point partly in the same direction. If it is negative, they point partly in opposite directions. If it is zero, the vectors are orthogonal.
Orthogonality¶
Two vectors are orthogonal if
In two or three dimensions, orthogonality corresponds to a right angle. In higher dimensions, the picture is harder to draw, but the algebraic condition is the same.
Orthogonality is useful because independent contributions do not interfere. If \(\mathbf{u}\perp\mathbf{v}\), then
This is the Pythagorean theorem in vector form.
A set of vectors \(\mathbf{q}_1,\ldots,\mathbf{q}_k\) is orthonormal if
If we collect these vectors as columns of a matrix
then orthonormality is written compactly as
Projection onto a line¶
Suppose \(\mathbf{q}\) is a unit vector, meaning \(\left\lVert \mathbf{q} \right\rVert_2=1\). The projection of \(\mathbf{x}\) onto the line spanned by \(\mathbf{q}\) is
The scalar \(\mathbf{q}^{\mathsf{T}}\mathbf{x}\) is the component of \(\mathbf{x}\) in the \(\mathbf{q}\) direction. Multiplying by \(\mathbf{q}\) turns that component back into a vector.
If the direction vector \(\mathbf{v}\) is not normalized, the projection is
The error \(\mathbf{x}-\widehat{\mathbf{x}}\) is orthogonal to the line:
This orthogonality condition is the key to least-squares problems.
The geometry is the important part. Projection splits one vector into a part the line can explain and a leftover part that points away from the line:

Figure. Projection onto a line decomposes \(\mathbf{x}\) into a component along the line and an orthogonal residual.
The formula matters because it computes the picture; the picture matters because it tells us what the formula is trying to do.
Example.
Let
Then
The error is
and this error is orthogonal to \(\mathbf{v}\).
Projection onto a subspace¶
A subspace is a flat set of vectors that is closed under linear combinations. A line through the origin is a one-dimensional subspace; a plane through the origin is a two-dimensional subspace. In higher dimensions the picture is harder to draw, but the rule is the same.
Let the columns of \(\mathbf{Q}\in\mathbb{R}^{n\times k}\) be an orthonormal basis for a subspace \(\mathcal{S}\). The orthogonal projection of \(\mathbf{x}\) onto \(\mathcal{S}\) is
The vector \(\mathbf{Q}^{\mathsf{T}}\mathbf{x}\) contains the coordinates of the projection in the orthonormal basis. Its entries are dot products:
Multiplying by \(\mathbf{Q}\) converts those coordinates back to the original vector space:

Figure. For orthonormal columns, \(\mathbf{Q}^{\mathsf{T}}\) measures coordinates by dot products and \(\mathbf{Q}\) reconstructs the projected vector from those coordinates.
The projection is the best approximation to \(\mathbf{x}\) from the subspace:
The error is orthogonal to the whole subspace:
This orthogonal-residual condition is the key idea to remember: projection is not only a formula, but the closest available approximation together with an error that points outside the chosen subspace.
If the basis columns in \(\mathbf{V}\) are linearly independent but not orthonormal, then the projection matrix is
This formula is useful for interpretation, but it is usually less pleasant computationally than working with an orthonormal basis. If the columns are nearly dependent, \(\mathbf{V}^{\mathsf{T}}\mathbf{V}\) can be poorly conditioned, and the formula can magnify roundoff error.
Example.
A subspace projection with an orthonormal basis. Let
These vectors are orthonormal. If \(\mathbf{x}=[2,0,1]^T\), then the coordinates of the projection are
The projected vector is
The residual \(\mathbf{x}-\widehat{\mathbf{x}}=[1/2,-1,1/2]^T\) is orthogonal to both \(\mathbf{q}_1\) and \(\mathbf{q}_2\).
Least squares as projection¶
An overdetermined system
usually has more equations than unknowns. We cannot, in general, make \(\mathbf{A}\mathbf{x}\) equal \(\mathbf{b}\). Instead, least squares chooses \(\mathbf{x}\) to minimize the residual norm:
Geometrically, \(\mathbf{A}\mathbf{x}_{\text{ls}}\) is the projection of \(\mathbf{b}\) onto the column space of \(\mathbf{A}\). Therefore, the residual
is orthogonal to every column of \(\mathbf{A}\). This gives the normal equations
or
The normal equations are a compact way to express the projection condition. In practical numerical work, QR- or SVD-based algorithms are often preferred because they use orthonormal bases more directly and handle rank or conditioning issues more reliably.
Gram–Schmidt¶
Gram–Schmidt turns a linearly independent set of vectors into an orthonormal set spanning the same subspace. Suppose we start with \(\mathbf{v}_1,\ldots,\mathbf{v}_k\). The first basis vector is
To build \(\mathbf{q}_2\), remove the component of \(\mathbf{v}_2\) in the \(\mathbf{q}_1\) direction:
then normalize:
The first nontrivial step is the same projection picture used in reverse. The candidate vector \(\mathbf{v}_2\) contains a part already explained by \(\mathbf{q}_1\). Gram–Schmidt subtracts that old part and keeps the new orthogonal direction.

Figure. Gram–Schmidt removes the component already lying in the old direction, leaving a new vector orthogonal to that direction.
For the \(j\)th vector, subtract all previously constructed directions:
Algorithm.
Algorithm: Modified Gram–Schmidt
Start with linearly independent columns \(\mathbf{v}_1,\ldots,\mathbf{v}_k\).
For \(j=1,2,\ldots,k\), set \(\mathbf{w}=\mathbf{v}_j\).
For each previous basis vector \(\mathbf{q}_i\), subtract \((\mathbf{q}_i^{\mathsf{T}}\mathbf{w})\mathbf{q}_i\) from \(\mathbf{w}\).
Normalize the remaining vector: \(\mathbf{q}_j=\mathbf{w}/\left\lVert \mathbf{w} \right\rVert_2\).
Listing. Modified Gram–Schmidt in Python.
[1]:
import numpy as np
def modified_gram_schmidt(V):
V = V.astype(float)
n, k = V.shape
Q = np.zeros((n, k))
R = np.zeros((k, k))
for j in range(k):
w = V[:, j].copy()
for i in range(j):
R[i, j] = Q[:, i] @ w
w = w - R[i, j] * Q[:, i]
R[j, j] = np.linalg.norm(w)
if R[j, j] == 0.0:
raise ValueError('dependent input vectors')
Q[:, j] = w / R[j, j]
return Q, R
The output satisfies approximately
and
This is a QR factorization.
Example.
Discrete polynomial-like basis vectors. On the grid \(i=0,1,2,3\), start with the sampled monomials
The first vector is the constant shape:
The component of \(\mathbf{v}_1\) along \(\mathbf{q}_0\) is
so the centered linear part is
After normalization,
For the quadratic sample, remove the constant and linear parts:
so

Figure. A discrete constant-linear-quadratic orthonormal basis on four sample points. The scale is less important than the shapes and their orthogonality.
This is a discrete analogue of building orthogonal polynomials: begin with simple powers of the grid index, then remove what has already been represented. A sampled response can then be reconstructed from basis coefficients. For example, if
then
and the reconstruction is
The three coefficients say how much constant, linear, and quadratic shape is present in this four-point signal.
Why orthonormal bases are computationally useful¶
If \(\mathbf{Q}\) has orthonormal columns, then projecting onto its column space is simple:
Coordinates are computed by dot products:
Lengths are preserved under square orthogonal transformations:
when \(\mathbf{Q}^{\mathsf{T}}\mathbf{Q}=\mathbf{I}\) and \(\mathbf{Q}\) is square. These properties make orthonormal bases stable and convenient in least squares, model reduction, and iterative methods.
Example.
A Galerkin system from a projection condition. Suppose we want an approximate solution of \(\mathbf{A}\mathbf{x}=\mathbf{b}\) in the two-dimensional subspace spanned by the orthonormal columns of \(\mathbf{Q}\). We write
The residual is
A Galerkin condition asks that this residual be orthogonal to the same subspace:
Substituting the residual gives the smaller system
This system did not come out of thin air: it is the orthogonality condition applied to the residual. Krylov and finite-element methods use this same projection logic with more carefully chosen subspaces.
Connection to Krylov ideas¶
Lesson 14 ended with repeated matrix-vector products. There, the iteration index \(k\) was a sweep or generation counter: Richardson, Jacobi, and Gauss–Seidel repeatedly converted a residual into a new population estimate, mostly keeping only the current iterate. Krylov methods keep more of that generated history. Starting from a residual \(\mathbf{r}_0\), one can build the sequence
The span of the first \(k\) vectors is a Krylov subspace:
The vectors in this sequence can become nearly dependent, so orthonormalization is essential in many Krylov methods. Gram–Schmidt applied to this sequence is the core idea behind Arnoldi iteration, GMRES, and related algorithms.
We will not develop GMRES fully here. The important message for this course is that the same ingredients keep appearing:
Connection to eigenvectors¶
Orthogonality also appears in eigenvalue problems. For real symmetric matrices, eigenvectors associated with distinct eigenvalues are orthogonal. This is one reason symmetric eigenvalue problems are especially well behaved.
Power iteration in Lesson 16 repeatedly multiplies by \(\mathbf{A}\) and normalizes. More advanced eigenvalue algorithms keep several directions at once, orthogonalize them, and project the matrix onto a lower-dimensional subspace. The geometry in this chapter is therefore not separate from eigenvalue computation; it is one of the foundations.
Exercises¶
Let \(\mathbf{u}=[1,2,-1]^T\) and \(\mathbf{v}=[2,-1,0]^T\). Compute \(\mathbf{u}\cdot\mathbf{v}\). Are the vectors orthogonal?
Project \(\mathbf{x}=[2,1]^T\) onto the line spanned by \(\mathbf{v}=[1,1]^T\). What is the projection error, and how can you check that it is orthogonal to \(\mathbf{v}\)?
Apply the first two steps of Gram–Schmidt to \(\mathbf{v}_1=[1,1,0]^T\) and \(\mathbf{v}_2=[1,0,1]^T\). Give \(\mathbf{q}_1\) and \(\mathbf{q}_2\).
Explain why orthonormal columns make projection and coordinate calculations easier.
Answers.
The dot product is \(1(2)+2(-1)+(-1)(0)=0\), so the vectors are orthogonal.
The projection coefficient is \(3/2\), so
\[\begin{split}\widehat{\mathbf{x}}= \frac{3}{2} \begin{bmatrix} 1\\ 1 \end{bmatrix} = \begin{bmatrix} 3/2\\ 3/2 \end{bmatrix}.\end{split}\]
The error is \([1/2,-1/2]^T\), and its dot product with \([1,1]^T\) is zero.
The first vector is
\[\begin{split}\mathbf{q}_1=\frac{1}{\sqrt{2}} \begin{bmatrix} 1\\ 1\\ 0 \end{bmatrix}.\end{split}\]
Subtracting the part of \(\mathbf{v}_2\) along \(\mathbf{q}_1\) gives
If \(\mathbf{Q}\) has orthonormal columns, coordinates are \(\mathbf{Q}^T\mathbf{x}\), and the projection is \(\mathbf{Q}\mathbf{Q}^T\mathbf{x}\). No normal-equation solve is needed.
End-of-lecture summary¶
Takeaway.
The dot product measures both weighted sums and geometric alignment.
Orthogonal vectors have zero dot product and independent length contributions.
Projection gives the closest vector in a line or subspace, and the projection error is orthogonal to that subspace.
Least squares is a projection problem onto a column space.
Gram–Schmidt builds an orthonormal basis from independent vectors.
Orthonormal bases support stable projections, least-squares solves, Krylov methods, and eigenvalue algorithms.