Lab 11: Gram-Schmidt and QR Factorization

due by the end of class on Wednesday, November 11, 2026

Each lab worksheet will contain several activities, most of which will involve writing math on paper, and some of which will involve running code in a Jupyter Notebook. Lab activities are meant to last an hour, and the second hour of lab is dedicated to starting the homework assignment. To receive credit for a lab, you must show your lab TA your work on both the lab worksheet and homework assignment.

While you must get checked off by your lab TA individually, we encourage you to form groups with 1-2 other students to complete the activities together.


Activities


Activity 1: A \(2\times2\) QR factorization

Consider the matrix

$$ A=\begin{bmatrix}5&-7\\12&17\end{bmatrix}. $$

Call its columns \(\vec v_1,\vec v_2\), in that order. Our goal is to find a QR factorization of \(A\).

a)

First, use the Gram-Schmidt process to find an orthonormal basis \(\vec u_1,\vec u_2\) of \(\operatorname{colsp}(A)\).

Solution

The first column has length \(|\vec v_1|=\sqrt{5^2+12^2}=13\), so

$$ \vec u_1=\frac1{13}\begin{bmatrix}5\\12\end{bmatrix},\qquad \vec v_2\cdot\vec u_1=\frac{-35+204}{13}=13. $$

Then

$$ \vec w_2=\vec v_2-13\vec u_1 =\begin{bmatrix}-7\\17\end{bmatrix}-\begin{bmatrix}5\\12\end{bmatrix} =\begin{bmatrix}-12\\5\end{bmatrix}. $$

Its length is also \(13\), giving \(\vec u_2=\frac1{13}\begin{bmatrix}-12\\5\end{bmatrix}\).

The Gram-Schmidt process preserves the span: \(\vec u_1,\vec u_2\) are combinations of the original columns, and the equations in part (b) recover those columns. Thus they form an orthonormal basis of \(\operatorname{colsp}(A)\).

b)

Write the relationships between the original vectors (\(\vec v_1\) and \(\vec v_2\)) and the orthonormal basis vectors (\(\vec u_1\) and \(\vec u_2\)). Use these equations to find \(Q\) and \(R\) such that \(A=QR\).

Solution

Undoing the normalization and projection steps gives the relationships \(\vec v_1=13\vec u_1\) and \(\vec v_2=13\vec u_1+13\vec u_2\). Thus

$$ Q=\frac1{13}\begin{bmatrix}5&-12\\12&5\end{bmatrix},\qquad R=\begin{bmatrix}13&13\\0&13\end{bmatrix}. $$

Each column of \(R\) records the coefficients of one original vector in the orthonormal basis; the columns of \(Q\) are \(\vec u_1\) and \(\vec u_2\).

c)

Verify that \(QR=A\) and that the columns of \(Q\) are orthonormal.

Solution

Both columns have squared length \((25+144)/169=1\), and their dot product is \((-60+60)/169=0\). Also,

$$ QR=\frac1{13}\begin{bmatrix}65&65-156\\156&156+65\end{bmatrix} =\begin{bmatrix}5&-7\\12&17\end{bmatrix}=A. $$

Gram-Schmidt with three vectors

We’ll now consider the case of three vectors. Suppose that \(A\) is a matrix whose columns \(\vec v_1,\vec v_2,\vec v_3\) are linearly independent. We want to use the Gram-Schmidt method to construct an orthonormal basis \(\vec u_1,\vec u_2,\vec u_3\) of their span and find the QR factorization of \(A\). The first two steps are just what we’ve already done.

  • First vector: normalize. Divide by its length:
$$ \vec u_1=\frac{\vec v_1}{\|\vec v_1\|}. $$
  • Second vector: subtract one projection, then normalize. Let \(\ell_1=\operatorname{span}(\vec u_1)\). Remove the part of \(\vec v_2\) along this line:
$$ \vec w_2=\vec v_2-\operatorname{proj}_{\ell_1}(\vec v_2) =\vec v_2-(\vec v_2\cdot\vec u_1)\vec u_1, \qquad \vec u_2=\frac{\vec w_2}{\|\vec w_2\|}. $$
  • Third vector: subtract two projections, then normalize. Here’s the new step. We want a vector perpendicular to both \(\vec u_1\) and \(\vec u_2\). Let \(\ell_2=\operatorname{span}(\vec u_2)\). Subtract the parts along both lines:
$$ \begin{align*} \vec w_3&=\vec v_3-\operatorname{proj}_{\ell_1}(\vec v_3)-\operatorname{proj}_{\ell_2}(\vec v_3)\\ &=\vec v_3-(\vec v_3\cdot\vec u_1)\vec u_1-(\vec v_3\cdot\vec u_2)\vec u_2, \qquad \vec u_3=\frac{\vec w_3}{\|\vec w_3\|}. \end{align*} $$

Because \(\vec u_1\) and \(\vec u_2\) are perpendicular, removing the component along one doesn’t change the component along the other. What’s left is perpendicular to both. The remaining vectors \(\vec w_2,\vec w_3\) cannot be zero, because the original columns are independent.

How does this give \(A=QR\)? First record how the old vectors relate to the new ones. Put back the components we subtracted:

$$ \begin{align*} \vec v_1&=\boxed{\|\vec v_1\|}\vec u_1,\\ \vec v_2&=\boxed{(\vec v_2\cdot\vec u_1)}\vec u_1+\boxed{\|\vec w_2\|}\vec u_2,\\ \vec v_3&=\boxed{(\vec v_3\cdot\vec u_1)}\vec u_1+\boxed{(\vec v_3\cdot\vec u_2)}\vec u_2+\boxed{\|\vec w_3\|}\vec u_3. \end{align*} $$

These equations can be written together as

$$ \underbrace{\begin{bmatrix}|&|&|\\\vec v_1&\vec v_2&\vec v_3\\|&|&|\end{bmatrix}}_{A} =\underbrace{\begin{bmatrix}|&|&|\\\vec u_1&\vec u_2&\vec u_3\\|&|&|\end{bmatrix}}_{Q} \underbrace{\begin{bmatrix} \|\vec v_1\|&\vec v_2\cdot\vec u_1&\vec v_3\cdot\vec u_1\\ 0&\|\vec w_2\|&\vec v_3\cdot\vec u_2\\ 0&0&\|\vec w_3\| \end{bmatrix}}_{R}. $$

So \(Q\) contains the orthonormal vectors, and each column of \(R\) contains the coefficients for one original vector. The diagonal entries are the lengths we divided by; the entries above them are the dot products we used for the projections. Keep these numbers as you work!


Activity 2: A \(3\times3\) QR factorization

Now consider the matrix

$$ A=\begin{bmatrix}2&5&-2\\-2&-2&5\\1&4&5\end{bmatrix}. $$

Our goal is to find a QR factorization of \(A\). To start, we’ll apply the Gram-Schmidt process to \(A\). Call \(A\)’s columns \(\vec v_1\), \(\vec v_2\), and \(\vec v_3\), in that order.

a)

Find \(\vec u_1\), then subtract the component of \(\vec v_2\) along \(\vec u_1\) and normalize to find \(\vec u_2\).

Solution

The first column has length \(\sqrt{2^2+(-2)^2+1^2}=3\), so

$$ \vec u_1=\frac13\begin{bmatrix}2\\-2\\1\end{bmatrix},\qquad \vec v_2\cdot\vec u_1=\frac{10+4+4}{3}=6. $$

Then

$$ \vec w_2=\begin{bmatrix}5\\-2\\4\end{bmatrix} -2\begin{bmatrix}2\\-2\\1\end{bmatrix} =\begin{bmatrix}1\\2\\2\end{bmatrix}. $$

Its length is \(3\), so \(\vec u_2=\frac13\begin{bmatrix}1\\2\\2\end{bmatrix}\).

b)

Subtract the components of \(\vec v_3\) along both \(\vec u_1\) and \(\vec u_2\), then normalize to find \(\vec u_3\).

Solution

The projection coefficients are

$$ \vec v_3\cdot\vec u_1=\frac{-4-10+5}{3}=-3,\qquad \vec v_3\cdot\vec u_2=\frac{-2+10+10}{3}=6. $$

Subtracting both components gives

$$ \vec w_3=\vec v_3-(-3)\vec u_1-6\vec u_2 =\begin{bmatrix}-2\\5\\5\end{bmatrix} +\begin{bmatrix}2\\-2\\1\end{bmatrix} -2\begin{bmatrix}1\\2\\2\end{bmatrix} =\begin{bmatrix}-2\\-1\\2\end{bmatrix}. $$

Its length is \(3\), so \(\vec u_3=\frac13\begin{bmatrix}-2\\-1\\2\end{bmatrix}\).

c)

Write the relationships between the original vectors (\(\vec v_1\), \(\vec v_2\), and \(\vec v_3\)) and the orthonormal basis vectors (\(\vec u_1\), \(\vec u_2\), and \(\vec u_3\)). Use these equations to find \(Q\) and \(R\) such that \(A=QR\). Verify that \(QR=A\) and that the columns of \(Q\) are orthonormal.

Solution

Putting back the components we subtracted and undoing normalization gives

$$ \vec v_1=3\vec u_1,\qquad \vec v_2=6\vec u_1+3\vec u_2,\qquad \vec v_3=-3\vec u_1+6\vec u_2+3\vec u_3. $$

Thus

$$ Q=\frac13\begin{bmatrix}2&1&-2\\-2&2&-1\\1&2&2\end{bmatrix},\qquad R=\begin{bmatrix}3&6&-3\\0&3&6\\0&0&3\end{bmatrix}. $$

Each column of \(Q\) has squared length \(9/9=1\). The distinct-column dot products are \((2-4+2)/9=0\), \((-4+2+2)/9=0\), and \((-2-2+4)/9=0\). Finally,

$$ QR=\frac13\begin{bmatrix}6&15&-6\\-6&-6&15\\3&12&15\end{bmatrix} =\begin{bmatrix}2&5&-2\\-2&-2&5\\1&4&5\end{bmatrix}=A. $$