Tutorials › Linear Algebra › The Normal Equations For A Candidate Fit

Least Squares and QR Factorization · Tutorial 684 of 1000

The Normal Equations For A Candidate Fit

Learn how orthogonality of a residual to the columns of a matrix produces the normal equations and how to form and check those equations.

Intermediate 9 min read

What You'll Learn

  • Define the residual for an overdetermined system and express its orthogonality to the columns of the coefficient matrix.
  • Derive the normal equations from the column-by-column dot-product conditions.
  • Build the matrix and right-hand side of the normal equations using exact arithmetic.
  • Check candidate coefficient vectors by computing their residuals and dot products.
  • Recognize that the normal equations can be formed even when the columns are dependent.

From a Residual to Equations for a Candidate Fit

For an overdetermined system, a matrix equation \(\mathbf{A}\mathbf{x}=\mathbf{b}\) has more equations than unknowns. It may have no exact solution, so we instead seek a vector \(\mathbf{x}\) whose fitted vector \(\mathbf{A}\mathbf{x}\) approximates \(\mathbf{b}\). The residual is the difference between the data vector and the fitted vector. For a least-squares fit, residual orthogonality gives a useful condition: the residual must be orthogonal to every column of \(\mathbf{A}\). This tutorial translates that geometric condition into a system of equations for a candidate fit.

Let \(\mathbf{A}\in\mathbb{R}^{m\times n}\), where \(m>n\), and let \(\mathbf{b}\in\mathbb{R}^m\). Write the columns of \(\mathbf{A}\) as \(\mathbf{a}_1,\ldots,\mathbf{a}_n\), and let \(\mathbf{x}=(x_1,\ldots,x_n)^{\top}\in\mathbb{R}^n\). The fitted vector is \(\mathbf{A}\mathbf{x}=x_1\mathbf{a}_1+\cdots+x_n\mathbf{a}_n\), which lies in the column space of \(\mathbf{A}\).

Definition: The residual associated with \(\mathbf{x}\) is \(\mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{x}\). It is orthogonal to every column of \(\mathbf{A}\) if \(\mathbf{a}_j^{\top}\mathbf{r}=0\) for every \(j=1,\ldots,n\). This condition is also written \(\mathbf{r}\perp\operatorname{Col}\mathbf{A}\).

The columns are vectors in \(\mathbb{R}^m\), so each orthogonality condition is a dot product in \(\mathbb{R}^m\). There is one such condition for each unknown coefficient \(x_j\). The goal is to collect all \(n\) scalar conditions into a single matrix equation.

Collecting the Orthogonality Conditions

Theorem: Let \(\mathbf{A}\in\mathbb{R}^{m\times n}\) have columns \(\mathbf{a}_1,\ldots,\mathbf{a}_n\), and let \(\mathbf{r}\in\mathbb{R}^m\). Then \(\mathbf{r}\) is orthogonal to every column of \(\mathbf{A}\) if and only if \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\).

Proof. The \(j\)th entry of \(\mathbf{A}^{\top}\mathbf{r}\) is the dot product of the \(j\)th column of \(\mathbf{A}\) with \(\mathbf{r}\), so \((\mathbf{A}^{\top}\mathbf{r})_j=\mathbf{a}_j^{\top}\mathbf{r}\). If \(\mathbf{r}\) is orthogonal to every column, every entry of \(\mathbf{A}^{\top}\mathbf{r}\) is zero, and thus \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\). Conversely, if \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\), each of its entries is zero, so \(\mathbf{a}_j^{\top}\mathbf{r}=0\) for every \(j\). This is exactly orthogonality to every column. \(\square\)

For a candidate coefficient vector \(\mathbf{x}\), substitute its residual \(\mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{x}\) into this result. The requirement becomes \(\mathbf{A}^{\top}(\mathbf{b}-\mathbf{A}\mathbf{x})=\mathbf{0}\). Distributing \(\mathbf{A}^{\top}\) and moving the term involving \(\mathbf{x}\) to the other side gives the normal equations.

