Worked examples — QR decomposition
The scenario matrix
Before working examples, let us list every distinct thing a QR problem can throw at you. Each row is a case class; the last column names the example that hits it.
| # | Case class | What is special | Covered by |
|---|---|---|---|
| 1 | Clean tall matrix, positive diagonal | textbook path, nothing weird | Ex. 1 |
| 2 | Negative sign appears | points "backwards" → sign convention on | Ex. 2 |
| 3 | Already orthogonal | limiting case: , | Ex. 3 |
| 4 | Already upper-triangular | limiting case: , recover a diagonal-sign matrix | Ex. 4 |
| 5 | Degenerate: dependent columns | → QR breaks, and why | Ex. 5 |
| 6 | Nearly dependent columns | limiting behaviour: , instability | Ex. 6 |
| 7 | Wide matrix (, more columns than rows) | forced dependence → breakdown | Ex. 7 |
| 8 | Least squares word problem | real data, over-determined system | Ex. 8 |
| 9 | Exam twist: use 's diagonal for | reading a hidden quantity out of | Ex. 9 |
We will now walk them in order. Keep the Gram-Schmidt process steps in mind — every example is that process, just with different inputs.
A one-figure reminder of the geometry
Every example does the same physical move: keep the first arrow, then throw away the shadow of the next arrow so only the perpendicular part survives.

