4

I have a set of X-Y values (i.e. a scatter plot) and I want a Pascal routine to generate the coefficients of a Nth order polynomial that fits those points, in the same way that Excel does.

rossmcm
  • 5,493
  • 10
  • 55
  • 118

5 Answers5

10

I used David J Taylor's Polyfit example (curvefit.zip), which implements a least squares curve fitting algorithm (also known as linear regression) David's site is here, but keep reading, because my version is better. (See below).

The origin of the algorithms David is using is a book on scientific math for Pascal programmers, Allen Miller's Curve Fitting routine from the book "Pascal Programs For Scientists And Engineers", typed and submitted to MTPUG in Oct. 1982 by Juergen Loewner, and corrected and adaptated for Turbo Pascal by Jeff Weiss.

You can grab curvefit.zip directly from bitbucket here. (You can clone the sourcecode with Mercurial/TortoiseHG, or download a ZIP from bitbucket)

hg clone https://bitbucket.org/wpostma/curvefit curvefit

It runs in any delphi version 5 and up, Unicode or not, even Delphi 10 Berlin. It has a little chart in the demo, added by me. I also added a way to force the result through the origin, a common technique where you want a best fit on all values, other than the constant term, which should be forced, either to zero, or to some experimentally derived average. A forced "blank subtraction" which is set equal to the average of a series of analytical "zero samples", is common in certain types of analytical chemistry when used with certain types of instrumentation, and in other scientific cases, where it can be more useful than a best-fit, because you may wish to minimize error around the origin more than minimize error across the area of the curve that is farthest from the origin.

I should also clarify that for purposes of linear regression, a "curve" may also be a line, which is the case I needed for analytical chemistry purposes, and that equation for any straight line (y=mx+b) is also called the "calibration curve". A first order curve fit is a line (y = mx +b), a second order curve fit (shown in the picture) is a parabola (y= nX^2 + mX + b). As you might guess, this algorithm scales from first order up to any level you might wish. I haven't tested it above 8 terms though.

Here's a screenshot:

polyfit example with chart

Bitbucket project link:

https://bitbucket.org/wpostma/curvefit/overview

RobertFrank
  • 7,332
  • 11
  • 53
  • 99
Warren P
  • 65,725
  • 40
  • 181
  • 316
  • Thanks @Warren. When I download using the link I get 67k of what looks like the HTML from a SkyDrive web page. Please try again, as I think yours might be a nice fit. – rossmcm Apr 20 '11 at 00:19
  • I got the file by renaming your zip file to html and loading it into a browser and poking around! – rossmcm Apr 20 '11 at 01:10
  • Ross; The easy way would be to click on the link and load it as a webpage and click the DOWNLOAD link provided by Skydrive. But hey, suit yourself. – Warren P Apr 20 '11 at 15:34
  • @Warren. That's weird. I don't understand why the browser opens a URL that ends in ".zip" as a web page. I did indeed right-click and do a "save as" originally. – rossmcm Apr 20 '11 at 20:51
  • Browsers don't care about files or file extensions in URLs, they care about HTTP content-types in their headers. Which you can't see. You can see the URL, so your brain zeroes in on that. :-) – Warren P Apr 21 '11 at 12:50
  • "This item might not exist or is no longer available" Any chance you could fix the link? – Roland Rabien Feb 13 '12 at 19:39
5

Try TPMath http://tpmath.sourceforge.net/ - I've been using this for years for fitting a hill regression and can recommend it.

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
  • 2
    There is also a specific version of TpMath called DMath for Delphi here: http://www.unilim.fr/pages_perso/jean.debord/tpmath/tpmath.htm DMath / TPMath comes with excellent documentation - highly recommendable. – iamjoosy Apr 19 '11 at 16:37
  • 1
    I used this, and it worked great for me. I also answered this question myself, because I use a small standalone single-pascal-unit solution whenever I need to do a least-squares curve fit. See my answer below. – Warren P Apr 19 '11 at 20:56
  • Cool @iamjoosy, @Frank. DMath seems very complete. – rossmcm Apr 20 '11 at 00:11
2

Check the functions in Turbo Power's SysTools library, now is open source, it includes math functions in the unit StStat.

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • There's no math tools other than a few special trigonometry functions in SysTools. – Warren P Mar 04 '13 at 14:11
  • Wrong. See StStat.pas with lots of statistical math functions. Don't know if the regression functions there have polynomial regression. I think that you have downvoted my response without knowing this. – Clóvis Valadares Junior Mar 17 '13 at 00:32
1

Even though you've already awarded an answer, for completeness, I thought I'd add this:

We use SDL Components' Math pack and have been very happy with it.

http://www.lohninger.com/delfcomp.html

It's well thought out, and does exactly what we need.

He's got a variety of other interesting tools on his site.

RobertFrank
  • 7,332
  • 11
  • 53
  • 99
  • I have seen this stuff, and it's very nice too. If you need to do a lot of stuff, including matrix math, this is the thing to do. – Warren P Apr 20 '11 at 15:35
1

XlXtrFun is the best curve fitting I know and use, but it is for Excel:

http://www.xlxtrfun.com/XlXtrFun/XlXtrFun.htm

Warren P
  • 65,725
  • 40
  • 181
  • 316
avra
  • 3,690
  • 19
  • 19
  • not what I was looking for (I wanted pascal source, not Excel) but useful nevertheless. Thanks. – rossmcm Apr 20 '11 at 20:53