Fit a line y = mx + c through the points (1,1), (2,3), (3,2) using the normal equations AᵀAx̂ = Aᵀb. Compute m and c.
Explanation
The least-squares problem
Given an overdetermined system Ax=b (more equations than unknowns), usually no exact solution exists. Least squares finds the x^ that minimizes
∥Ax−b∥2=∑i(Ax−b)i2
(the sum of squared residuals).
The normal equations
The minimizer solves
ATAx^=ATb
If A has linearly independent columns (full column rank), ATA is invertible and
x^=(ATA)−1ATb
Geometrically: x^ solves Ax^=projCol(A)(b), so the least-squares fit projectsb onto the column space.
Step-by-step — fit a line to 3 points
Points: (xi,yi) = (1,1),(2,3),(3,2). Model: y=mx+c — two unknowns m,c; three equations. Overdetermined.
Step 1 — Set up Ax=b.
A=123111,x=(mc),b=132
(Column 1 is xi, column 2 is 1 for the intercept.)
Solving Ax=b directly. The system is usually inconsistent; solve the normal equations instead.
Forgetting the intercept column. If your model has a constant, include a column of 1s.
Misidentifying rows and columns. Each observation is a row of A; each parameter is a column.
Try it in the visualization
Drag data points; the best-fit line snaps to its least-squares position. Dashed vertical segments show the residuals; their squared lengths are summed and displayed. The sum strictly grows if you drag the line away from the optimal fit.
Interactive Visualization
Parameters
1.00
1.00
2.00
3.00
3.00
2.00
4.00
4.00
Your turn
Got your own math or physics problem?
Turn any problem into an interactive visualization like this one — powered by AI, generated in seconds. Free to try, no credit card required.