Least Squares Made Clear: Normal Equations and Line Fits
Understand least squares as a projection, solve the normal equations AᵀAx = Aᵀb, fit lines and curves to data, and read SSE, RMSE and R² correctly.

Least squares is the meeting point of linear algebra and data. In a linear algebra course it appears as the best approximate solution of an inconsistent system. In a statistics course it is linear regression. In a lab it is the line of best fit through your measurements. It is the same mathematics each time, and this guide explains it with the free Least Squares Solver, which solves the normal equations with exact fractions and shows the working.
The problem: too many equations
Suppose you want a line y = c0 + c1x through the points (0, 6), (1, 0), (2, 0) and (3, 0). Each point gives one equation, so you have four equations for two unknowns, and no line passes through all four points. Written as a matrix equation Ax = b, A has a column of ones and a column of x values, x holds c0 and c1, and b holds the y values. The system is inconsistent.
The idea: make the error as small as possible
Least squares chooses x to minimize the length of the error vector r = b − Ax, which is the same as minimizing the sum of squared vertical distances from the points to the line. Geometrically, Ax can only reach vectors in the column space of A, so the best you can do is the point of that space closest to b: the projection of b.
The projection is characterized by one fact: the error is perpendicular to every column of A. Written as an equation, Aᵀ(b − Ax) = 0, or AᵀAx = Aᵀb. These are the normal equations. AᵀA is a small square matrix, here 2×2, so they are easy to solve.
Solving the example
For the four points, AᵀA has rows 4 6 and 6 14, and Aᵀb is (6, 0). Row reduction gives c0 = 21/5 and c1 = −9/5, so the line is y = 4.2 − 1.8x. The calculator shows AᵀA and Aᵀb, the row operations and the fitted equation in both fraction and decimal form.
Substituting the x values gives fitted values 21/5, 12/5, 3/5 and −6/5, and the residuals are 9/5, −12/5, −3/5 and 6/5. Notice they add up to zero; that always happens when the model has a constant term. The tool's final check computes Aᵀr and gets (0, 0), confirming the error really is perpendicular to the columns.
Reading the numbers
SSE, the sum of squared errors, is 54/5 = 10.8. RMSE divides SSE by the number of points and takes the square root, about 1.64, which you can read as a typical size of the error in y units. R² compares SSE with the total variation of y around its mean, which is 27 here; 1 − 10.8/27 gives 0.6, so the line explains 60 percent of the variation. For real data, such as the height and weight sample in the examples, R² is often much closer to 1.
Be careful with R² when you force a line through the origin. Without a constant term the usual formula can give odd values, which is why the tool hides it in that case.
Beyond straight lines
The same method fits any model that is linear in its unknown coefficients. A parabola y = c0 + c1x + c2x² just adds a column of x² values to A. A plane z = b0 + b1x + b2y uses columns of ones, x values and y values. The calculator builds these design matrices for you; choose Polynomial with a degree up to six, or Plane for three-column data.
When the answer is not unique
If the columns of A are dependent, for example two identical columns, then AᵀA cannot be inverted and many coefficient vectors give the same smallest error. The calculator then returns the minimum-norm solution, the shortest such vector, and labels it so you know. This is the same answer the pseudoinverse gives.
A word on numerical stability
On a computer with ordinary floating-point numbers, forming AᵀA can lose accuracy when the columns are nearly dependent, and QR or SVD methods are preferred for that reason. Because this tool uses exact fractions, the normal equations give the exact least-squares answer for the numbers you enter.
You can paste data, import a CSV file exported from a spreadsheet, read the residual table, view the plot with residual lines, and download or print the results. Everything runs in your browser and is saved locally.