Example 1 — Case 1: clean tall matrix
- First axis. , . So and . Why this step? The first clean direction is just the first column, shrunk to length 1 — there is nothing earlier to be perpendicular to.
- Shadow coefficient. . Why this step? measures how much of already points along — the part we must remove.
- Remove the shadow. . Why this step? What remains is the piece of perpendicular to — a brand-new direction.
- Second axis. , and . Why this step? Normalize so has length 1; now is orthonormal.
Verify: ✓ (perpendicular), each has length 1, and . All diagonal entries positive → this is the unique QR.
Example 2 — Case 2: a backward-pointing first column (sign convention)
- First axis. , so (a length is always — the minus signs squared away). . Why this step? A norm cannot be negative; the sign lives in the direction , not in . This is exactly why QR fixes : it forces the sign into .
- Shadow coefficient. . Why this step? can be negative — it just means leans against . Off-diagonal signs are unconstrained.
- Remove shadow. . Why this step? Subtract the shadow to leave only the part of perpendicular to ; here the shadow itself has negative sign, so subtracting it adds back — the same rule as always, just with a signed coefficient.
- Second axis. . . Why this step? Normalize the perpendicular leftover to length 1 so is orthonormal; the diagonal entry is again a positive length.
Verify: diagonal ✓. ✓ (compute: , cross term ). ✓. The negativity of the data went entirely into , never into 's diagonal.
Example 3 — Case 3: an already-orthogonal matrix (limiting case)
- First axis. , so , . Why this step? Column 1 is already length 1 — normalizing changes nothing.
- Shadow coefficient. . Why this step? Zero shadow means is already perpendicular to — nothing to subtract.
- Second axis. , , . Why this step? With zero shadow removed, the leftover is , and it is already unit length, so with — Gram–Schmidt confirms the frame was already clean.
Verify: ✓. This is the "QR does nothing" limit predicted in the parent note: for an orthogonal , every and every off-diagonal .
Example 4 — Case 4: an already upper-triangular matrix
- First axis. , . Why this step? Column 1 lies exactly along the first coordinate axis, so its clean direction is and its length is .
- Shadow coefficient. . Why this step? measures how far reaches along — the horizontal part we must strip off.
- Remove shadow. , , . Why this step? Subtracting the horizontal shadow leaves only the vertical part , which normalizes to the second axis — showing the columns already pointed along the coordinate axes.
Verify: ✓. Key insight: an already-upper-triangular matrix with positive diagonal is its own and . (If the diagonal had a negative entry, that column's sign would move into , exactly as in Example 2 — so would be on the diagonal, not .)
Example 5 — Case 5: DEGENERATE, dependent columns (QR breaks)
- First axis. , . Why this step? Same first move as always — normalize the first column to get the first clean direction; nothing is wrong yet.
- Shadow coefficient. . Why this step? Measure how much of lies along ; a large coefficient here is the first hint that points almost entirely along .
- Remove shadow — collapse. . Why this matters: was entirely shadow — it lies exactly along . After removing the shadow, nothing is left. , so .
- The break. is undefined — division by zero.
Verify: , so the columns are dependent (rank 1). The QR theorem in the parent note assumed linearly independent columns precisely so that every . Here that assumption fails, , and thin QR with positive diagonal simply does not exist. (Rank-revealing QR with column pivoting handles this, but the plain algorithm halts.)
Example 6 — Case 6: nearly dependent columns (limiting behaviour)
- First axis. , . Why this step? The first column is already a unit vector, so it is its own clean direction.
- Shadow coefficient. . Why this step? Measure the along- part of ; almost all of is this shadow.
- Remove shadow. , so . Why this step? Strip the shadow; the tiny leftover is all that distinguishes column 2 from column 1.
- Take the limit. As , . Why this step? The leftover perpendicular part shrinks to nothing — the columns are merging into one line, echoing Example 5's collapse.
Verify: ✓ (see Example 9 — the product of 's diagonal is the determinant). For , : any rounding error of size swamps it, so becomes garbage. This is exactly why classical Gram–Schmidt is unstable near dependence, and why practitioners switch to Householder reflections or modified Gram–Schmidt.
Example 7 — Case 7: a WIDE matrix (more columns than rows)
- First axis. , . Why this step? Normalize column 1 for the first clean direction — plenty of room so far.
- Second axis. , so , , . Why this step? Column 2 is already perpendicular to ; now span the entire plane — the room is full.
- Third axis — collapse. For : , . Then . Why this matters: already span all of , so is entirely built from earlier directions. Removing every shadow leaves nothing: , .
- The break. is undefined. Why this step? You cannot create a third direction perpendicular to two that already fill the plane — geometry forbids it.
Verify: the columns of a matrix are three vectors in , so they are always linearly dependent (you cannot have independent vectors in dimensions). Thus is required in the QR theorem; a wide matrix () can never have a thin QR with positive diagonal — the very first column that overflows the dimension gives .
Example 8 — Case 8: least-squares word problem
Set up the over-determined system with a column of ones (for ) and a column of (for ):
- QR of . , . . . , . Why this step? QR turns the messy least-squares into a triangular solve; see Least squares and normal equations.
- Form . . . Why this step? The minimizer of satisfies (parent note) — is expressed in the clean frame.
- Back-substitute . Row 2: . Row 1: . Why this step? Triangular systems solve bottom-up with no matrix inversion — exact and numerically stable, the whole point of using QR.
Verify (units + sanity): intercept sales at (close to the observed ), slope sale per hour. Predictions vs data — residuals sum to , as least squares demands (the fit passes through the data's centroid : ✓). Slope , matching the forecast option.
Example 9 — Case 9: exam twist, read off
- QR of . , , . Why this step? Normalize column 1 for the first diagonal entry — the only values we need are these diagonal norms.
- Shadow + second axis. ; ; . Why this step? Strip 's shadow to get the perpendicular leftover, whose length is the second diagonal entry .
- Multiply the diagonal. . Why this step? For a triangular matrix the determinant is the product down the diagonal — off-diagonal entries never contribute.
- Use orthogonality. . Why this step? An orthogonal preserves volume, so ; all the "stretch" of lives in 's diagonal.
Verify: direct expansion , so ✓. General rule you can quote in an exam: . (This same trick powers the Eigenvalues and the QR algorithm and mirrors how LU decomposition reads off its triangular factor.)
Active recall
Recall Which cases had no valid QR, and why?
Case 5 (Example 5): dependent columns make . Case 7 (Example 7): a wide matrix () forces dependence, so column gives . Both hit , undefined. Positive-diagonal QR needs and independent columns.
Why can never be negative?
What limit does approach as two columns merge?
Why can a wide matrix () never have a thin QR?
How do you get from QR?
QR of an already-orthogonal ?
QR of an already upper-triangular with positive diagonal?
Least-squares fitted line in Example 8?
Connections
- QR decomposition — the parent this page drills into.
- Gram-Schmidt process — every example is one run of it.
- Orthonormal bases and projections — the shadow is a projection.
- Least squares and normal equations — Example 8's engine.
- Householder reflections — the stable fix for Case 6.
- Eigenvalues and the QR algorithm — uses the idea.
- LU decomposition — sibling factorization that also reads off a triangle.