Least-squares Curve Fit unit

Modified by David Taylor, Warren Postma

Least-squares is a way of approximating a set of data points by an equation, allowing you to predict intermediate values or calculate some measure of the data. You may have approximated a trend-line by drawing a straight line through a number of data points plotted on a graph. Least-squares does just this "fitting" but in a mathematical way that minimises the errors that result. This unit extends least-squares from just a straight-line fit into higher order polynomials for more complex data (where there may be a physical reason for a square-law fit, for example) or for a better match to the data.

Some users are completely unaware of the mathematical names of any of these things, and have no idea what a "linear regression" is, and simply want something that does curve fitting just "like excel does", with slope, intercept, correl, and rsq functions.

David Taylor, for his HRPT Reader, had a need for a curve fitting routine that could handle a higher order fit than a simple straight-line or parabolic curve fit, and that could handle an arbitrary number of data points. He found a version of Allen Miller's Curve Fitting routine (from: the book "Pascal Programs For Scientists And Engineers", which had been typed and submitted to MTPUG in Oct. 1982 by Juergen Loewner, and corrected and adapted for Turbo Pascal by Jeff Weiss. He updated these routines to work with Delphi 4 and 5's open array parameters. which allows the routine to be generalised so that it is no longer hard-coded to make a specific order of best fit or work with a specific number of points.

Richard Kavanagh ( kindly supplied a C++ version which is included in the archive as

Warren Postma added the chart to the demo, and added a way to force the intercept, because he needed it in order to meet certain US EPA 1631E method requirements, and has tested and maintained this sample since 2008.


Original Downloaded from (no longer online)

Stackoverflow discussion: