Skip to content

QR Decomposition: Worked Examples

We apply QR decomposition to two least squares problems. The first is the polynomial fitting problem solved in the Normal Equations section, so both methods must produce identical answers, which serves as a useful consistency check. The second is a larger overdetermined system fitted with a quadratic, which exercises the full three step Gram-Schmidt process.


Example 1

Problem Setup

Problem: Given the data f(−3)=0f(-3) = 0, f(0)=0f(0) = 0, f(6)=2f(6) = 2, find the least squares polynomial P1(x)=a0+a1xP_1(x) = a_0 + a_1 x using QR decomposition.

The coefficient matrix and right-hand side are:

A=[1−31016],b=[002]A = \begin{bmatrix} 1 & -3 \\ 1 & 0 \\ 1 & 6 \end{bmatrix}, \qquad b = \begin{bmatrix} 0 \\ 0 \\ 2 \end{bmatrix}

AA has two columns, so the Gram-Schmidt process will run for k=1k = 1 and k=2k = 2.

Label the columns: u1=[111]u_1 = \begin{bmatrix}1\\1\\1\end{bmatrix} and u2=[−306]u_2 = \begin{bmatrix}-3\\0\\6\end{bmatrix}.


Step 1 — Gram-Schmidt: k=1k = 1

For k=1k = 1, the sum is empty, so:

p1=u1=[111]p_1 = u_1 = \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} ∣p1∣=12+12+12=3|p_1| = \sqrt{1^2 + 1^2 + 1^2} = \sqrt{3} q1=p1∣p1∣=13[111]q_1 = \frac{p_1}{|p_1|} = \frac{1}{\sqrt{3}}\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}

Step 2 — Gram-Schmidt: k=2k = 2

p2=u2−(u2Tq1) q1p_2 = u_2 - (u_2^T q_1)\,q_1

First, compute the projection coefficient u2Tq1u_2^T q_1:

u2Tq1=[−306]13[111]=13(−3+0+6)=33=3u_2^T q_1 = \begin{bmatrix} -3 & 0 & 6 \end{bmatrix} \frac{1}{\sqrt{3}}\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} = \frac{1}{\sqrt{3}}(-3 + 0 + 6) = \frac{3}{\sqrt{3}} = \sqrt{3}

Now subtract the projection:

p2=[−306]−3⋅13[111]=[−306]−[111]=[−4−15]p_2 = \begin{bmatrix} -3 \\ 0 \\ 6 \end{bmatrix} - \sqrt{3} \cdot \frac{1}{\sqrt{3}}\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} = \begin{bmatrix} -3 \\ 0 \\ 6 \end{bmatrix} - \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} = \begin{bmatrix} -4 \\ -1 \\ 5 \end{bmatrix}

Normalize:

∣p2∣=(−4)2+(−1)2+52=16+1+25=42|p_2| = \sqrt{(-4)^2 + (-1)^2 + 5^2} = \sqrt{16 + 1 + 25} = \sqrt{42} q2=p2∣p2∣=142[−4−15]q_2 = \frac{p_2}{|p_2|} = \frac{1}{\sqrt{42}}\begin{bmatrix} -4 \\ -1 \\ 5 \end{bmatrix}

Step 3 — Assemble QQ and QTQ^T

Q=[q1q2]=[13−44213−14213542]Q = \begin{bmatrix} q_1 & q_2 \end{bmatrix} = \begin{bmatrix} \dfrac{1}{\sqrt{3}} & \dfrac{-4}{\sqrt{42}} \\[8pt] \dfrac{1}{\sqrt{3}} & \dfrac{-1}{\sqrt{42}} \\[8pt] \dfrac{1}{\sqrt{3}} & \dfrac{5}{\sqrt{42}} \end{bmatrix} QT=[131313−442−142542]Q^T = \begin{bmatrix} \dfrac{1}{\sqrt{3}} & \dfrac{1}{\sqrt{3}} & \dfrac{1}{\sqrt{3}} \\[8pt] \dfrac{-4}{\sqrt{42}} & \dfrac{-1}{\sqrt{42}} & \dfrac{5}{\sqrt{42}} \end{bmatrix}