Definition: The normal equations for \(\mathbf{A}\mathbf{x}\approx\mathbf{b}\) are \(\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=\mathbf{A}^{\top}\mathbf{b}\). They are the equations obtained by requiring the residual \(\mathbf{b}-\mathbf{A}\mathbf{x}\) to be orthogonal to every column of \(\mathbf{A}\).
Proposition: For any \(\mathbf{A}\in\mathbb{R}^{m\times n}\), \(\mathbf{b}\in\mathbb{R}^m\), and \(\mathbf{x}\in\mathbb{R}^n\), the residual \(\mathbf{b}-\mathbf{A}\mathbf{x}\) is orthogonal to every column of \(\mathbf{A}\) if and only if \(\mathbf{x}\) satisfies the normal equations.

Proof. By the theorem, residual orthogonality is equivalent to \(\mathbf{A}^{\top}(\mathbf{b}-\mathbf{A}\mathbf{x})=\mathbf{0}\). Distributing the product gives \(\mathbf{A}^{\top}\mathbf{b}-\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=\mathbf{0}\), which is equivalent, after adding \(\mathbf{A}^{\top}\mathbf{A}\mathbf{x}\) to both sides, to \(\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=\mathbf{A}^{\top}\mathbf{b}\). Reversing these algebraic steps proves the converse as well. \(\square\)

The dimensions help keep the equation oriented correctly. The product \(\mathbf{A}^{\top}\mathbf{A}\) is \(n\times n\), \(\mathbf{x}\) has \(n\) entries, and \(\mathbf{A}^{\top}\mathbf{b}\) has \(n\) entries. The resulting system has \(n\) equations in \(n\) unknowns, even though the original system has \(m>n\) equations. This change in shape does not by itself guarantee a unique solution; the columns of \(\mathbf{A}\) may be dependent.

Forming and Checking the Equations

Worked Example: Two Columns and Three Observations

For the overdetermined system below, form the normal equations, find a candidate coefficient vector, and check its residual for orthogonality.

$$ \mathbf{A}= \begin{bmatrix} 1&0\\ 1&1\\ 1&2 \end{bmatrix}, \qquad \mathbf{b}= \begin{bmatrix} 1\\2\\2 \end{bmatrix}. $$

The columns are \(\mathbf{a}_1=(1,1,1)^{\top}\) and \(\mathbf{a}_2=(0,1,2)^{\top}\). Their dot products give the entries of \(\mathbf{A}^{\top}\mathbf{A}\): \(\mathbf{a}_1^{\top}\mathbf{a}_1=1+1+1=3\), \(\mathbf{a}_1^{\top}\mathbf{a}_2=0+1+2=3\), and \(\mathbf{a}_2^{\top}\mathbf{a}_2=0+1+4=5\). The entries of \(\mathbf{A}^{\top}\mathbf{b}\) are \(\mathbf{a}_1^{\top}\mathbf{b}=1+2+2=5\) and \(\mathbf{a}_2^{\top}\mathbf{b}=0+2+4=6\). Hence

$$ \begin{bmatrix} 3&3\\ 3&5 \end{bmatrix} \begin{bmatrix} x_1\\x_2 \end{bmatrix} = \begin{bmatrix} 5\\6 \end{bmatrix}, \qquad \begin{aligned} 3x_1+3x_2&=5,\\ 3x_1+5x_2&=6. \end{aligned} $$

Subtract the first scalar equation from the second to obtain \(2x_2=1\), so \(x_2=\tfrac12\). Substitution into \(3x_1+3x_2=5\) gives \(3x_1+\tfrac32=5\), hence \(3x_1=\tfrac72\) and \(x_1=\tfrac76\). The candidate is \(\mathbf{x}=(\tfrac76,\tfrac12)^{\top}\).

Check the equations: multiplying gives \(3(\tfrac76)+3(\tfrac12)=\tfrac72+\tfrac32=5\) and \(3(\tfrac76)+5(\tfrac12)=\tfrac72+\tfrac52=6\). The residual is

