Tutorials › Linear Algebra › Least-Squares Formulation For Overdetermined Systems

Least Squares and QR Factorization · Tutorial 681 of 1000

Least-Squares Formulation For Overdetermined Systems

Learn to express an overdetermined system as a residual-minimization problem and interpret the difference between an exact solution and a least-squares fit.

Intermediate 9 min read

What You'll Learn

  • Identify the dimensions and meaning of an overdetermined system.
  • Define the residual vector for a proposed coefficient vector.
  • Write squared error as both a norm and a sum of coordinate errors.
  • Distinguish an exact solution from a least-squares solution.
  • Check exact fits and calculate best fits in small examples.

From Equations to a Fitting Problem

A system of linear equations asks whether one vector of unknowns satisfies every equation exactly. When there are more equations than unknowns, the system is called overdetermined. Such a system may still have an exact solution, but it need not. If the equations are inconsistent, one useful question is not whether all equations can be satisfied, but which choice of unknowns makes the discrepancies collectively as small as possible.

Let \(m,n\in\mathbb{N}\) with \(m>n\), let \(\mathbf{A}\in\mathbb{R}^{m\times n}\), and let \(\mathbf{b}\in\mathbb{R}^m\). The system \(\mathbf{A}\mathbf{x}=\mathbf{b}\) has \(m\) equations and \(n\) unknowns, where \(\mathbf{x}\in\mathbb{R}^n\) and \(\mathbf{b}\) is the target vector. The matrix-vector product \(\mathbf{A}\mathbf{x}\) is the vector of predicted left-hand sides. The difference between the target and that product records the error in each equation.

Definition: For a proposed vector \(\mathbf{x}\in\mathbb{R}^n\), the residual vector is \(\mathbf{r}(\mathbf{x})=\mathbf{b}-\mathbf{A}\mathbf{x}\). The squared-error objective is \(f(\mathbf{x})=\|\mathbf{r}(\mathbf{x})\|^2=\|\mathbf{b}-\mathbf{A}\mathbf{x}\|^2\), where \(\|\cdot\|\) is the Euclidean norm. A least-squares solution is a vector \(\widehat{\mathbf{x}}\in\mathbb{R}^n\) at which \(f\) attains its minimum over all \(\mathbf{x}\in\mathbb{R}^n\).

For a vector \(\mathbf{y}=(y_1,\dots,y_m)^\top\in\mathbb{R}^m\), the Euclidean norm satisfies \(\|\mathbf{y}\|^2=y_1^2+\cdots+y_m^2\). Thus, if \(a_{ij}\) is the entry in row \(i\), column \(j\) of \(\mathbf{A}\), the objective can be written one coordinate at a time:

$$ f(\mathbf{x})=\sum_{i=1}^{m}\left(b_i-\sum_{j=1}^{n}a_{ij}x_j\right)^2. $$

Each summand is the square of one equation's residual. Squaring prevents positive and negative errors from cancelling one another, and adding the squares gives a single nonnegative measure of the overall discrepancy. This is the unweighted Euclidean least-squares formulation; the objective treats the coordinates through their ordinary squared Euclidean errors.

Exact Solutions and Least-Squares Solutions

An exact solution makes every residual coordinate zero. A least-squares solution, by contrast, minimizes the total squared residual; its individual residuals do not have to be zero. The two ideas are connected, but they are not interchangeable.

Proposition: For every \(\mathbf{x}\in\mathbb{R}^n\), the squared-error objective is the sum of the squares of the coordinates of \(\mathbf{b}-\mathbf{A}\mathbf{x}\). In particular, \(f(\mathbf{x})=0\) if and only if \(\mathbf{A}\mathbf{x}=\mathbf{b}\).

Proof. Write \(\mathbf{r}(\mathbf{x})=\mathbf{b}-\mathbf{A}\mathbf{x}\), and let its \(i\)-th coordinate be \(r_i(\mathbf{x})=b_i-\sum_{j=1}^{n}a_{ij}x_j\), by the definition of matrix-vector multiplication. The Euclidean norm therefore gives \(f(\mathbf{x})=\|\mathbf{r}(\mathbf{x})\|^2=\sum_{i=1}^{m}r_i(\mathbf{x})^2\), which is the stated coordinate formula. Every square in this sum is nonnegative. The sum equals zero exactly when every square is zero, which is equivalent to \(r_i(\mathbf{x})=0\) for each \(i\). That coordinate condition says precisely \(\mathbf{b}-\mathbf{A}\mathbf{x}=\mathbf{0}\), or \(\mathbf{A}\mathbf{x}=\mathbf{b}\). \(\square\)