Step 4 — Compute RR

Using Rij=ujTqiR_{ij} = u_j^T q_i for i≤ji \leq j:

u1Tq1=[111]13[111]=33=3u_1^T q_1 = \begin{bmatrix}1 & 1 & 1\end{bmatrix} \frac{1}{\sqrt{3}}\begin{bmatrix}1\\1\\1\end{bmatrix} = \frac{3}{\sqrt{3}} = \sqrt{3} u2Tq1=3(computed in Step 2)u_2^T q_1 = \sqrt{3} \quad \text{(computed in Step 2)} u2Tq2=[−306]142[−4−15]=142(12+0+30)=4242=42u_2^T q_2 = \begin{bmatrix}-3 & 0 & 6\end{bmatrix} \frac{1}{\sqrt{42}}\begin{bmatrix}-4\\-1\\5\end{bmatrix} = \frac{1}{\sqrt{42}}(12 + 0 + 30) = \frac{42}{\sqrt{42}} = \sqrt{42}

Therefore:

R=[33042]R = \begin{bmatrix} \sqrt{3} & \sqrt{3} \\ 0 & \sqrt{42} \end{bmatrix}

Step 5 — Compute the right-hand side QTbQ^T b

QTb=[131313−442−142542][002]=[231042]Q^T b = \begin{bmatrix} \dfrac{1}{\sqrt{3}} & \dfrac{1}{\sqrt{3}} & \dfrac{1}{\sqrt{3}} \\[8pt] \dfrac{-4}{\sqrt{42}} & \dfrac{-1}{\sqrt{42}} & \dfrac{5}{\sqrt{42}} \end{bmatrix} \begin{bmatrix} 0 \\ 0 \\ 2 \end{bmatrix} = \begin{bmatrix} \dfrac{2}{\sqrt{3}} \\[8pt] \dfrac{10}{\sqrt{42}} \end{bmatrix}

Step 6 — Solve Rx=QTbRx = Q^T b by back substitution

The system to solve:

[33042][a0a1]=[231042]\begin{bmatrix} \sqrt{3} & \sqrt{3} \\ 0 & \sqrt{42} \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \end{bmatrix} = \begin{bmatrix} \dfrac{2}{\sqrt{3}} \\[8pt] \dfrac{10}{\sqrt{42}} \end{bmatrix}

From row 2:

42 a1=1042  ⟹  a1=1042⋅42=1042=521\sqrt{42}\,a_1 = \frac{10}{\sqrt{42}} \implies a_1 = \frac{10}{\sqrt{42} \cdot \sqrt{42}} = \frac{10}{42} = \frac{5}{21}

From row 1:

3 a0+3 a1=23  ⟹  3 a0=23−3⋅521=23−5321\sqrt{3}\,a_0 + \sqrt{3}\,a_1 = \frac{2}{\sqrt{3}} \implies \sqrt{3}\,a_0 = \frac{2}{\sqrt{3}} - \sqrt{3} \cdot \frac{5}{21} = \frac{2}{\sqrt{3}} - \frac{5\sqrt{3}}{21} 3 a0=23−573=1473−573=973\sqrt{3}\,a_0 = \frac{2}{\sqrt{3}} - \frac{5}{7\sqrt{3}} = \frac{14}{7\sqrt{3}} - \frac{5}{7\sqrt{3}} = \frac{9}{7\sqrt{3}} a0=973⋅3=921=37a_0 = \frac{9}{7\sqrt{3} \cdot \sqrt{3}} = \frac{9}{21} = \frac{3}{7}

Result

a0=37,a1=521\boxed{a_0 = \frac{3}{7}, \qquad a_1 = \frac{5}{21}}

The least squares polynomial is:

P1(x)=37+521 xP_1(x) = \frac{3}{7} + \frac{5}{21}\,x

