How to find a least-squares solution using normal equations

A system can have more equations than we can satisfy at once. Least squares gives us a way to choose a solution by minimizing the total squared error. We’ll use normal equations to find that choice for three equations in two unknowns.

Write the system as a matrix equation

Use x-y=3, 2x+y=1, and -x-4y=2. The coefficient matrix and target vector are

A=[1-121-1-4]
b=[312]

Make the residual perpendicular to the columns

Call the least-squares coefficient vector x*. We want the output Ax* closest to b. The residual b-Ax* must be perpendicular to every column of A, so AT(b-Ax*)=0. Rearranging gives the normal equations.

ATAx*=ATb
ATA=[65518]
ATb=[3-10]

Solve the two normal equations

6x+5y=35x+18y=-10x=10483y=-7583

The residual is 183(70,-50,-30). Dotting it with either column of A gives zero. The columns are independent, so this least-squares solution is unique.