Theorem: If \(\mathbf{A}\mathbf{x}=\mathbf{b}\) has an exact solution, then its least-squares minimum is zero, and a vector is a least-squares solution if and only if it is an exact solution.

Proof. Assume that an exact solution \(\mathbf{x}_0\) exists. The proposition gives \(f(\mathbf{x}_0)=0\). Since \(f(\mathbf{x})\) is a sum of squares for every \(\mathbf{x}\), it is nonnegative, so no vector can have objective value below zero. Thus the minimum is zero. If \(\mathbf{x}\) is an exact solution, then \(f(\mathbf{x})=0\), so it attains that minimum and is a least-squares solution. Conversely, if \(\mathbf{x}\) is a least-squares solution, its objective value is the minimum, which is zero. The proposition then implies \(\mathbf{A}\mathbf{x}=\mathbf{b}\). This proves both directions. \(\square\)

If no exact solution exists, the proposition says that no vector has objective value zero. A least-squares solution, when identified, is instead a vector attaining the smallest possible squared error, which is then positive. The formulation itself does not say that a particular candidate is a minimizer; that must be established by calculation or by a result suited to finding minimizers.

Worked Fits in Small Systems

Worked Example: An Overdetermined System with an Exact Fit

Consider three equations in two unknowns, written as \(\mathbf{A}\mathbf{x}=\mathbf{b}\), with \(\mathbf{x}=(x_1,x_2)^\top\):

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

There are three equations and two unknowns, so the system is overdetermined. For the proposed vector \(\mathbf{x}=(2,-1)^\top\), compute the product row by row:

$$ \mathbf{A}\mathbf{x} = \begin{bmatrix} 1(2)+0(-1)\\ 0(2)+1(-1)\\ 1(2)+1(-1) \end{bmatrix} = \begin{bmatrix} 2\\ -1\\ 1 \end{bmatrix} =\mathbf{b}. $$

The residual is therefore \(\mathbf{r}(\mathbf{x})=\mathbf{b}-\mathbf{A}\mathbf{x}=(0,0,0)^\top\), and the objective is \(f(\mathbf{x})=0^2+0^2+0^2=0\). Since the objective cannot be negative, this is a least-squares solution, and it is also an exact solution. Substitution into all three equations confirms the fit: \(x_1=2\), \(x_2=-1\), and \(x_1+x_2=1\).

Worked Example: One Unknown and Three Inconsistent Equations

Now consider \(\mathbf{A}\mathbf{x}=\mathbf{b}\), where \(x\in\mathbb{R}\):

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

The equations ask for \(x=1\), \(x=2\), and \(x=3\) simultaneously, so there is no exact solution. For a proposed value \(x=t\), the predicted vector and residual are

$$ \mathbf{A}t= \begin{bmatrix} t\\t\\t \end{bmatrix}, \qquad \mathbf{r}(t)= \begin{bmatrix} 1-t\\ 2-t\\ 3-t \end{bmatrix}. $$

Consequently, the squared-error objective is \(f(t)=(1-t)^2+(2-t)^2+(3-t)^2\). Complete squares by grouping around \(t=2\):

$$ \begin{aligned} f(t) &=(t-1)^2+(t-2)^2+(t-3)^2\\ &=((t-2)+1)^2+(t-2)^2+((t-2)-1)^2\\ &=3(t-2)^2+2. \end{aligned} $$

Because \((t-2)^2\geq0\), the objective is at least \(2\), with equality at \(t=2\). Thus \(\widehat{x}=2\) is a least-squares solution. Its predicted vector is \(\mathbf{A}\widehat{x}=(2,2,2)^\top\), so its residual is \((-1,0,1)^\top\), and direct substitution gives \(f(2)=(-1)^2+0^2+1^2=2\). This confirms the minimum found by completing squares. The residual is not zero, so this best fit is not an exact solution.

Worked Example: Two Unknowns with Four Equations

Let \(\mathbf{x}=(s,t)^\top\) and take

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

The predicted vector is \(\mathbf{A}\mathbf{x}=(s,t,s,t)^\top\), so the residual and squared error are

$$ \mathbf{r}(s,t)= \begin{bmatrix} 1-s\\ 2-t\\ 3-s\\ 4-t \end{bmatrix}, \qquad f(s,t)=(1-s)^2+(2-t)^2+(3-s)^2+(4-t)^2. $$

Group the terms involving \(s\), and then those involving \(t\). For \(s\), write \(s-1=(s-2)+1\) and \(s-3=(s-2)-1\). For \(t\), write \(t-2=(t-3)+1\) and \(t-4=(t-3)-1\). This gives

