Ken Hough's Website
Software


Polyfit -- A Polynomial Curve Fitting Program


Introduction:
Have you ever been in the situation where you wish to develop a computer program to model real situations? It's then neccessary to produce algorithms or equations which reflect real behaviours. In many cases simple equations can suffice. For example the equation y = mx +c can be used to define 'straight line' relationships between the parameters x and y.

In the real world many relationships are not that simple. Data arrays are often used to define sets of data, but these can become unwealdy, especially when accurately interpolated values are required.

Another solution is to use a polynomial equation of the form:

y = coef0 + coef1 * x + coef2 * x ^2 + coef3 * x ^3..........etc

'polyfit' provides an easy means of calculating polynomial equations of various orders for a given data set. The current version is restricted to a maximum of 50 data pairs and a maximum order of 8, which should cover most situations. 'polyfit' could handle fits of higher orders, but who cares?

'polyfit' is command line driven and writes its results to a text based file. This means that 'polyfit' can be called from within other applications. There are two neccessary parts to the command line instruction and these must be separated by at least one space:

ie. Full file path and name of a data file    AND   Order of polynomial fit required

For example:      'polyfit   datafile.dat   3'    would result in a 3rd order fit being done on the data file 'datafile.dat. Results will then be displayed on screen.

If a full file path and name for a results file is included in the command line, results will then be written to this file and will not be shown on screen. This mode of operation could be used when calling 'polyfit' from within another application. The command line format for this mode of operation is:

'polyfit   datafile.dat   3   resultsfile.dat'

Adding a final 'd' to the command line, causes results again to be displayed on screen (and also written to file)

eg.    'polyfit   datafile.dat   3   resultsfile.dat   d'

This mode of operation is intended for use while developing/debugging applications which use polyfit

Note: Do not use the 'd' flag if the calling application uses 'ncurses'.

I developed 'polyfit' from an original BBC Basic programme (ref. D.G.K.Guy, Practical Computing, May 1985, page 135).

The source code file for 'polyfit' is polyfit.c

polydrive.c is a demonstration program to show how to use 'polyfit'.

These source code files can be compiled to run under the Linux operating system. You will need to have the gcc compiler installed.

Use the commands:

gcc -Wall -O3 -o polyfit polyfit.c -lm
gcc -Wall -O3 -o polydrive polydrive.c -lm

I haven't provided a version of 'polyfit' for DOS/Microsoft Windows, because I don't have a suitable 'C' compiler. The source code is basicaly ANSI 'C', so with modifictions to account for the different file system, it should compile for the Microsoft systems. The only point to remember is that ANSI screen escape code is used so make sure that you have 'ANSI.SYS' installed.

'polyfit' includes built-in help pages which will be displayed if 'polyfit' is called without any command line instructions. These include full definitions of formats for the data file and the results file. The source code files are well 'REMed' to fairly well describe the two programs.

Return To Software Index Page
21/02/13