Gram-Schmidt Straightens a Skewed Basis
Say two vectors sit at a slant. They span the same plane, but they aren't perpendicular and their lengths differ, which is awkward for calculation. Gram-Schmidt is the procedure that straightens this skewed basis. Keep the first direction as it is, then from the second vector subtract the shadow it casts onto the first (its projection). What remains is exactly the part perpendicular to the first. Finally rescale both to length 1, and out comes a clean unit frame meeting at right angles. In the first widget, step through one stage at a time and follow how two slanted vectors become orthonormal. This is precisely the Q of the QR decomposition in the next lesson.
Step through the stages with the buttons. Stage 0 is the slanted input a1, a2 as given. Stage 1 shrinks a1 to length 1, making the first unit vector u1 (green). Stage 2 draws in gold the shadow a2 casts onto u1, the projection (a2·u1)u1 — how much of the u1 direction lives inside a2. Stage 3 subtracts that projection from a2 to leave the perpendicular remainder w (orange), then rescales it to length 1 to get the second unit vector u2. Now u1 and u2 meet at exactly a right angle. The skewed pair has been straightened into a clean orthonormal one.
Take just the core move of Gram-Schmidt into your own hands. Fix u1 as the unit vector along the x axis, and drag the blue vector a2 wherever you like. The gold arrow is the projection a2 casts onto u1, (a2·u1)u1 — only the u1 component pulled out of a2. The orange arrow is the remainder, a2 minus that projection. Wherever you drag a2, this orange remainder always meets u1 at exactly a right angle, because subtracting the projection removes the parallel part and leaves only the perpendicular one. This single subtraction is the heart of Gram-Schmidt, the move that sets a skewed second vector upright.
Now see that however skewed the input, the output stays straight. Keep a1 fixed and use the slider to swing the tilt angle of a2 around. The two faint vectors are the slanted input a1, a2, and the bold right-angle unit frame u1, u2 is the output Gram-Schmidt produces. However far you tilt the input, the output u1 and u2 always stay at length 1 and perpendicular to each other. There is one dangerous spot: make a2 nearly parallel to a1 and they point the same way, so the plane to span collapses and the perpendicular component drops toward zero. At that moment no orthonormal frame can be built and a warning appears. A nearly parallel basis cannot be straightened.
Gram-Schmidt gives a different result depending on which vector you start from, because the direction of the first vector alone is preserved. Use the toggle to switch the starting order. With a1 first, u1 follows the a1 direction unchanged and u2 is raised perpendicular to it. Switch to a2 first and now u1 follows the a2 direction, with u2 as its perpendicular. With the same two inputs spanning the same plane, the orthonormal frame you get is rotated differently. Both are equally valid orthonormal bases; it is just that Gram-Schmidt anchors on the first direction, so which one you call first decides the answer.
Now gather the two straightened unit vectors into one matrix. Toggle u1 and u2 on one at a time, and each takes its place as column 1 and column 2 of a matrix Q. The readout below shows that 2x2 Q; the green column is u1, the orange column is u2. Because Q's two columns are perpendicular unit vectors, Q is a pure rotation (an orthogonal matrix) that touches neither length nor angle, so Qᵀ Q = I holds. This assembled Q is exactly the Q of the QR decomposition in the next lesson. Any matrix A splits as A = QR, where Q is these orthonormal columns straightened by Gram-Schmidt and R is the upper-triangular matrix holding the projection coefficients pulled out along the way.