Ma · Mathematics

How to Write Least Squares in Matrix Form

Updated 13 October 2026

At a glance

  1. 1. Set up the system

    Put the unknown coefficients in x, the observed values in b, and the model coefficients in the rows of A.

  2. 2. Define the objective

    Choose x to minimize the squared residual length, ||Ax - b||².

  3. 3. Solve the equations

    For full-column-rank A, solve AᵀAx = Aᵀb; use QR or SVD for numerical work.

  4. 4. Check the fit

    Compute the residual and verify it is orthogonal to every column of A.

Try it: Matrix calculator

Least squares is a way to find the best approximate solution when a matrix equation has no exact solution, as commonly happens when fitting a model to more observations than unknown coefficients. Its matrix form is a minimization problem: choose the coefficient vector that makes the residual as small as possible.

This article sets up the problem, derives the normal equations, and works through a straight-line fit. The closed-form formula is useful for understanding the method, while QR factorization and singular value decomposition are generally preferable for numerical computation.

The matrix formulation

Let AA be an m×nm\times n matrix, x∈Rnx\in\mathbb{R}^n the unknown coefficient vector, and b∈Rmb\in\mathbb{R}^m the observed data vector. The system is

Ax=b.Ax=b.

When m>nm>n, there are more equations than unknowns, and the equations may be inconsistent. The residual for a proposed xx is r=b−Axr=b-Ax. The least-squares solution, written x^\hat{x}, minimizes the squared Euclidean length of that residual:

x^=arg min⁡x∈Rn∥Ax−b∥22.\hat{x}=\operatorname*{arg\,min}_{x\in\mathbb{R}^n}\|Ax-b\|_2^2.

Squaring the norm does not change which xx minimizes it, but makes the objective convenient to differentiate. The entries of AA encode the model, the entries of xx are the coefficients to estimate, and the entries of bb are the measured values.

Deriving the normal equations

Expand the objective as a function of xx:

f(x)=∥Ax−b∥22=(Ax−b)T(Ax−b).f(x)=\|Ax-b\|_2^2=(Ax-b)^T(Ax-b).

Its gradient is ∇f(x)=2AT(Ax−b)\nabla f(x)=2A^T(Ax-b). At a minimum, the gradient is zero, so

AT(Ax^−b)=0,A^T(A\hat{x}-b)=0,

or equivalently,

ATAx^=ATb.A^TA\hat{x}=A^Tb.

These are the normal equations. If the columns of AA are linearly independent, then ATAA^TA is invertible, giving the familiar formula

x^=(ATA)−1ATb.\hat{x}=(A^TA)^{-1}A^Tb.

This formula is a theoretical expression, not usually the best way to calculate the answer: explicitly forming an inverse can magnify numerical error. For numerical work, solve the least-squares problem with QR factorization or SVD instead.

Geometrically, Ax^A\hat{x} is the point in the column space of AA closest to bb. The residual is perpendicular to that column space, which is why ATr=0A^Tr=0. This orthogonality condition is another way to understand the normal equations.

Worked example: fitting a straight line

Fit a line y=c+mxy=c+mx to the points (0,1)(0,1), (1,2)(1,2), and (2,2)(2,2) by minimizing the sum of squared vertical errors. The unknown vector is x=[c  m]Tx=[c\;m]^T, and the data give

A=[101112],b=[122].A=\begin{bmatrix}1&0\\1&1\\1&2\end{bmatrix},\qquad b=\begin{bmatrix}1\\2\\2\end{bmatrix}.

Each row of AA contains the coefficients of cc and mm for one observation. Calculate the two sides of the normal equations:

ATA=[3335],ATb=[56].A^TA=\begin{bmatrix}3&3\\3&5\end{bmatrix},\qquad A^Tb=\begin{bmatrix}5\\6\end{bmatrix}.

Thus 3c+3m=53c+3m=5 and 3c+5m=63c+5m=6. Subtracting the first equation from the second gives 2m=12m=1, so m=1/2m=1/2 and c=7/6c=7/6. The fitted line is

y^=76+12x.\hat{y}=\frac{7}{6}+\frac{1}{2}x.

Its predictions at the three observed xx-values are 7/67/6, 5/35/3, and 13/613/6. Therefore the residual vector, using r=b−Ax^r=b-A\hat{x}, is

r=[−1/61/3−1/6],∥r∥22=16.r=\begin{bmatrix}-1/6\\1/3\\-1/6\end{bmatrix},\qquad \|r\|_2^2=\frac{1}{6}.

As a check, the residuals sum to zero and their products with the corresponding xx-values also sum to zero. In matrix terms, ATr=0A^Tr=0, confirming that the residual is orthogonal to both columns of AA.

When the formula needs care

The expression (ATA)−1ATb(A^TA)^{-1}A^Tb requires AA to have full column rank: its columns must be linearly independent. If they are dependent, there can be multiple coefficient vectors with the same best fit, and ATAA^TA is singular. The Moore–Penrose pseudoinverse gives the minimum-norm least-squares solution, while SVD makes rank deficiencies easier to identify.

For a weighted least-squares fit, where observations have different weights, minimize (Ax−b)TW(Ax−b)(Ax-b)^TW(Ax-b) for a suitable weight matrix WW. If WW is symmetric positive definite, the corresponding normal equations are ATWAx=ATWbA^TWAx=A^TWb. Ordinary least squares is the special case W=IW=I.

Common mistakes

  • Trying to invert a rectangular matrix: A non-square AA has no ordinary matrix inverse. Least squares instead solves a minimization problem.
  • Using the closed-form inverse without checking rank: The formula involving (ATA)−1(A^TA)^{-1} applies only when AA has full column rank.
  • Assuming the fit has zero residual: A least-squares solution need not satisfy Ax^=bA\hat{x}=b. It minimizes the residual; it does not necessarily eliminate it.
  • Confusing residual conventions: This article uses r=b−Ax^r=b-A\hat{x}. The alternative Ax^−bA\hat{x}-b changes the sign, but not the residual norm or the least-squares solution.
  • Using normal equations blindly in computation: When AA is poorly conditioned, solving with QR or SVD is generally more stable than forming ATAA^TA.

References

  1. Introduction to Linear Algebra, Gilbert Strang
  2. Matrix Computations, Gene H. Golub and Charles F. Van Loan

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.