When A A A is not square, we cannot solve A x = b Ax = b A x = b directly. The normal equations transform the overdetermined system into a square one by multiplying both sides by A T A^T A T . This produces an n × n n \times n n × n system that always has a unique solution when A A A has full column rank.
Derivation
Start with the overdetermined system:
A m × n x n × 1 = b m × 1 A_{m \times n}\, x_{n \times 1} = b_{m \times 1} A m × n x n × 1 = b m × 1
Multiply both sides on the left by A T A^T A T (an n × m n \times m n × m matrix):
A T A x = A T b A^T A\, x = A^T b A T A x = A T b
Examining the dimensions:
A T ⏟ n × m A ⏟ m × n x ⏟ n × 1 = A T ⏟ n × m b ⏟ m × 1 \underbrace{A^T}_{n \times m} \underbrace{A}_{m \times n} \underbrace{x}_{n \times 1} = \underbrace{A^T}_{n \times m} \underbrace{b}_{m \times 1} n × m A T m × n A n × 1 x = n × m A T m × 1 b
The product A T A A^T A A T A is an n × n n \times n n × n square matrix. This is the system we solve to find the least squares solution.
Note
The system A T A x = A T b A^T A x = A^T b A T A x = A T b is called the normal equations . Solving it gives the x x x that minimizes ∥ A x − b ∥ 2 \|Ax - b\|^2 ∥ A x − b ∥ 2 . You can solve the resulting n × n n \times n n × n system using Gaussian elimination, LU decomposition, or (for small systems) by inverting A T A A^T A A T A directly.
Worked Example 1: Polynomial Fitting
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 of degree 1 that best fits these three points.
Step 1 — Set up the overdetermined system
Substituting each data point into P 1 ( x ) = a 0 + a 1 x P_1(x) = a_0 + a_1 x P 1 ( x ) = a 0 + a 1 x :
P 1 ( − 3 ) = a 0 − 3 a 1 = 0 P_1(-3) = a_0 - 3a_1 = 0 P 1 ( − 3 ) = a 0 − 3 a 1 = 0
P 1 ( 0 ) = a 0 + 0 ⋅ a 1 = 0 P_1(0) = a_0 + 0 \cdot a_1 = 0 P 1 ( 0 ) = a 0 + 0 ⋅ a 1 = 0
P 1 ( 6 ) = a 0 + 6 a 1 = 2 P_1(6) = a_0 + 6a_1 = 2 P 1 ( 6 ) = a 0 + 6 a 1 = 2
In matrix form A x = b Ax = b A x = b :
A = [ 1 − 3 1 0 1 6 ] , x = [ a 0 a 1 ] , b = [ 0 0 2 ] A = \begin{bmatrix} 1 & -3 \\ 1 & 0 \\ 1 & 6 \end{bmatrix}, \qquad x = \begin{bmatrix} a_0 \\ a_1 \end{bmatrix}, \qquad b = \begin{bmatrix} 0 \\ 0 \\ 2 \end{bmatrix} A = 1 1 1 − 3 0 6 , x = [ a 0 a 1 ] , b = 0 0 2
Three equations, two unknowns. This is overdetermined — no exact solution exists in general.
[ 1 1 1 − 3 0 6 ] [ 1 − 3 1 0 1 6 ] [ a 0 a 1 ] = [ 1 1 1 − 3 0 6 ] [ 0 0 2 ] \begin{bmatrix} 1 & 1 & 1 \\ -3 & 0 & 6 \end{bmatrix} \begin{bmatrix} 1 & -3 \\ 1 & 0 \\ 1 & 6 \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \end{bmatrix} = \begin{bmatrix} 1 & 1 & 1 \\ -3 & 0 & 6 \end{bmatrix} \begin{bmatrix} 0 \\ 0 \\ 2 \end{bmatrix} [ 1 − 3 1 0 1 6 ] 1 1 1 − 3 0 6 [ a 0 a 1 ] = [ 1 − 3 1 0 1 6 ] 0 0 2
Compute A T A A^T A A T A :
A T A = [ 1 + 1 + 1 − 3 + 0 + 6 − 3 + 0 + 6 9 + 0 + 36 ] = [ 3 3 3 45 ] A^T A = \begin{bmatrix} 1+1+1 & -3+0+6 \\ -3+0+6 & 9+0+36 \end{bmatrix} = \begin{bmatrix} 3 & 3 \\ 3 & 45 \end{bmatrix} A T A = [ 1 + 1 + 1 − 3 + 0 + 6 − 3 + 0 + 6 9 + 0 + 36 ] = [ 3 3 3 45 ]
Compute A T b A^T b A T b :
A T b = [ 0 + 0 + 2 0 + 0 + 12 ] = [ 2 12 ] A^T b = \begin{bmatrix} 0+0+2 \\ 0+0+12 \end{bmatrix} = \begin{bmatrix} 2 \\ 12 \end{bmatrix} A T b = [ 0 + 0 + 2 0 + 0 + 12 ] = [ 2 12 ]
The normal equations become:
[ 3 3 3 45 ] [ a 0 a 1 ] = [ 2 12 ] \begin{bmatrix} 3 & 3 \\ 3 & 45 \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \end{bmatrix} = \begin{bmatrix} 2 \\ 12 \end{bmatrix} [ 3 3 3 45 ] [ a 0 a 1 ] = [ 2 12 ]
Step 3 — Solve by inverse matrix
[ a 0 a 1 ] = [ 3 3 3 45 ] − 1 [ 2 12 ] \begin{bmatrix} a_0 \\ a_1 \end{bmatrix} = \begin{bmatrix} 3 & 3 \\ 3 & 45 \end{bmatrix}^{-1} \begin{bmatrix} 2 \\ 12 \end{bmatrix} [ a 0 a 1 ] = [ 3 3 3 45 ] − 1 [ 2 12 ]
The determinant is det = 3 × 45 − 3 × 3 = 135 − 9 = 126 \det = 3 \times 45 - 3 \times 3 = 135 - 9 = 126 det = 3 × 45 − 3 × 3 = 135 − 9 = 126 . The inverse is:
[ 3 3 3 45 ] − 1 = 1 126 [ 45 − 3 − 3 3 ] \begin{bmatrix} 3 & 3 \\ 3 & 45 \end{bmatrix}^{-1} = \frac{1}{126} \begin{bmatrix} 45 & -3 \\ -3 & 3 \end{bmatrix} [ 3 3 3 45 ] − 1 = 126 1 [ 45 − 3 − 3 3 ]
Therefore:
[ a 0 a 1 ] = 1 126 [ 45 − 3 − 3 3 ] [ 2 12 ] = 1 126 [ 90 − 36 − 6 + 36 ] = 1 126 [ 54 30 ] \begin{bmatrix} a_0 \\ a_1 \end{bmatrix} = \frac{1}{126} \begin{bmatrix} 45 & -3 \\ -3 & 3 \end{bmatrix} \begin{bmatrix} 2 \\ 12 \end{bmatrix} = \frac{1}{126} \begin{bmatrix} 90 - 36 \\ -6 + 36 \end{bmatrix} = \frac{1}{126} \begin{bmatrix} 54 \\ 30 \end{bmatrix} [ a 0 a 1 ] = 126 1 [ 45 − 3 − 3 3 ] [ 2 12 ] = 126 1 [ 90 − 36 − 6 + 36 ] = 126 1 [ 54 30 ]
Step 4 — Solution
a 0 = 54 126 = 3 7 , a 1 = 30 126 = 5 21 \boxed{a_0 = \frac{54}{126} = \frac{3}{7}, \qquad a_1 = \frac{30}{126} = \frac{5}{21}} a 0 = 126 54 = 7 3 , a 1 = 126 30 = 21 5
The best-fit line 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
Worked Example 2: Four-Equation System
Problem: The following overdetermined system has 4 equations and 3 unknowns. Find the least squares solution.
a 0 + 2 a 1 + 4 a 2 = 3 a_0 + 2a_1 + 4a_2 = 3 a 0 + 2 a 1 + 4 a 2 = 3
a 0 + 3 a 1 + 9 a 2 = 5 a_0 + 3a_1 + 9a_2 = 5 a 0 + 3 a 1 + 9 a 2 = 5
a 0 + 5 a 1 + 25 a 2 = 12 a_0 + 5a_1 + 25a_2 = 12 a 0 + 5 a 1 + 25 a 2 = 12
a 0 + 6 a 1 + 36 a 2 = 15 a_0 + 6a_1 + 36a_2 = 15 a 0 + 6 a 1 + 36 a 2 = 15
Step 1 — Identify the coefficient matrix and right-hand side
A = [ 1 2 4 1 3 9 1 5 25 1 6 36 ] , b = [ 3 5 12 15 ] A = \begin{bmatrix} 1 & 2 & 4 \\ 1 & 3 & 9 \\ 1 & 5 & 25 \\ 1 & 6 & 36 \end{bmatrix}, \qquad b = \begin{bmatrix} 3 \\ 5 \\ 12 \\ 15 \end{bmatrix} A = 1 1 1 1 2 3 5 6 4 9 25 36 , b = 3 5 12 15
A A A is 4 × 3 4 \times 3 4 × 3 . We cannot solve directly — apply the normal equations.
A T A = [ 1 1 1 1 2 3 5 6 4 9 25 36 ] [ 1 2 4 1 3 9 1 5 25 1 6 36 ] A^T A = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 2 & 3 & 5 & 6 \\ 4 & 9 & 25 & 36 \end{bmatrix} \begin{bmatrix} 1 & 2 & 4 \\ 1 & 3 & 9 \\ 1 & 5 & 25 \\ 1 & 6 & 36 \end{bmatrix} A T A = 1 2 4 1 3 9 1 5 25 1 6 36 1 1 1 1 2 3 5 6 4 9 25 36
The 3 × 4 3 \times 4 3 × 4 matrix multiplied by the 4 × 3 4 \times 3 4 × 3 matrix yields a 3 × 3 3 \times 3 3 × 3 matrix:
A T A = [ 4 16 74 16 74 376 74 376 2018 ] A^T A = \begin{bmatrix} 4 & 16 & 74 \\ 16 & 74 & 376 \\ 74 & 376 & 2018 \end{bmatrix} A T A = 4 16 74 16 74 376 74 376 2018
A T b = [ 1 1 1 1 2 3 5 6 4 9 25 36 ] [ 3 5 12 15 ] = [ 35 171 879 ] A^T b = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 2 & 3 & 5 & 6 \\ 4 & 9 & 25 & 36 \end{bmatrix} \begin{bmatrix} 3 \\ 5 \\ 12 \\ 15 \end{bmatrix} = \begin{bmatrix} 35 \\ 171 \\ 879 \end{bmatrix} A T b = 1 2 4 1 3 9 1 5 25 1 6 36 3 5 12 15 = 35 171 879
The normal equations:
[ 4 16 74 16 74 376 74 376 2018 ] [ a 0 a 1 a 2 ] = [ 35 171 879 ] \begin{bmatrix} 4 & 16 & 74 \\ 16 & 74 & 376 \\ 74 & 376 & 2018 \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \\ a_2 \end{bmatrix} = \begin{bmatrix} 35 \\ 171 \\ 879 \end{bmatrix} 4 16 74 16 74 376 74 376 2018 a 0 a 1 a 2 = 35 171 879
Step 3 — Apply Gaussian elimination
Write the augmented matrix:
[ 4 16 74 35 16 74 376 171 74 376 2018 879 ] \left[\begin{array}{ccc|c} 4 & 16 & 74 & 35 \\ 16 & 74 & 376 & 171 \\ 74 & 376 & 2018 & 879 \end{array}\right] 4 16 74 16 74 376 74 376 2018 35 171 879
Apply R 2 ← R 2 − 16 4 R 1 R_2 \leftarrow R_2 - \dfrac{16}{4}\,R_1 R 2 ← R 2 − 4 16 R 1 and R 3 ← R 3 − 74 4 R 1 R_3 \leftarrow R_3 - \dfrac{74}{4}\,R_1 R 3 ← R 3 − 4 74 R 1 :
[ 4 16 74 35 0 10 80 31 0 80 649 231.5 ] \left[\begin{array}{ccc|c} 4 & 16 & 74 & 35 \\ 0 & 10 & 80 & 31 \\ 0 & 80 & 649 & 231.5 \end{array}\right] 4 0 0 16 10 80 74 80 649 35 31 231.5
Apply R 3 ← R 3 − 80 10 R 2 R_3 \leftarrow R_3 - \dfrac{80}{10}\,R_2 R 3 ← R 3 − 10 80 R 2 :
[ 4 16 74 35 0 10 80 31 0 0 9 − 16.5 ] \left[\begin{array}{ccc|c} 4 & 16 & 74 & 35 \\ 0 & 10 & 80 & 31 \\ 0 & 0 & 9 & -16.5 \end{array}\right] 4 0 0 16 10 0 74 80 9 35 31 − 16.5
Step 4 — Back substitution
From row 3:
9 a 2 = − 16.5 ⟹ a 2 = − 16.5 9 ≈ − 1.833 9a_2 = -16.5 \implies a_2 = -\frac{16.5}{9} \approx -1.833 9 a 2 = − 16.5 ⟹ a 2 = − 9 16.5 ≈ − 1.833
From row 2:
10 a 1 + 80 a 2 = 31 10 a 1 = 31 − 80 ( − 1.833 ) = 31 + 146.67 = 177.67 a 1 ≈ 17.767 \begin{aligned}
10a_1 + 80a_2 & = 31 \\
10a_1 & = 31 - 80(-1.833) \\
& = 31 + 146.67 \\
& = 177.67 \\
a_1 & \approx 17.767
\end{aligned} 10 a 1 + 80 a 2 10 a 1 a 1 = 31 = 31 − 80 ( − 1.833 ) = 31 + 146.67 = 177.67 ≈ 17.767
From row 1:
4 a 0 + 16 a 1 + 74 a 2 = 35 4 a 0 = 35 − 16 ( 17.767 ) − 74 ( − 1.833 ) = 35 − 284.27 + 135.67 = − 113.6 a 0 ≈ − 28.4 \begin{aligned}
4a_0 + 16a_1 + 74a_2 & = 35 \\
4a_0 & = 35 - 16(17.767) - 74(-1.833) \\
& = 35 - 284.27 + 135.67 \\
& = -113.6 \\
a_0 & \approx -28.4
\end{aligned} 4 a 0 + 16 a 1 + 74 a 2 4 a 0 a 0 = 35 = 35 − 16 ( 17.767 ) − 74 ( − 1.833 ) = 35 − 284.27 + 135.67 = − 113.6 ≈ − 28.4
a 0 ≈ − 28.4 a_0 \approx -28.4 a 0 ≈ − 28.4
Step 5 — Solution
a 0 ≈ − 28.4 , a 1 ≈ 17.77 , a 2 ≈ − 1.83 \boxed{a_0 \approx -28.4, \qquad a_1 \approx 17.77, \qquad a_2 \approx -1.83} a 0 ≈ − 28.4 , a 1 ≈ 17.77 , a 2 ≈ − 1.83