$$ \mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{x} = \begin{bmatrix}1\\2\\2\end{bmatrix} - \begin{bmatrix}7/6\\5/3\\13/6\end{bmatrix} = \begin{bmatrix}-1/6\\1/3\\-1/6\end{bmatrix}. $$

Its dot products with the columns are \(\mathbf{a}_1^{\top}\mathbf{r}=-\tfrac16+\tfrac13-\tfrac16=0\) and \(\mathbf{a}_2^{\top}\mathbf{r}=0+\tfrac13+2(-\tfrac16)=0\). Thus the residual has the required orthogonality.

This calculation illustrates a reliable order of work: compute the column dot products to build the two sides, solve for \(\mathbf{x}\), then calculate \(\mathbf{b}-\mathbf{A}\mathbf{x}\) and check its dot products. Checking the residual directly is valuable because it tests the geometric condition itself, not just an earlier line of algebra.

Worked Example: A Candidate with Integer Entries

Form and solve the normal equations for the following matrix and vector, then verify the residual.

$$ \mathbf{A}= \begin{bmatrix} 1&0\\ 0&1\\ 1&1\\ 0&1 \end{bmatrix}, \qquad \mathbf{b}= \begin{bmatrix} 2\\1\\4\\3 \end{bmatrix}. $$

The columns are \(\mathbf{a}_1=(1,0,1,0)^{\top}\) and \(\mathbf{a}_2=(0,1,1,1)^{\top}\). Therefore \(\mathbf{a}_1^{\top}\mathbf{a}_1=1+0+1+0=2\), \(\mathbf{a}_1^{\top}\mathbf{a}_2=0+0+1+0=1\), and \(\mathbf{a}_2^{\top}\mathbf{a}_2=0+1+1+1=3\). Also, \(\mathbf{a}_1^{\top}\mathbf{b}=2+0+4+0=6\) and \(\mathbf{a}_2^{\top}\mathbf{b}=0+1+4+3=8\). The equations are

$$ \begin{bmatrix} 2&1\\ 1&3 \end{bmatrix} \begin{bmatrix} x_1\\x_2 \end{bmatrix} = \begin{bmatrix} 6\\8 \end{bmatrix}, \qquad \begin{aligned} 2x_1+x_2&=6,\\ x_1+3x_2&=8. \end{aligned} $$

From the first equation \(x_2=6-2x_1\). Substitute this into the second: \(x_1+3(6-2x_1)=8\), so \(x_1+18-6x_1=8\), \(-5x_1=-10\), and \(x_1=2\). Then \(x_2=6-2(2)=2\). Thus \(\mathbf{x}=(2,2)^{\top}\).

Check: \(\mathbf{A}\mathbf{x}=(2,2,4,2)^{\top}\), so \(\mathbf{r}=(0,-1,0,1)^{\top}\). The column dot products are \(\mathbf{a}_1^{\top}\mathbf{r}=0+0+0+0=0\) and \(\mathbf{a}_2^{\top}\mathbf{r}=0-1+0+1=0\). The residual is orthogonal to both columns.

Dependent Columns Still Give Normal Equations

The normal equations can be formed without assuming that the columns of \(\mathbf{A}\) are independent. When they are dependent, the equations may themselves be dependent, and solving them may not specify a unique coefficient vector. The dot-product derivation still applies: every candidate satisfying the normal equations has a residual orthogonal to each column, by the proposition. The next example demonstrates the distinction between forming the equations and claiming that they determine one answer.

Worked Example: Dependent Columns

Form the normal equations for this overdetermined system and check one candidate that satisfies them.

$$ \mathbf{A}= \begin{bmatrix} 1&2\\ 0&0\\ 1&2 \end{bmatrix}, \qquad \mathbf{b}= \begin{bmatrix} 1\\1\\3 \end{bmatrix}. $$

The columns are \(\mathbf{a}_1=(1,0,1)^{\top}\) and \(\mathbf{a}_2=(2,0,2)^{\top}\). Their dot products are \(\mathbf{a}_1^{\top}\mathbf{a}_1=1+0+1=2\), \(\mathbf{a}_1^{\top}\mathbf{a}_2=2+0+2=4\), and \(\mathbf{a}_2^{\top}\mathbf{a}_2=4+0+4=8\). The right-hand entries are \(\mathbf{a}_1^{\top}\mathbf{b}=1+0+3=4\) and \(\mathbf{a}_2^{\top}\mathbf{b}=2+0+6=8\). Thus the normal equations are