Example 2

Problem Setup

Problem: Solve the following system in the least squares sense using QR decomposition.

a0+a1+a2=2a0+2a1+4a2=3a0+3a1+9a2=6a0+4a1+16a2=4\begin{aligned} a_0 + a_1 + a_2 &= 2 \\ a_0 + 2a_1 + 4a_2 &= 3 \\ a_0 + 3a_1 + 9a_2 &= 6 \\ a_0 + 4a_1 + 16a_2 &= 4 \end{aligned}

In matrix form Ax=bAx = b:

[1111241391416][a0a1a2]=[2364]\begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 4 \\ 1 & 3 & 9 \\ 1 & 4 & 16 \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \\ a_2 \end{bmatrix} = \begin{bmatrix} 2 \\ 3 \\ 6 \\ 4 \end{bmatrix}

AA is a 4×34 \times 3 matrix, so this is an overdetermined system with four equations and three unknowns. It is the quadratic fit P2(x)=a0+a1x+a2x2P_2(x) = a_0 + a_1 x + a_2 x^2 through the data f(1)=2f(1) = 2, f(2)=3f(2) = 3, f(3)=6f(3) = 6, f(4)=4f(4) = 4.

AA has three columns, so the Gram-Schmidt process will run for k=1k = 1, k=2k = 2 and k=3k = 3. There will be three steps.

Label the columns:

u1=[1111],u2=[1234],u3=[14916]u_1 = \begin{bmatrix}1\\1\\1\\1\end{bmatrix}, \qquad u_2 = \begin{bmatrix}1\\2\\3\\4\end{bmatrix}, \qquad u_3 = \begin{bmatrix}1\\4\\9\\16\end{bmatrix}

Step 1 — Gram-Schmidt: k=1k = 1

For k=1k = 1, the sum is empty, so:

p1=u1=[1111]p_1 = u_1 = \begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} ∣p1∣=12+12+12+12=4=2|p_1| = \sqrt{1^2 + 1^2 + 1^2 + 1^2} = \sqrt{4} = 2 q1=p1∣p1∣=12[1111]=[0.50.50.50.5]q_1 = \frac{p_1}{|p_1|} = \frac{1}{2}\begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 0.5 \\ 0.5 \\ 0.5 \\ 0.5 \end{bmatrix}

Step 2 — Gram-Schmidt: k=2k = 2

p2=u2−(u2Tq1) q1p_2 = u_2 - (u_2^T q_1)\,q_1

First, compute the projection coefficient u2Tq1u_2^T q_1:

u2Tq1=[1234]12[1111]=12(1+2+3+4)=102=5u_2^T q_1 = \begin{bmatrix} 1 & 2 & 3 & 4 \end{bmatrix} \frac{1}{2}\begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} = \frac{1}{2}(1 + 2 + 3 + 4) = \frac{10}{2} = 5

Now subtract the projection:

p2=[1234]−5⋅12[1111]=[1234]−[2.52.52.52.5]=[−1.5−0.50.51.5]p_2 = \begin{bmatrix} 1 \\ 2 \\ 3 \\ 4 \end{bmatrix} - 5 \cdot \frac{1}{2}\begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} = \begin{bmatrix} 1 \\ 2 \\ 3 \\ 4 \end{bmatrix} - \begin{bmatrix} 2.5 \\ 2.5 \\ 2.5 \\ 2.5 \end{bmatrix} = \begin{bmatrix} -1.5 \\ -0.5 \\ 0.5 \\ 1.5 \end{bmatrix}

Normalize:

∣p2∣=(−1.5)2+(−0.5)2+(0.5)2+(1.5)2=2.25+0.25+0.25+2.25=5|p_2| = \sqrt{(-1.5)^2 + (-0.5)^2 + (0.5)^2 + (1.5)^2} = \sqrt{2.25 + 0.25 + 0.25 + 2.25} = \sqrt{5} q2=p2∣p2∣=15[−1.5−0.50.51.5]q_2 = \frac{p_2}{|p_2|} = \frac{1}{\sqrt{5}}\begin{bmatrix} -1.5 \\ -0.5 \\ 0.5 \\ 1.5 \end{bmatrix}

Step 3 — Gram-Schmidt: k=3k = 3

With two orthonormal vectors already in hand, the sum now has two terms:

p3=u3−[(u3Tq1) q1+(u3Tq2) q2]p_3 = u_3 - \left[(u_3^T q_1)\,q_1 + (u_3^T q_2)\,q_2\right]

Compute both projection coefficients:

u3Tq1=[14916]12[1111]=12(1+4+9+16)=302=15u_3^T q_1 = \begin{bmatrix} 1 & 4 & 9 & 16 \end{bmatrix} \frac{1}{2}\begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} = \frac{1}{2}(1 + 4 + 9 + 16) = \frac{30}{2} = 15 u3Tq2=[14916]15[−1.5−0.50.51.5]=15(−1.5−2+4.5+24)=255=55u_3^T q_2 = \begin{bmatrix} 1 & 4 & 9 & 16 \end{bmatrix} \frac{1}{\sqrt{5}}\begin{bmatrix} -1.5 \\ -0.5 \\ 0.5 \\ 1.5 \end{bmatrix} = \frac{1}{\sqrt{5}}(-1.5 - 2 + 4.5 + 24) = \frac{25}{\sqrt{5}} = 5\sqrt{5}

Now subtract both projections:

p3=[14916]−[ 15[0.50.50.50.5]+55⋅15[−1.5−0.50.51.5]]p_3 = \begin{bmatrix} 1 \\ 4 \\ 9 \\ 16 \end{bmatrix} - \left[\, 15 \begin{bmatrix} 0.5 \\ 0.5 \\ 0.5 \\ 0.5 \end{bmatrix} + 5\sqrt{5} \cdot \frac{1}{\sqrt{5}}\begin{bmatrix} -1.5 \\ -0.5 \\ 0.5 \\ 1.5 \end{bmatrix} \right] p3=[14916]−[7.57.57.57.5]−[−7.5−2.52.57.5]=[1−1−11]p_3 = \begin{bmatrix} 1 \\ 4 \\ 9 \\ 16 \end{bmatrix} - \begin{bmatrix} 7.5 \\ 7.5 \\ 7.5 \\ 7.5 \end{bmatrix} - \begin{bmatrix} -7.5 \\ -2.5 \\ 2.5 \\ 7.5 \end{bmatrix} = \begin{bmatrix} 1 \\ -1 \\ -1 \\ 1 \end{bmatrix}

Normalize:

∣p3∣=12+(−1)2+(−1)2+12=4=2|p_3| = \sqrt{1^2 + (-1)^2 + (-1)^2 + 1^2} = \sqrt{4} = 2 q3=p3∣p3∣=12[1−1−11]q_3 = \frac{p_3}{|p_3|} = \frac{1}{2}\begin{bmatrix} 1 \\ -1 \\ -1 \\ 1 \end{bmatrix}

Step 4 — Assemble QQ and QTQ^T

Q=[q1q2q3]=[0.5−1.550.50.5−0.55−0.50.50.55−0.50.51.550.5]Q = \begin{bmatrix} q_1 & q_2 & q_3 \end{bmatrix} = \begin{bmatrix} 0.5 & \dfrac{-1.5}{\sqrt{5}} & 0.5 \\[8pt] 0.5 & \dfrac{-0.5}{\sqrt{5}} & -0.5 \\[8pt] 0.5 & \dfrac{0.5}{\sqrt{5}} & -0.5 \\[8pt] 0.5 & \dfrac{1.5}{\sqrt{5}} & 0.5 \end{bmatrix} QT=[0.50.50.50.5−1.55−0.550.551.550.5−0.5−0.50.5]Q^T = \begin{bmatrix} 0.5 & 0.5 & 0.5 & 0.5 \\[8pt] \dfrac{-1.5}{\sqrt{5}} & \dfrac{-0.5}{\sqrt{5}} & \dfrac{0.5}{\sqrt{5}} & \dfrac{1.5}{\sqrt{5}} \\[8pt] 0.5 & -0.5 & -0.5 & 0.5 \end{bmatrix}

