Least Squares Calculator
Find the best fit when no exact answer exists, for a line through points or for any Ax ≈ b. See what it minimizes, as real squares you can drag around.
Points
Enter each point as x and y, up to 30 points, or start from an example.
Result
Updates as you type.
- Best-fit line
- Total squared error
- R²
What least squares means
When a system has more equations than unknowns, there's usually no exact answer. Least squares finds the that comes closest, by making the total of the squared errors as small as possible:
The best-known case is fitting a line. Each point asks for , and with more than two points these rarely all hold. The best-fit line is the one whose squared vertical distances add up to the least.
Squaring the errors keeps positive and negative errors from canceling out, and it gives a clean formula. It also means that one point far from the others can pull the whole line toward it.
The normal equations
Least squares has a geometric meaning. Everything can reach is its column space, and if isn't in it, the closest reachable point is the projection of . The leftover residual is then perpendicular to every column of , so . Rearranging that gives the normal equations:
This is a square system that can always be solved. If the columns of are independent, the answer is unique. If they aren't, as the Matrix Rank Calculator can show, many share the same smallest error, and this calculator shows the one that sets the free unknowns to 0.
Reading the result
For a line fit, measures how much of the spread in the line explains:
A value of 1 means that the line passes through every point. A value near 0 means that the line does little better than a flat line at the average height.
This calculator solves the normal equations in exact fractions, so rounding can't affect it. Programs that work with rounded numbers usually avoid them, because makes rounding errors worse, and use the QR factors from the Gram-Schmidt and QR Calculator instead. The projection of a single vector, the simplest case of all this, is on the Vector Projection Calculator.
Related calculators
Keep working with vectors and matrices.
Vector Projection Calculator
Project one vector onto another in exact fractions. Get the vector projection, scalar projection, and orthogonal remainder, and see the projection as a shadow.
Gram-Schmidt and QR Calculator
Turn vectors into an orthogonal and orthonormal basis with exact fractions and roots. Step through each projection, get the QR factors, and watch vectors snap to perpendicular.
Matrix Rank Calculator
Find rank and nullity with exact fractions, get bases for the column, row, null, and left null spaces, and see what a matrix collapses.