Numerical Methods · Module 6 of 10
Curve Fitting: Least-Squares Regression
Measured data scatter. Regression draws the single line that sits closest to all of it, in the least-squares sense, and reports how well that line explains the data through one number, r squared.
Readiness check
This module fits a line to data. Tick only what you can do closed-notes.
- Compute a mean and a sum.
- Recall the equation of a straight line y = a0 + a1x.
- Square a residual and add the squares.
- Take a logarithm to linearize a relation.
- Distinguish a fit that interpolates from one that smooths.
The core idea
Least-squares regression chooses the line that minimises the sum of squared vertical residuals. Its slope and intercept come from the normal equations, and the coefficient of determination measures how much of the scatter the line explains.
minimise Sr = Σ (yi − a0 − a1xi)2a1 = (nΣxy − ΣxΣy)/(nΣx2 − (Σx)2)r2 = (St − Sr)/StWhen data carry measurement noise, fitting a curve through every point is wrong; the goal is the trend, not the scatter. Least-squares regression finds the line y = a0 + a1x that minimises Sr, the sum of the squared vertical distances from the points to the line. Setting the derivatives of Sr with respect to the two coefficients to zero gives the normal equations, solved in closed form for the slope a1 and intercept a0. The quality of the fit is the coefficient of determination r2 = (St − Sr)/St, the fraction of the total spread St that the line accounts for: r2 = 1 is a perfect fit, r2 = 0 explains nothing. The standard error of the estimate measures the typical residual. Many nonlinear laws, exponential or power, are fitted by first linearising them with logarithms.
The skills, taught in order
Five skills build the least-squares line, its quality measures, and the linearisation of nonlinear laws.
6.1 The least-squares criterion
Regression minimises the sum of squared residuals Sr = Σ(yi − a0 − a1xi)2. Squaring penalises large misses and makes the problem smooth and uniquely solvable, which is why least squares, not least absolute error, is the standard.
6.2 The normal equations
Setting the partial derivatives of Sr to zero gives two linear normal equations whose solution is a1 = (nΣxy − ΣxΣy)/(nΣx2 − (Σx)2) and a0 = ȳ − a1x̄. Four sums, Σx, Σy, Σxy, Σx2, fix the line.
| Quantity | Meaning | Formula |
|---|---|---|
| St | total spread about the mean | Σ(yi − ȳ)2 |
| Sr | spread about the line | Σ(yi − a0 − a1xi)2 |
| r2 | fraction explained | (St − Sr)/St |
| sy/x | standard error | √(Sr/(n − 2)) |
The fit and its quality. St is the spread with no model; Sr is what remains after the line.
6.3 The coefficient of determination
r2 = (St − Sr)/St is the fraction of the original spread the line explains. Its square root r is the correlation coefficient. A high r2 means the line captures most of the variation; a low one warns the model is poor.
6.4 The standard error of the estimate
The standard error sy/x = √(Sr/(n − 2)) is the typical size of a residual, in the units of y. It quantifies the scatter about the line and feeds prediction intervals, the spread expected around a fitted value.
6.5 Linearising nonlinear models
An exponential y = αeβx becomes linear as ln y = ln α + βx; a power law y = αxβ becomes linear in log-log. Fitting the transformed data with the same normal equations recovers the nonlinear coefficients, extending regression far beyond straight lines.
Engineering connection: calibrating a sensor, fitting a material law, or reducing test data all rest on least-squares regression and its r2.
Worked example 1: a least-squares straight line
Fit a least-squares line to the data x = 1, 2, 3, 4, 5, 6, 7 and y = 0.5, 2.5, 2, 4, 3.5, 6, 5.5. Find the slope and intercept.
- ProblemFind the least-squares slope and intercept for the data in Figure 1.
- Given / findSeven (x, y) pairs. Find a1 and a0.
- AssumptionsErrors are in y only; a straight line is an appropriate model.
- Modela1 = (nΣxy − ΣxΣy)/(nΣx2 − (Σx)2); a0 = ȳ − a1x̄.
- Equationsn = 7, Σx = 28, Σy = 24, Σxy = 119.5, Σx2 = 140
- Solvea1 = (7·119.5 − 28·24)/(7·140 − 282) = (836.5 − 672)/(980 − 784) = 164.5/196 = 0.8393. a0 = 24/7 − 0.8393·(28/7) = 3.4286 − 3.3571 = 0.0714.
- CheckThe line passes through the centroid (x̄, ȳ) = (4, 3.4286): 0.0714 + 0.8393·4 = 3.4286 ✓, as every least-squares line must.
- ConclusionThe best-fit line is y = 0.0714 + 0.8393x. It captures the upward trend without chasing the scatter.
Worked example 2: the quality of the fit
For the line of Worked Example 1, the total spread is St = 22.714 and the spread about the line is Sr = 2.991. Find the coefficient of determination, the correlation coefficient, and the standard error.
- ProblemFind r2, r, and sy/x for the fit in Figure 2.
- Given / findSt = 22.714, Sr = 2.991, n = 7. Find r2, r, sy/x.
- AssumptionsThe same straight-line model from Worked Example 1.
- Modelr2 = (St − Sr)/St; r = √(r2); sy/x = √(Sr/(n − 2)).
- Equationsr2 = (St − Sr)/Stsy/x = √(Sr/(n − 2))
- Solver2 = (22.714 − 2.991)/22.714 = 19.723/22.714 = 0.868. r = √0.868 = 0.932. sy/x = √(2.991/5) = √0.598 = 0.773.
- Checkr2 = 0.868 means the line explains about 87 percent of the spread, a good but not perfect fit, consistent with visible scatter about the line.
- ConclusionThe fit explains 87 percent of the variation with a typical residual of 0.77. These numbers, not the line alone, report how trustworthy the model is.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| Forcing the line through points | A wiggly fit chasing noise | "Am I smoothing or interpolating?" | Regression smooths; it should not pass through every point. |
| High r2 means correct model | A good r2 on the wrong curve | "Do the residuals show a pattern?" | Check residuals; r2 alone does not prove the model. |
| Fitting a nonlinear law directly | Poor fit to exponential data | "Should I linearise first?" | Transform with logarithms, then fit a line. |
| Standard error confused with slope error | Misreported uncertainty | "Is this the residual scatter?" | sy/x is the spread of residuals, not the slope's error. |
Practice ladder
For points (1, 1), (2, 3), (3, 5), find Σx, Σy, Σxy, Σx2.
Show answer
Σx = 6, Σy = 9, Σxy = 1 + 6 + 15 = 22, Σx2 = 1 + 4 + 9 = 14.
Use those sums to find the least-squares slope (n = 3).
Show answer
a1 = (3·22 − 6·9)/(3·14 − 36) = (66 − 54)/(42 − 36) = 12/6 = 2. The data lie exactly on a line of slope 2.
A fit has St = 50 and Sr = 8. Find r2 and say how good the fit is.
Show answer
r2 = (50 − 8)/50 = 42/50 = 0.84. The line explains 84 percent of the spread, a fairly good fit.
You calibrate a sensor and get r2 = 0.999 but the residuals curve systematically. Explain what this tells you and what to do.
What good work looks like
A high r2 with patterned residuals signals the wrong model shape: the linear fit is close but the true relation is curved. Fit a nonlinear form (or linearise it) so the residuals become random, not r2 alone.
Working with AI, and proving it yourself
Use AI as an examiner, not a solver
Portfolio task
Fit a least-squares line to real measured data, report r2 and the standard error, and inspect the residuals for a pattern that would call for a nonlinear model.
Retrieval and spaced review
Closed notes. Answer out loud, then reveal.
1. What does least squares minimise?
The sum of squared vertical residuals, Sr.
2. Write the slope from the normal equations.
a1 = (nΣxy − ΣxΣy)/(nΣx2 − (Σx)2).
3. Define r2.
(St − Sr)/St, the fraction of spread the line explains.
4. What is the standard error of the estimate?
sy/x = √(Sr/(n − 2)), the typical residual size.
5. How is an exponential law fitted?
Linearise it with logarithms, then apply the normal equations.
Textbook mapping
This module follows Chapra and Canale, Numerical Methods for Engineers, 7th edition. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| Least-squares regression | Chapra & Canale, Chapter 17 |
| Coefficient of determination and standard error | Chapra & Canale, Chapter 17 |
| Linearisation of nonlinear models | Chapra & Canale, Chapter 17 |
Chapter numbers refer to the 7th edition. The regression formulas are standard, so any recent edition will align closely.