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.

01

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.
0 or 1 weak itemsContinue with this module.
2 weak itemsRefresh statistics in Mathematics, Module 15.
3 or more weak itemsReview error measures in Module 1.
02

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)/St

When 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 skill works when: you compute the sums, solve for slope and intercept, and report r2.
The skill breaks down when: regression is used to interpolate exactly, or a nonlinear law is fitted without linearising.
The concept. Each point's vertical distance to the line is a residual. Least squares picks the slope and intercept that make the sum of those squared distances as small as possible.
03

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.

QuantityMeaningFormula
Sttotal spread about the meanΣ(yi − ȳ)2
Srspread about the lineΣ(yi − a0 − a1xi)2
r2fraction explained(St − Sr)/St
sy/xstandard 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.

04

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.

Figure 1. The fitted line passes through the centroid of the data with a slope near 0.84. It does not touch every point; it minimises the total squared miss.
  1. ProblemFind the least-squares slope and intercept for the data in Figure 1.
  2. Given / findSeven (x, y) pairs. Find a1 and a0.
  3. AssumptionsErrors are in y only; a straight line is an appropriate model.
  4. Modela1 = (nΣxy − ΣxΣy)/(nΣx2 − (Σx)2); a0 = ȳ − a1x̄.
  5. Equationsn = 7, Σx = 28, Σy = 24, Σxy = 119.5, Σx2 = 140
  6. 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.
  7. CheckThe line passes through the centroid (x̄, ȳ) = (4, 3.4286): 0.0714 + 0.8393·4 = 3.4286 ✓, as every least-squares line must.
  8. ConclusionThe best-fit line is y = 0.0714 + 0.8393x. It captures the upward trend without chasing the scatter.
Result. y = 0.0714 + 0.8393x.
05

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.

Figure 2. The line removes about 87 percent of the original spread, leaving a small residual sum. The coefficient of determination quantifies that improvement in one number.
  1. ProblemFind r2, r, and sy/x for the fit in Figure 2.
  2. Given / findSt = 22.714, Sr = 2.991, n = 7. Find r2, r, sy/x.
  3. AssumptionsThe same straight-line model from Worked Example 1.
  4. Modelr2 = (St − Sr)/St; r = √(r2); sy/x = √(Sr/(n − 2)).
  5. Equationsr2 = (St − Sr)/Stsy/x = √(Sr/(n − 2))
  6. 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.
  7. 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.
  8. 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.
Result. r2 = 0.868, r = 0.932, sy/x = 0.773.
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Forcing the line through pointsA wiggly fit chasing noise"Am I smoothing or interpolating?"Regression smooths; it should not pass through every point.
High r2 means correct modelA 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 directlyPoor fit to exponential data"Should I linearise first?"Transform with logarithms, then fit a line.
Standard error confused with slope errorMisreported uncertainty"Is this the residual scatter?"sy/x is the spread of residuals, not the slope's error.
07

Practice ladder

Level 1 · Direct skill

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.

Level 2 · Mixed concept

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.

Level 3 · Independent problem

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.

Transfer task | Real engineering

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.

08

Working with AI, and proving it yourself

Use AI as an examiner, not a solver

"Check that my line passes through the data centroid."
"Give me three data sets; I will judge whether a line is the right model."
"Fit the line for me." Computing the sums and coefficients is the skill.
"Is r2 high enough?" Reading the residual pattern is the point.

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.

Must include: the fitted coefficients, r2, the standard error, and a residual check.
09

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.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayRe-derive the line and r2 from a blank page.
+3 daysFit three new data sets and report r2.
+7 daysContrast smoothing with exact interpolation, Module 7.
+30 daysReuse regression to calibrate a real sensor.
10

Textbook mapping

This module follows Chapra and Canale, Numerical Methods for Engineers, 7th edition. Use these references to read further.

Topic in this moduleWhere to read more
Least-squares regressionChapra & Canale, Chapter 17
Coefficient of determination and standard errorChapra & Canale, Chapter 17
Linearisation of nonlinear modelsChapra & Canale, Chapter 17

Chapter numbers refer to the 7th edition. The regression formulas are standard, so any recent edition will align closely.