Step 5 — Compute RR

Using Rij=ujTqiR_{ij} = u_j^T q_i for i≤ji \leq j, the matrix has the structure:

R=[u1Tq1u2Tq1u3Tq10u2Tq2u3Tq200u3Tq3]R = \begin{bmatrix} u_1^T q_1 & u_2^T q_1 & u_3^T q_1 \\[6pt] 0 & u_2^T q_2 & u_3^T q_2 \\[6pt] 0 & 0 & u_3^T q_3 \end{bmatrix}

Three of these entries are already available from the Gram-Schmidt steps:

u2Tq1=5,u3Tq1=15,u3Tq2=55u_2^T q_1 = 5, \qquad u_3^T q_1 = 15, \qquad u_3^T q_2 = 5\sqrt{5}

The remaining entries are the diagonal terms, and each one equals the norm ∣pk∣|p_k| found in the corresponding step:

u1Tq1=[1111]12[1111]=42=2u_1^T q_1 = \begin{bmatrix}1 & 1 & 1 & 1\end{bmatrix} \frac{1}{2}\begin{bmatrix}1\\1\\1\\1\end{bmatrix} = \frac{4}{2} = 2 u2Tq2=[1234]15[−1.5−0.50.51.5]=15(−1.5−1+1.5+6)=55=5u_2^T q_2 = \begin{bmatrix}1 & 2 & 3 & 4\end{bmatrix} \frac{1}{\sqrt{5}}\begin{bmatrix}-1.5\\-0.5\\0.5\\1.5\end{bmatrix} = \frac{1}{\sqrt{5}}(-1.5 - 1 + 1.5 + 6) = \frac{5}{\sqrt{5}} = \sqrt{5} u3Tq3=[14916]12[1−1−11]=12(1−4−9+16)=42=2u_3^T q_3 = \begin{bmatrix}1 & 4 & 9 & 16\end{bmatrix} \frac{1}{2}\begin{bmatrix}1\\-1\\-1\\1\end{bmatrix} = \frac{1}{2}(1 - 4 - 9 + 16) = \frac{4}{2} = 2

Therefore:

R=[25150555002]R = \begin{bmatrix} 2 & 5 & 15 \\ 0 & \sqrt{5} & 5\sqrt{5} \\ 0 & 0 & 2 \end{bmatrix}

Step 6 — Compute the right-hand side QTbQ^T b

QTb=[0.50.50.50.5−1.55−0.550.551.550.5−0.5−0.50.5][2364]Q^T b = \begin{bmatrix} 0.5 & 0.5 & 0.5 & 0.5 \\[8pt] \dfrac{-1.5}{\sqrt{5}} & \dfrac{-0.5}{\sqrt{5}} & \dfrac{0.5}{\sqrt{5}} & \dfrac{1.5}{\sqrt{5}} \\[8pt] 0.5 & -0.5 & -0.5 & 0.5 \end{bmatrix} \begin{bmatrix} 2 \\ 3 \\ 6 \\ 4 \end{bmatrix}

Row by row:

Row 1:12(2+3+6+4)=152\text{Row 1:} \quad \frac{1}{2}(2 + 3 + 6 + 4) = \frac{15}{2} Row 2:15(−3−1.5+3+6)=4.55=9510\text{Row 2:} \quad \frac{1}{\sqrt{5}}(-3 - 1.5 + 3 + 6) = \frac{4.5}{\sqrt{5}} = \frac{9\sqrt{5}}{10} Row 3:12(2−3−6+4)=−32\text{Row 3:} \quad \frac{1}{2}(2 - 3 - 6 + 4) = -\frac{3}{2} QTb=[1529510−32]Q^T b = \begin{bmatrix} \dfrac{15}{2} \\[8pt] \dfrac{9\sqrt{5}}{10} \\[8pt] -\dfrac{3}{2} \end{bmatrix}

