Least Squares and Data Fitting
Fit an inconsistent system by minimizing residual length and checking its orthogonality.
Builds on Gram–Schmidt and QR Factorization
The bigger question: What is the closest answer when an exact fit is impossible?
On this page
Replace exact matching with closest matching
When is inconsistent, least squares seeks minimizing . The fitted vector is the orthogonal projection of onto , so the residual is perpendicular to every column of . Therefore
These normal equations always have a solution. The coefficient vector is unique when has independent columns. Otherwise the fitted vector is still unique, but several coefficient vectors may produce it.
Visual guide
- Least-squares line
- Residual
Worked example: fit a line
Fit to . Then
Solving gives , . The residual is . Its dot product with the constant column is zero, and with the time column is also zero. This verifies the least-squares condition.
Try it yourself.
Choose an answer and explain your reasoning to yourself. Use a hint if you get stuck.
Hint 1 · Find a starting point
A residual that still points along a column could be reduced.
Hint 2 · Take the next step
The normal equations give Aᵀr=0.
Show the reasoning
Answer: It is perpendicular to every column.
The optimal residual is orthogonal to the column space, even when an exact solution is impossible.
Before moving on: what would make one of the other answers wrong? Saying why is part of understanding.
Worked example: repeated measurements
For a constant model fitted to measurements , the matrix is a column of ones. The normal equation is , so , the arithmetic mean. Residuals sum to zero.
Ordinary least squares treats all residuals with equal weight and squares their magnitudes, making large errors influential. Measurement uncertainty may motivate weighted least squares, while outliers may call for a different model. A mathematically correct fit does not establish that the physical relationship is truly linear.
For numerical work, solve through QR or SVD. Forming squares the spectral condition number for a full-rank matrix and can lose accuracy when columns are nearly dependent.
Practice
- Fit a constant to .
- If already lies in , what is the minimum residual?
- Why can a fitted vector be unique when the coefficients are not?
Show worked solutions
- The mean is .
- Zero: an exact solution is available.
- Adding any vector in to the coefficients leaves unchanged.
Pause before the next idea.
Can you explain how the visualization connects to this lesson’s goal? If a step still feels uncertain, put this lesson on your review list and try the check again another day.