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 ) = 0 f(-3) = 0 f ( − 3 ) = 0 , f ( 0 ) = 0 f(0) = 0 f ( 0 ) = 0 , f ( 6 ) = 2 f(6) = 2 f ( 6 ) = 2 , find the least squares polynomial P 1 ( x ) = a 0 + a 1 x P_1(x) = a_0 + a_1 x P 1 ( x ) = a 0 + a 1 x using QR decomposition.
The coefficient matrix and right-hand side are:
A = [ 1 − 3 1 0 1 6 ] , b = [ 0 0 2 ] A = \begin{bmatrix} 1 & -3 \\ 1 & 0 \\ 1 & 6 \end{bmatrix}, \qquad b = \begin{bmatrix} 0 \\ 0 \\ 2 \end{bmatrix} A = 1 1 1 − 3 0 6 , b = 0 0 2
A A A has two columns, so the Gram-Schmidt process will run for k = 1 k = 1 k = 1 and k = 2 k = 2 k = 2 .
Label the columns: u 1 = [ 1 1 1 ] u_1 = \begin{bmatrix}1\\1\\1\end{bmatrix} u 1 = 1 1 1 and u 2 = [ − 3 0 6 ] u_2 = \begin{bmatrix}-3\\0\\6\end{bmatrix} u 2 = − 3 0 6 .
Step 1 — Gram-Schmidt: k = 1 k = 1 k = 1
For k = 1 k = 1 k = 1 , the sum is empty, so:
p 1 = u 1 = [ 1 1 1 ] p_1 = u_1 = \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} p 1 = u 1 = 1 1 1
∣ p 1 ∣ = 1 2 + 1 2 + 1 2 = 3 |p_1| = \sqrt{1^2 + 1^2 + 1^2} = \sqrt{3} ∣ p 1 ∣ = 1 2 + 1 2 + 1 2 = 3
q 1 = p 1 ∣ p 1 ∣ = 1 3 [ 1 1 1 ] q_1 = \frac{p_1}{|p_1|} = \frac{1}{\sqrt{3}}\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} q 1 = ∣ p 1 ∣ p 1 = 3 1 1 1 1
Step 2 — Gram-Schmidt: k = 2 k = 2 k = 2
p 2 = u 2 − ( u 2 T q 1 ) q 1 p_2 = u_2 - (u_2^T q_1)\,q_1 p 2 = u 2 − ( u 2 T q 1 ) q 1
First, compute the projection coefficient u 2 T q 1 u_2^T q_1 u 2 T q 1 :
u 2 T q 1 = [ − 3 0 6 ] 1 3 [ 1 1 1 ] = 1 3 ( − 3 + 0 + 6 ) = 3 3 = 3 u_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} u 2 T q 1 = [ − 3 0 6 ] 3 1 1 1 1 = 3 1 ( − 3 + 0 + 6 ) = 3 3 = 3
Now subtract the projection:
p 2 = [ − 3 0 6 ] − 3 ⋅ 1 3 [ 1 1 1 ] = [ − 3 0 6 ] − [ 1 1 1 ] = [ − 4 − 1 5 ] 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} p 2 = − 3 0 6 − 3 ⋅ 3 1 1 1 1 = − 3 0 6 − 1 1 1 = − 4 − 1 5
Normalize:
∣ p 2 ∣ = ( − 4 ) 2 + ( − 1 ) 2 + 5 2 = 16 + 1 + 25 = 42 |p_2| = \sqrt{(-4)^2 + (-1)^2 + 5^2} = \sqrt{16 + 1 + 25} = \sqrt{42} ∣ p 2 ∣ = ( − 4 ) 2 + ( − 1 ) 2 + 5 2 = 16 + 1 + 25 = 42
q 2 = p 2 ∣ p 2 ∣ = 1 42 [ − 4 − 1 5 ] q_2 = \frac{p_2}{|p_2|} = \frac{1}{\sqrt{42}}\begin{bmatrix} -4 \\ -1 \\ 5 \end{bmatrix} q 2 = ∣ p 2 ∣ p 2 = 42 1 − 4 − 1 5
Step 3 — Assemble Q Q Q and Q T Q^T Q T
Q = [ q 1 q 2 ] = [ 1 3 − 4 42 1 3 − 1 42 1 3 5 42 ] 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} Q = [ q 1 q 2 ] = 3 1 3 1 3 1 42 − 4 42 − 1 42 5
Q T = [ 1 3 1 3 1 3 − 4 42 − 1 42 5 42 ] 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} Q T = 3 1 42 − 4 3 1 42 − 1 3 1 42 5
Step 4 — Compute R R R
Using R i j = u j T q i R_{ij} = u_j^T q_i R ij = u j T q i for i ≤ j i \leq j i ≤ j :
u 1 T q 1 = [ 1 1 1 ] 1 3 [ 1 1 1 ] = 3 3 = 3 u_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} u 1 T q 1 = [ 1 1 1 ] 3 1 1 1 1 = 3 3 = 3
u 2 T q 1 = 3 (computed in Step 2) u_2^T q_1 = \sqrt{3} \quad \text{(computed in Step 2)} u 2 T q 1 = 3 (computed in Step 2)
u 2 T q 2 = [ − 3 0 6 ] 1 42 [ − 4 − 1 5 ] = 1 42 ( 12 + 0 + 30 ) = 42 42 = 42 u_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} u 2 T q 2 = [ − 3 0 6 ] 42 1 − 4 − 1 5 = 42 1 ( 12 + 0 + 30 ) = 42 42 = 42
Therefore:
R = [ 3 3 0 42 ] R = \begin{bmatrix} \sqrt{3} & \sqrt{3} \\ 0 & \sqrt{42} \end{bmatrix} R = [ 3 0 3 42 ]
Step 5 — Compute the right-hand side Q T b Q^T b Q T b
Q T b = [ 1 3 1 3 1 3 − 4 42 − 1 42 5 42 ] [ 0 0 2 ] = [ 2 3 10 42 ] 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} Q T b = 3 1 42 − 4 3 1 42 − 1 3 1 42 5 0 0 2 = 3 2 42 10
Step 6 — Solve R x = Q T b Rx = Q^T b R x = Q T b by back substitution
The system to solve:
[ 3 3 0 42 ] [ a 0 a 1 ] = [ 2 3 10 42 ] \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} [ 3 0 3 42 ] [ a 0 a 1 ] = 3 2 42 10
From row 2:
42 a 1 = 10 42 ⟹ a 1 = 10 42 ⋅ 42 = 10 42 = 5 21 \sqrt{42}\,a_1 = \frac{10}{\sqrt{42}} \implies a_1 = \frac{10}{\sqrt{42} \cdot \sqrt{42}} = \frac{10}{42} = \frac{5}{21} 42 a 1 = 42 10 ⟹ a 1 = 42 ⋅ 42 10 = 42 10 = 21 5
From row 1:
3 a 0 + 3 a 1 = 2 3 ⟹ 3 a 0 = 2 3 − 3 ⋅ 5 21 = 2 3 − 5 3 21 \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 a 0 + 3 a 1 = 3 2 ⟹ 3 a 0 = 3 2 − 3 ⋅ 21 5 = 3 2 − 21 5 3
3 a 0 = 2 3 − 5 7 3 = 14 7 3 − 5 7 3 = 9 7 3 \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}} 3 a 0 = 3 2 − 7 3 5 = 7 3 14 − 7 3 5 = 7 3 9
a 0 = 9 7 3 ⋅ 3 = 9 21 = 3 7 a_0 = \frac{9}{7\sqrt{3} \cdot \sqrt{3}} = \frac{9}{21} = \frac{3}{7} a 0 = 7 3 ⋅ 3 9 = 21 9 = 7 3
Result
a 0 = 3 7 , a 1 = 5 21 \boxed{a_0 = \frac{3}{7}, \qquad a_1 = \frac{5}{21}} a 0 = 7 3 , a 1 = 21 5
The least squares polynomial is:
P 1 ( x ) = 3 7 + 5 21 x P_1(x) = \frac{3}{7} + \frac{5}{21}\,x P 1 ( x ) = 7 3 + 21 5 x
Note
This matches the result from the normal equations in the previous section. Both approaches are mathematically equivalent — they minimize the same sum of squared residuals. QR decomposition avoids squaring the condition number of A A A (which happens when forming A T A A^T A A T A ), making it preferable for ill-conditioned problems.
Example 2
Problem Setup
Problem: Solve the following system in the least squares sense using QR decomposition.
a 0 + a 1 + a 2 = 2 a 0 + 2 a 1 + 4 a 2 = 3 a 0 + 3 a 1 + 9 a 2 = 6 a 0 + 4 a 1 + 16 a 2 = 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} a 0 + a 1 + a 2 a 0 + 2 a 1 + 4 a 2 a 0 + 3 a 1 + 9 a 2 a 0 + 4 a 1 + 16 a 2 = 2 = 3 = 6 = 4
In matrix form A x = b Ax = b A x = b :
[ 1 1 1 1 2 4 1 3 9 1 4 16 ] [ a 0 a 1 a 2 ] = [ 2 3 6 4 ] \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} 1 1 1 1 1 2 3 4 1 4 9 16 a 0 a 1 a 2 = 2 3 6 4
A A A is a 4 × 3 4 \times 3 4 × 3 matrix, so this is an overdetermined system with four equations and three unknowns. It is the quadratic fit P 2 ( x ) = a 0 + a 1 x + a 2 x 2 P_2(x) = a_0 + a_1 x + a_2 x^2 P 2 ( x ) = a 0 + a 1 x + a 2 x 2 through the data f ( 1 ) = 2 f(1) = 2 f ( 1 ) = 2 , f ( 2 ) = 3 f(2) = 3 f ( 2 ) = 3 , f ( 3 ) = 6 f(3) = 6 f ( 3 ) = 6 , f ( 4 ) = 4 f(4) = 4 f ( 4 ) = 4 .
A A A has three columns, so the Gram-Schmidt process will run for k = 1 k = 1 k = 1 , k = 2 k = 2 k = 2 and k = 3 k = 3 k = 3 . There will be three steps.
Label the columns:
u 1 = [ 1 1 1 1 ] , u 2 = [ 1 2 3 4 ] , u 3 = [ 1 4 9 16 ] 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} u 1 = 1 1 1 1 , u 2 = 1 2 3 4 , u 3 = 1 4 9 16
Step 1 — Gram-Schmidt: k = 1 k = 1 k = 1
For k = 1 k = 1 k = 1 , the sum is empty, so:
p 1 = u 1 = [ 1 1 1 1 ] p_1 = u_1 = \begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} p 1 = u 1 = 1 1 1 1
∣ p 1 ∣ = 1 2 + 1 2 + 1 2 + 1 2 = 4 = 2 |p_1| = \sqrt{1^2 + 1^2 + 1^2 + 1^2} = \sqrt{4} = 2 ∣ p 1 ∣ = 1 2 + 1 2 + 1 2 + 1 2 = 4 = 2
q 1 = p 1 ∣ p 1 ∣ = 1 2 [ 1 1 1 1 ] = [ 0.5 0.5 0.5 0.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} q 1 = ∣ p 1 ∣ p 1 = 2 1 1 1 1 1 = 0.5 0.5 0.5 0.5
Step 2 — Gram-Schmidt: k = 2 k = 2 k = 2
p 2 = u 2 − ( u 2 T q 1 ) q 1 p_2 = u_2 - (u_2^T q_1)\,q_1 p 2 = u 2 − ( u 2 T q 1 ) q 1
First, compute the projection coefficient u 2 T q 1 u_2^T q_1 u 2 T q 1 :
u 2 T q 1 = [ 1 2 3 4 ] 1 2 [ 1 1 1 1 ] = 1 2 ( 1 + 2 + 3 + 4 ) = 10 2 = 5 u_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 u 2 T q 1 = [ 1 2 3 4 ] 2 1 1 1 1 1 = 2 1 ( 1 + 2 + 3 + 4 ) = 2 10 = 5
Now subtract the projection:
p 2 = [ 1 2 3 4 ] − 5 ⋅ 1 2 [ 1 1 1 1 ] = [ 1 2 3 4 ] − [ 2.5 2.5 2.5 2.5 ] = [ − 1.5 − 0.5 0.5 1.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} p 2 = 1 2 3 4 − 5 ⋅ 2 1 1 1 1 1 = 1 2 3 4 − 2.5 2.5 2.5 2.5 = − 1.5 − 0.5 0.5 1.5
Normalize:
∣ p 2 ∣ = ( − 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} ∣ p 2 ∣ = ( − 1.5 ) 2 + ( − 0.5 ) 2 + ( 0.5 ) 2 + ( 1.5 ) 2 = 2.25 + 0.25 + 0.25 + 2.25 = 5
q 2 = p 2 ∣ p 2 ∣ = 1 5 [ − 1.5 − 0.5 0.5 1.5 ] q_2 = \frac{p_2}{|p_2|} = \frac{1}{\sqrt{5}}\begin{bmatrix} -1.5 \\ -0.5 \\ 0.5 \\ 1.5 \end{bmatrix} q 2 = ∣ p 2 ∣ p 2 = 5 1 − 1.5 − 0.5 0.5 1.5
Step 3 — Gram-Schmidt: k = 3 k = 3 k = 3
With two orthonormal vectors already in hand, the sum now has two terms:
p 3 = u 3 − [ ( u 3 T q 1 ) q 1 + ( u 3 T q 2 ) q 2 ] p_3 = u_3 - \left[(u_3^T q_1)\,q_1 + (u_3^T q_2)\,q_2\right] p 3 = u 3 − [ ( u 3 T q 1 ) q 1 + ( u 3 T q 2 ) q 2 ]
Compute both projection coefficients:
u 3 T q 1 = [ 1 4 9 16 ] 1 2 [ 1 1 1 1 ] = 1 2 ( 1 + 4 + 9 + 16 ) = 30 2 = 15 u_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 u 3 T q 1 = [ 1 4 9 16 ] 2 1 1 1 1 1 = 2 1 ( 1 + 4 + 9 + 16 ) = 2 30 = 15
u 3 T q 2 = [ 1 4 9 16 ] 1 5 [ − 1.5 − 0.5 0.5 1.5 ] = 1 5 ( − 1.5 − 2 + 4.5 + 24 ) = 25 5 = 5 5 u_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} u 3 T q 2 = [ 1 4 9 16 ] 5 1 − 1.5 − 0.5 0.5 1.5 = 5 1 ( − 1.5 − 2 + 4.5 + 24 ) = 5 25 = 5 5
Now subtract both projections:
p 3 = [ 1 4 9 16 ] − [ 15 [ 0.5 0.5 0.5 0.5 ] + 5 5 ⋅ 1 5 [ − 1.5 − 0.5 0.5 1.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] p 3 = 1 4 9 16 − 15 0.5 0.5 0.5 0.5 + 5 5 ⋅ 5 1 − 1.5 − 0.5 0.5 1.5
p 3 = [ 1 4 9 16 ] − [ 7.5 7.5 7.5 7.5 ] − [ − 7.5 − 2.5 2.5 7.5 ] = [ 1 − 1 − 1 1 ] 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} p 3 = 1 4 9 16 − 7.5 7.5 7.5 7.5 − − 7.5 − 2.5 2.5 7.5 = 1 − 1 − 1 1
Normalize:
∣ p 3 ∣ = 1 2 + ( − 1 ) 2 + ( − 1 ) 2 + 1 2 = 4 = 2 |p_3| = \sqrt{1^2 + (-1)^2 + (-1)^2 + 1^2} = \sqrt{4} = 2 ∣ p 3 ∣ = 1 2 + ( − 1 ) 2 + ( − 1 ) 2 + 1 2 = 4 = 2
q 3 = p 3 ∣ p 3 ∣ = 1 2 [ 1 − 1 − 1 1 ] q_3 = \frac{p_3}{|p_3|} = \frac{1}{2}\begin{bmatrix} 1 \\ -1 \\ -1 \\ 1 \end{bmatrix} q 3 = ∣ p 3 ∣ p 3 = 2 1 1 − 1 − 1 1
Step 4 — Assemble Q Q Q and Q T Q^T Q T
Q = [ q 1 q 2 q 3 ] = [ 0.5 − 1.5 5 0.5 0.5 − 0.5 5 − 0.5 0.5 0.5 5 − 0.5 0.5 1.5 5 0.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} Q = [ q 1 q 2 q 3 ] = 0.5 0.5 0.5 0.5 5 − 1.5 5 − 0.5 5 0.5 5 1.5 0.5 − 0.5 − 0.5 0.5
Q T = [ 0.5 0.5 0.5 0.5 − 1.5 5 − 0.5 5 0.5 5 1.5 5 0.5 − 0.5 − 0.5 0.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} Q T = 0.5 5 − 1.5 0.5 0.5 5 − 0.5 − 0.5 0.5 5 0.5 − 0.5 0.5 5 1.5 0.5
Step 5 — Compute R R R
Using R i j = u j T q i R_{ij} = u_j^T q_i R ij = u j T q i for i ≤ j i \leq j i ≤ j , the matrix has the structure:
R = [ u 1 T q 1 u 2 T q 1 u 3 T q 1 0 u 2 T q 2 u 3 T q 2 0 0 u 3 T q 3 ] 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} R = u 1 T q 1 0 0 u 2 T q 1 u 2 T q 2 0 u 3 T q 1 u 3 T q 2 u 3 T q 3
Three of these entries are already available from the Gram-Schmidt steps:
u 2 T q 1 = 5 , u 3 T q 1 = 15 , u 3 T q 2 = 5 5 u_2^T q_1 = 5, \qquad u_3^T q_1 = 15, \qquad u_3^T q_2 = 5\sqrt{5} u 2 T q 1 = 5 , u 3 T q 1 = 15 , u 3 T q 2 = 5 5
The remaining entries are the diagonal terms, and each one equals the norm ∣ p k ∣ |p_k| ∣ p k ∣ found in the corresponding step:
u 1 T q 1 = [ 1 1 1 1 ] 1 2 [ 1 1 1 1 ] = 4 2 = 2 u_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 u 1 T q 1 = [ 1 1 1 1 ] 2 1 1 1 1 1 = 2 4 = 2
u 2 T q 2 = [ 1 2 3 4 ] 1 5 [ − 1.5 − 0.5 0.5 1.5 ] = 1 5 ( − 1.5 − 1 + 1.5 + 6 ) = 5 5 = 5 u_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} u 2 T q 2 = [ 1 2 3 4 ] 5 1 − 1.5 − 0.5 0.5 1.5 = 5 1 ( − 1.5 − 1 + 1.5 + 6 ) = 5 5 = 5
u 3 T q 3 = [ 1 4 9 16 ] 1 2 [ 1 − 1 − 1 1 ] = 1 2 ( 1 − 4 − 9 + 16 ) = 4 2 = 2 u_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 u 3 T q 3 = [ 1 4 9 16 ] 2 1 1 − 1 − 1 1 = 2 1 ( 1 − 4 − 9 + 16 ) = 2 4 = 2
Therefore:
R = [ 2 5 15 0 5 5 5 0 0 2 ] R = \begin{bmatrix} 2 & 5 & 15 \\ 0 & \sqrt{5} & 5\sqrt{5} \\ 0 & 0 & 2 \end{bmatrix} R = 2 0 0 5 5 0 15 5 5 2
Step 6 — Compute the right-hand side Q T b Q^T b Q T b
Q T b = [ 0.5 0.5 0.5 0.5 − 1.5 5 − 0.5 5 0.5 5 1.5 5 0.5 − 0.5 − 0.5 0.5 ] [ 2 3 6 4 ] 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} Q T b = 0.5 5 − 1.5 0.5 0.5 5 − 0.5 − 0.5 0.5 5 0.5 − 0.5 0.5 5 1.5 0.5 2 3 6 4
Row by row:
Row 1: 1 2 ( 2 + 3 + 6 + 4 ) = 15 2 \text{Row 1:} \quad \frac{1}{2}(2 + 3 + 6 + 4) = \frac{15}{2} Row 1: 2 1 ( 2 + 3 + 6 + 4 ) = 2 15
Row 2: 1 5 ( − 3 − 1.5 + 3 + 6 ) = 4.5 5 = 9 5 10 \text{Row 2:} \quad \frac{1}{\sqrt{5}}(-3 - 1.5 + 3 + 6) = \frac{4.5}{\sqrt{5}} = \frac{9\sqrt{5}}{10} Row 2: 5 1 ( − 3 − 1.5 + 3 + 6 ) = 5 4.5 = 10 9 5
Row 3: 1 2 ( 2 − 3 − 6 + 4 ) = − 3 2 \text{Row 3:} \quad \frac{1}{2}(2 - 3 - 6 + 4) = -\frac{3}{2} Row 3: 2 1 ( 2 − 3 − 6 + 4 ) = − 2 3
Q T b = [ 15 2 9 5 10 − 3 2 ] Q^T b = \begin{bmatrix} \dfrac{15}{2} \\[8pt] \dfrac{9\sqrt{5}}{10} \\[8pt] -\dfrac{3}{2} \end{bmatrix} Q T b = 2 15 10 9 5 − 2 3
Step 7 — Solve R x = Q T b Rx = Q^T b R x = Q T b by back substitution
The system to solve:
[ 2 5 15 0 5 5 5 0 0 2 ] [ a 0 a 1 a 2 ] = [ 15 2 9 5 10 − 3 2 ] \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} 2 0 0 5 5 0 15 5 5 2 a 0 a 1 a 2 = 2 15 10 9 5 − 2 3
From row 3:
2 a 2 = − 3 2 ⟹ a 2 = − 3 4 = − 0.75 2\,a_2 = -\frac{3}{2} \implies a_2 = -\frac{3}{4} = -0.75 2 a 2 = − 2 3 ⟹ a 2 = − 4 3 = − 0.75
From row 2:
5 a 1 + 5 5 a 2 = 9 5 10 \sqrt{5}\,a_1 + 5\sqrt{5}\,a_2 = \frac{9\sqrt{5}}{10} 5 a 1 + 5 5 a 2 = 10 9 5
Dividing through by 5 \sqrt{5} 5 and substituting a 2 a_2 a 2 :
a 1 + 5 ( − 3 4 ) = 9 10 ⟹ a 1 = 9 10 + 15 4 = 18 20 + 75 20 = 93 20 = 4.65 a_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 a 1 + 5 ( − 4 3 ) = 10 9 ⟹ a 1 = 10 9 + 4 15 = 20 18 + 20 75 = 20 93 = 4.65
From row 1:
2 a 0 + 5 a 1 + 15 a 2 = 15 2 2\,a_0 + 5\,a_1 + 15\,a_2 = \frac{15}{2} 2 a 0 + 5 a 1 + 15 a 2 = 2 15
2 a 0 + 5 ( 93 20 ) + 15 ( − 3 4 ) = 15 2 ⟹ 2 a 0 + 93 4 − 45 4 = 15 2 2\,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 a 0 + 5 ( 20 93 ) + 15 ( − 4 3 ) = 2 15 ⟹ 2 a 0 + 4 93 − 4 45 = 2 15
2 a 0 + 12 = 15 2 ⟹ 2 a 0 = − 9 2 ⟹ a 0 = − 9 4 = − 2.25 2\,a_0 + 12 = \frac{15}{2} \implies 2\,a_0 = -\frac{9}{2} \implies a_0 = -\frac{9}{4} = -2.25 2 a 0 + 12 = 2 15 ⟹ 2 a 0 = − 2 9 ⟹ a 0 = − 4 9 = − 2.25
Result
a 0 = − 9 4 , a 1 = 93 20 , a 2 = − 3 4 \boxed{a_0 = -\frac{9}{4}, \qquad a_1 = \frac{93}{20}, \qquad a_2 = -\frac{3}{4}} a 0 = − 4 9 , a 1 = 20 93 , a 2 = − 4 3
The least squares polynomial is:
P 2 ( x ) = − 9 4 + 93 20 x − 3 4 x 2 P_2(x) = -\frac{9}{4} + \frac{93}{20}\,x - \frac{3}{4}\,x^2 P 2 ( x ) = − 4 9 + 20 93 x − 4 3 x 2
Verification
Two independent checks confirm the solution. First, the normal equations A T A x = A T b A^T A\,x = A^T b A T A x = A T b :
A T A = [ 4 10 30 10 30 100 30 100 354 ] , A T b = [ 15 42 132 ] 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} A T A = 4 10 30 10 30 100 30 100 354 , A T b = 15 42 132
All three equations are satisfied exactly by the coefficients above.
Second, the residuals r = b − A x r = b - Ax r = b − A x at the four data points:
P 2 ( 1 ) = 1.65 , r 1 = 0.35 P 2 ( 2 ) = 4.05 , r 2 = − 1.05 P 2 ( 3 ) = 4.95 , r 3 = 1.05 P 2 ( 4 ) = 4.35 , r 4 = − 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} P 2 ( 1 ) P 2 ( 2 ) P 2 ( 3 ) P 2 ( 4 ) = 1.65 , = 4.05 , = 4.95 , = 4.35 , r 1 r 2 r 3 r 4 = 0.35 = − 1.05 = 1.05 = − 0.35
The residual vector is orthogonal to every column of A A A , since ∑ r i = 0 \sum r_i = 0 ∑ r i = 0 , ∑ x i r i = 0 \sum x_i r_i = 0 ∑ x i r i = 0 and ∑ x i 2 r i = 0 \sum x_i^2 r_i = 0 ∑ 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.
Note
The structure is identical in both examples. Only the column count changes. An m × n m \times n m × n matrix A A A gives n n n Gram-Schmidt steps, an m × n m \times n m × n matrix Q Q Q with orthonormal columns and an n × n n \times n n × n upper triangular matrix R R R solved by back substitution from the bottom row upward. The number of steps is set by the columns of A A A , never by the rows.