Step 7 — Solve Rx=QTbRx = Q^T b by back substitution

The system to solve:

[25150555002][a0a1a2]=[1529510−32]\begin{bmatrix} 2 & 5 & 15 \\ 0 & \sqrt{5} & 5\sqrt{5} \\ 0 & 0 & 2 \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \\ a_2 \end{bmatrix} = \begin{bmatrix} \dfrac{15}{2} \\[8pt] \dfrac{9\sqrt{5}}{10} \\[8pt] -\dfrac{3}{2} \end{bmatrix}

From row 3:

2 a2=−32  ⟹  a2=−34=−0.752\,a_2 = -\frac{3}{2} \implies a_2 = -\frac{3}{4} = -0.75

From row 2:

5 a1+55 a2=9510\sqrt{5}\,a_1 + 5\sqrt{5}\,a_2 = \frac{9\sqrt{5}}{10}

Dividing through by 5\sqrt{5} and substituting a2a_2:

a1+5(−34)=910  ⟹  a1=910+154=1820+7520=9320=4.65a_1 + 5\left(-\frac{3}{4}\right) = \frac{9}{10} \implies a_1 = \frac{9}{10} + \frac{15}{4} = \frac{18}{20} + \frac{75}{20} = \frac{93}{20} = 4.65

From row 1:

2 a0+5 a1+15 a2=1522\,a_0 + 5\,a_1 + 15\,a_2 = \frac{15}{2} 2 a0+5(9320)+15(−34)=152  ⟹  2 a0+934−454=1522\,a_0 + 5\left(\frac{93}{20}\right) + 15\left(-\frac{3}{4}\right) = \frac{15}{2} \implies 2\,a_0 + \frac{93}{4} - \frac{45}{4} = \frac{15}{2} 2 a0+12=152  ⟹  2 a0=−92  ⟹  a0=−94=−2.252\,a_0 + 12 = \frac{15}{2} \implies 2\,a_0 = -\frac{9}{2} \implies a_0 = -\frac{9}{4} = -2.25

Result

a0=−94,a1=9320,a2=−34\boxed{a_0 = -\frac{9}{4}, \qquad a_1 = \frac{93}{20}, \qquad a_2 = -\frac{3}{4}}

The least squares polynomial is:

P2(x)=−94+9320 x−34 x2P_2(x) = -\frac{9}{4} + \frac{93}{20}\,x - \frac{3}{4}\,x^2

Verification

Two independent checks confirm the solution. First, the normal equations ATA x=ATbA^T A\,x = A^T b:

ATA=[41030103010030100354],ATb=[1542132]A^T A = \begin{bmatrix} 4 & 10 & 30 \\ 10 & 30 & 100 \\ 30 & 100 & 354 \end{bmatrix}, \qquad A^T b = \begin{bmatrix} 15 \\ 42 \\ 132 \end{bmatrix}

All three equations are satisfied exactly by the coefficients above.

Second, the residuals r=b−Axr = b - Ax at the four data points:

P2(1)=1.65,r1=0.35P2(2)=4.05,r2=−1.05P2(3)=4.95,r3=1.05P2(4)=4.35,r4=−0.35\begin{aligned} P_2(1) &= 1.65, &\quad r_1 &= 0.35 \\ P_2(2) &= 4.05, &\quad r_2 &= -1.05 \\ P_2(3) &= 4.95, &\quad r_3 &= 1.05 \\ P_2(4) &= 4.35, &\quad r_4 &= -0.35 \end{aligned}

The residual vector is orthogonal to every column of AA, since ∑ri=0\sum r_i = 0, ∑xiri=0\sum x_i r_i = 0 and ∑xi2ri=0\sum x_i^2 r_i = 0. This orthogonality is the defining property of the least squares solution, so it is a reliable check on the arithmetic.