$$ \begin{bmatrix} 2&4\\ 4&8 \end{bmatrix} \begin{bmatrix} x_1\\x_2 \end{bmatrix} = \begin{bmatrix} 4\\8 \end{bmatrix}, \qquad \begin{aligned} 2x_1+4x_2&=4,\\ 4x_1+8x_2&=8. \end{aligned} $$

For a particular candidate, take \(\mathbf{x}=(2,0)^{\top}\). The left sides are \(2(2)+4(0)=4\) and \(4(2)+8(0)=8\), so both equations hold. Its fitted vector is \(\mathbf{A}\mathbf{x}=(2,0,2)^{\top}\), and its residual is \(\mathbf{r}=(-1,1,1)^{\top}\). Then \(\mathbf{a}_1^{\top}\mathbf{r}=-1+0+1=0\) and \(\mathbf{a}_2^{\top}\mathbf{r}=-2+0+2=0\). This confirms orthogonality for the chosen candidate; the displayed equations also show why one must not assume that every normal-equation system has independent equations.

Common Mistake: Using the Wrong Sign

A frequent error is to rearrange the residual equation and place a minus sign on the right-hand side. With the convention \(\mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{x}\), the orthogonality condition is \(\mathbf{A}^{\top}(\mathbf{b}-\mathbf{A}\mathbf{x})=\mathbf{0}\). Expanding gives \(\mathbf{A}^{\top}\mathbf{b}-\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=\mathbf{0}\); moving the second term to the other side yields \(\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=\mathbf{A}^{\top}\mathbf{b}\), with a positive right-hand side.

For the first worked example, the mistaken equation \(\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=-\mathbf{A}^{\top}\mathbf{b}\) would require \(\begin{bmatrix}3&3\\3&5\end{bmatrix}\mathbf{x}=\begin{bmatrix}-5\\-6\end{bmatrix}\). The candidate \((\tfrac76,\tfrac12)^{\top}\) does not satisfy it: the left side is \((5,6)^{\top}\), not \((-5,-6)^{\top}\). The residual check catches the error too: the correctly derived candidate has zero dot product with each column, while changing the sign of the right-hand side does not preserve those normal equations.

Keep the definitions and dimensions visible when setting up a problem: \(\mathbf{x}\) is an \(n\)-entry coefficient vector, \(\mathbf{r}\) is an \(m\)-entry residual, and \(\mathbf{A}^{\top}\mathbf{r}\) records exactly the \(n\) dot products with the columns. This makes the normal equations a direct translation of residual orthogonality rather than a formula to memorize without its source.

Check Your Understanding

For each question, use the residual definition \(\mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{x}\) and justify the matrix dimensions in your work.

  1. Explain why the \(j\)th entry of \(\mathbf{A}^{\top}\mathbf{r}\) is the dot product of the \(j\)th column of \(\mathbf{A}\) with \(\mathbf{r}\).
  2. For \(\mathbf{A}=\begin{bmatrix}1&0\\1&1\\1&2\end{bmatrix}\) and \(\mathbf{b}=(1,2,2)^{\top}\), write the two normal equations before solving them.
  3. For \(\mathbf{A}=\begin{bmatrix}1\\0\\1\end{bmatrix}\) and \(\mathbf{b}=(2,1,4)^{\top}\), form the single normal equation and check the residual for its solution.
  4. Show algebraically that \(\mathbf{A}^{\top}(\mathbf{b}-\mathbf{A}\mathbf{x})=\mathbf{0}\) is equivalent to \(\mathbf{A}^{\top}\mathbf{A}\mathbf{x}=\mathbf{A}^{\top}\mathbf{b}\).
  5. Why does the derivation of the normal equations not require the columns of \(\mathbf{A}\) to be independent?

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon