1. Home
  2. Linear algebra

Linear algebra · least squares

Left Matrix Division and Linear Least Squares

Eigenfield editorial team ·

Left matrix division, written A\b, solves Ax = b exactly when A is square and invertible. When A has more rows than columns it does something different and very useful: it returns the x that makes ‖Ax − b‖ as small as possible. That is the linear least squares solution, and it is the standard way to fit a line or any model that is linear in its unknown coefficients.

Why an exact answer usually does not exist

Suppose you measure three points and want a straight line through them. The line has two unknowns, intercept and slope, but there are three equations. Unless the points happen to be collinear, no pair of coefficients satisfies all three. The residual r = b − Ax is therefore nonzero for every x, and the sensible goal is to choose x that gives the shortest residual in the 2-norm.

Geometrically, Ax ranges over the column space of A, a plane inside three-dimensional space. The best Ax is the orthogonal projection of b onto that plane, and the residual is perpendicular to the plane. Perpendicular to every column of A means Aᵀr = 0, which gives the normal equations AᵀAx = Aᵀb.

A worked fit

Fit y = c₀ + c₁t to the points (1, 1), (2, 2), (3, 2). The matrix and right-hand side are

A = [1 1; 1 2; 1 3], b = (1, 2, 2).

Form the normal equations. AᵀA = [3 6; 6 14] and Aᵀb = (5, 11). The determinant of AᵀA is 3·14 − 6·6 = 6, so the inverse is (1/6)[14 −6; −6 3]. Multiplying by (5, 11) gives (70 − 66, −30 + 33)/6 = (4/6, 3/6). The solution is c₀ = 2/3 and c₁ = 1/2.

Check it by looking at the residual. The fitted values are 2/3 + 1/2 = 7/6, 2/3 + 1 = 5/3 and 2/3 + 3/2 = 13/6. Subtracting from b leaves r = (−1/6, 1/3, −1/6). The entries of r sum to zero, and −1/6 + 2/3 − 1/2 = 0 as well, so Aᵀr = 0. The squared residual norm is 1/36 + 4/36 + 1/36 = 1/6.

A = [1 1; 1 2; 1 3];  b = [1; 2; 2];
c = A \ b          % (0.6667, 0.5)
r = b - A*c        % (-0.1667, 0.3333, -0.1667)

How the operator computes it

Solving the normal equations by hand is fine for two unknowns, but numerical code avoids it. Forming AᵀA squares the condition number of the problem, so a system that is merely awkward becomes hopeless. Instead, left division uses a QR factorization: write A = QR with Q having orthonormal columns and R upper triangular, then solve the triangular system Rx = Qᵀb. The residual never passes through AᵀA.

When A may be rank deficient, a more careful route uses the singular value decomposition or a pivoted QR. Columns that are nearly dependent show up as tiny singular values, and the solver can truncate them to give the minimum-norm solution among all minimizers. If you suspect your columns are redundant, check the rank first with an orthonormal basis for the range.

For underdetermined systems, with fewer equations than unknowns, infinitely many exact solutions exist, and different implementations pick different ones. Some return the minimum-norm solution, others a basic solution with zeros in chosen positions. Do not rely on one behavior across tools.

Practical notes

Scale the columns of A when the unknowns live on very different scales, for instance a column of ones next to a column of years such as 1990 to 2020. Centering the time variable makes the problem far better conditioned. The residual deserves a look too: a pattern in it, rather than scatter, says the model form is wrong, not that the arithmetic failed.

Square systems and the basic operator are described under matrix division. For the underlying triangular machinery see LU factorization, and the glossary defines rank and least squares terms. Related sections live in the linear algebra hub.

Back to top