$$ \begin{aligned} f(s,t) &=(s-1)^2+(s-3)^2+(t-2)^2+(t-4)^2\\ &=2(s-2)^2+2+2(t-3)^2+2\\ &=2(s-2)^2+2(t-3)^2+4. \end{aligned} $$

Both squared terms are nonnegative, so \(f(s,t)\geq4\), with equality when \(s=2\) and \(t=3\). Hence \(\widehat{\mathbf{x}}=(2,3)^\top\) minimizes the objective. Check the matrix product and residual:

$$ \mathbf{A}\widehat{\mathbf{x}} = \begin{bmatrix} 2\\3\\2\\3 \end{bmatrix}, \qquad \mathbf{r}(\widehat{\mathbf{x}}) = \begin{bmatrix} -1\\-1\\1\\1 \end{bmatrix}, \qquad f(\widehat{\mathbf{x}})=1+1+1+1=4. $$

The nonzero residual confirms that this is an approximate fit, not an exact solution. In this example the completed-square expression also shows directly that no other pair gives the same minimum: equality requires both \(s=2\) and \(t=3\).

How to Read the Formulation Correctly

The matrix \(\mathbf{A}\) and target \(\mathbf{b}\) specify the fitting problem; the variable \(\mathbf{x}\) is what can be chosen. For every candidate \(\mathbf{x}\), first compute \(\mathbf{A}\mathbf{x}\), then subtract it from \(\mathbf{b}\) to obtain the residual, and then sum the squares of the residual's coordinates. This translates a system into an objective function on \(\mathbb{R}^n\). It does not by itself supply a minimizing vector.

1
Check the dimensions.
For an overdetermined system, \(\mathbf{A}\) has \(m\) rows and \(n\) columns with \(m>n\); \(\mathbf{b}\) has \(m\) entries and \(\mathbf{x}\) has \(n\).
2
Form the residual.
For a proposed \(\mathbf{x}\), calculate \(\mathbf{r}(\mathbf{x})=\mathbf{b}-\mathbf{A}\mathbf{x}\), keeping each equation's discrepancy in its own coordinate.
3
Write the objective.
Use \(f(\mathbf{x})=\|\mathbf{r}(\mathbf{x})\|^2\), or expand it as the sum of the squared coordinate errors.
4
Distinguish the claims.
A zero residual means an exact solution. A least-squares claim means the objective is no larger than at any other candidate.

A common mistake is to solve one equation and treat its answer as an exact fit to an inconsistent system. In the one-unknown example, using the first equation gives \(x=1\). The incorrect conclusion is that \(x=1\) solves the system. Substitution into all three equations gives the predicted vector \((1,1,1)^\top\), not \((1,2,3)^\top\); the residual is \((0,1,2)^\top\), and the squared error is \(0^2+1^2+2^2=5\). The correction is to check every equation for an exact solution, or, when exact satisfaction fails, compare candidates using the full squared-error objective. A small error in one equation is not enough to establish that the total error is minimized.

QuestionMathematical testMeaning
Is \(\mathbf{x}\) an exact solution?\(\mathbf{b}-\mathbf{A}\mathbf{x}=\mathbf{0}\)Every equation is satisfied.
What is its squared error?\(\sum_{i=1}^{m}r_i(\mathbf{x})^2\)The sum of all coordinate errors squared.
Is \(\mathbf{x}\) a least-squares solution?\(f(\mathbf{x})\leq f(\mathbf{z})\) for every \(\mathbf{z}\in\mathbb{R}^n\)No candidate has smaller squared error.
Can an exact solution be least squares?Yes; its objective is \(0\).Every exact solution is a least-squares solution.

Check Your Understanding

For each question, keep the residual and objective distinct from the claim that a candidate minimizes the objective.

  1. For \(\mathbf{A}=\begin{bmatrix}1\\2\\1\end{bmatrix}\), \(\mathbf{b}=\begin{bmatrix}2\\4\\2\end{bmatrix}\), and \(x=2\), compute the predicted vector, residual, and squared error. Is the fit exact?
  2. Write the squared-error objective for \(\mathbf{A}=\begin{bmatrix}1&0\\1&1\\0&1\end{bmatrix}\), \(\mathbf{b}=\begin{bmatrix}1\\3\\4\end{bmatrix}\), and \(\mathbf{x}=(s,t)^\top\).
  3. Explain why a zero value of the squared-error objective guarantees that every equation is satisfied.
  4. For the system in the worked example with \(\mathbf{A}=(1,1,1)^\top\) and \(\mathbf{b}=(1,2,3)^\top\), compute the residual and squared error at \(x=1\).
  5. State the difference between proving that a vector is an exact solution and proving that it is a least-squares solution.

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