HuntsvilleEngineers mark
HE Reference Shelf — huntsvilleengineers.com
The Reference Shelf · Linear Algebra & Numerical Methods

Curve fitting and regression

Drawing the best line or curve through scattered measurements so you get one equation you can actually plug numbers into.

Also known as: data fitting · trend fitting · polynomial regression

The formula

Least-squares fit of a straight line y = m·x + b to n data points minimizes the sum of squared residuals S = Σ (y_i − (m·x_i + b))². Setting the two partial derivatives to zero gives the normal equations, which solve to:

m = ( n·Σx·y − Σx·Σy ) / ( n·Σx² − (Σx)² )
b = ( Σy − m·Σx ) / n

Reading it: the slope is the covariance of x and y over the variance of x; the intercept just centers the line so it passes through the point (mean x, mean y).

For a general model that is linear in its coefficients — a polynomial y = c₀ + c₁·x + c₂·x² + …, or any sum of basis functions — the same idea in matrix form is:

c = (Aᵀ·A)⁻¹·Aᵀ·y      (the normal equations)

Reading it: A is the design matrix, one row per data point, one column per term in your model. This one line fits any model whose unknowns appear linearly, no matter how curved the result looks — provided the columns of A are independent, so that Aᵀ·A is actually invertible (distinct x values, no redundant basis functions). One caution before you type it in literally: forming Aᵀ·A squares the problem's condition number, and for a polynomial above roughly degree 4–5, or an x-range as wide as raw ADC codes, the inverse silently returns garbage in double precision. Center and scale x first, then hand the problem to a library least-squares solver (polyfit, MATLAB's backslash) — they solve the same equations through QR or SVD, which stays stable.

Goodness of fit is the coefficient of determination:

R² = 1 − ( Σ(y_i − ŷ_i)² ) / ( Σ(y_i − ȳ)² )

Reading it: the fraction of the data's variation your model explains. R² = 1 is a perfect fit; R² = 0 means your line does no better than the flat average. For a simple straight-line fit, R² equals the square of the Pearson correlation coefficient r.

Where you meet it

  • Sensor calibration on the bench. You feed a load cell known weights, read the bridge output in millivolts, and fit a line. The slope becomes your scale factor and the intercept your zero offset. Every strain-gauge channel, RTD, thermocouple, and pressure transducer in the lab gets a fit like this before it produces a trustworthy number.
  • Reducing test-stand data. A thrust stand or a vibration table hands you a cloud of points. You fit a trend to pull out the parameter that matters — a damping ratio from a decay envelope, a spring rate from a force-deflection sweep, a leak rate from a pressure-decay curve — and report the coefficient, not the raw scatter.
  • Building a correction table for a review board. When an ADC or an amplifier has a mild nonlinearity, you characterize it with a polynomial fit and ship the coefficients as the correction. The board wants to see the residuals and the R², not your promise that it's "close enough."
  • Trending life and drift. Fitting a line to gauge drift, bearing wear, or battery capacity over cycles turns a logbook into a prediction of when the part crosses the limit.

How it works

Least squares squares the residuals for two reasons: it makes the math give one clean closed-form answer, and it penalizes big misses far harder than small ones. That second property is also the method's biggest weakness. One bad data point — a fat-fingered entry, a dropped sample, a transducer that glitched — pulls the whole line toward itself, because a residual twice as large counts four times as much. Always plot the raw points with the fit line on top before you trust a single coefficient. The eye catches an outlier that R² hides.

R² is the most abused number in the whole subject. A high R² does not mean the model is right — it means the model tracks the spread of your data. Fit a straight line to something genuinely curved and you can still get R² = 0.98 while the residuals march in a smooth arc from negative to positive and back. The tell is always in the residual plot: a good fit leaves residuals scattered like noise around zero, with no pattern. Structure in the residuals means your model is the wrong shape, full stop.

Then there is the temptation to raise the polynomial degree until the curve threads every point. It always works, and it is almost always wrong. A degree-n polynomial passes exactly through n+1 points — fit a 4th-order curve to 5 calibration points and the residuals are literally zero, R² is exactly 1, and you have learned nothing except your own noise. That is overfitting. Worse, high-degree polynomials fit to evenly spaced points oscillate violently between the points — the Runge phenomenon. Interpolate the function 1/(1+25x²) with a degree-10 polynomial through 11 equally spaced nodes and the fitted curve overshoots by nearly 2 near the ends, predicting 1.92 where the true value is 0.04. The cure is to keep the model as low-order as physics allows, or switch to splines, which fit low-order pieces and stay tame.

Two more limits worth carrying. First, ordinary least squares assumes all the error is in y and none in x; when both axes carry measurement error, you want total least squares or orthogonal regression instead, or your slope comes out biased low. Second, the method finds correlation, not cause. Galton's own word "regression" came from an effect — extreme parents having less-extreme children — that has nothing to do with the mechanism people read into a trend line. A fit tells you what the numbers do together. It never tells you why.

History

The method was born from a lost planet. On the first night of the nineteenth century, January 1, 1801, the astronomer Giuseppe Piazzi spotted the dwarf planet Ceres, tracked it for a few weeks, then lost it behind the glare of the Sun. A 24-year-old Carl Friedrich Gauss took the sparse observations, fit an orbit to them by minimizing the squared errors, and told the astronomers where to point their telescopes. Franz Xaver von Zach found Ceres again, right where Gauss said it would be [1][2].

Gauss did not publish the technique until 1809, in Theoria Motus Corporum Coelestium, where he claimed he had been using least squares since 1795 [1][2]. That claim landed badly, because Adrien-Marie Legendre had already put the method in print in 1805, in an appendix to Nouvelles méthodes pour la détermination des orbites des comètes, and gave it the name it still carries [1][2]. Legendre was offended, wrote to Gauss demanding proof of the earlier date, and the two touched off one of the sharpest priority fights in mathematics — the statistical equivalent of Newton versus Leibniz [1].

The word "regression" arrived from a different direction and much later. In 1886 Francis Galton published "Regression towards mediocrity in hereditary stature," fitting the heights of over a thousand parent-child pairs and noticing that tall parents tended to have children closer to the average [3][4]. He was describing a biological effect, but the line-fitting machinery he built to describe it became the regression we use every day. The correlation coefficient that rides alongside it carries Karl Pearson's name from his work in the 1890s, though the underlying formula had been written down by Auguste Bravais back in 1844 — a tidy example of Stigler's law, that discoveries rarely bear the name of their discoverer [5].

Related tools

  • /tools/strain-gauge-bridge
  • /tools/wheatstone-bridge
  • /tools/stress-strain
  • /tools/adc-resolution
  • /tools/thermal-noise-floor
  • /tools/convert-temperature

Sources

  1. https://en.wikipedia.org/wiki/Least_squares
  2. https://www.historyofinformation.com/detail.php?entryid=2707
  3. https://en.wikipedia.org/wiki/Regression_toward_the_mean
  4. https://old.maa.org/press/periodicals/convergence/regression-to-the-mean-a-mini-primary-source-project-for-statistics-students
  5. https://en.wikipedia.org/wiki/Pearson_correlation_coefficient

Written by HE in our own words from the cited sources — engineering judgment included, your stamp still required. All entries →

★ The Reference Shelf

Reading is free. The shelf is for cardholders.

Your library card is an email address: pin it to your shelf, print the card, take the FE/PE quick-reference pack, read the Huntsville history. The shelf remembers what you reach for.

Already on the list? Enter with your subscribed email →