|
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' can 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 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 translated and developed 'polyfit' from an original BBC Basic programme (ref. D.G.K.Guy, Practical Computing, May 1985, page 135). I still don't understand the how it works!
This current version is written in 'C' and can be compiled to run under the Linux operating system. A source code tarball can be found here. This tarball includes a demonstration utility 'polydrive' to serve as an aid/check on calling 'polyfit' from within another application. Simply untar the file by 'tar xvfz polyfit-1.0.0.tar.gz', change into the resulting 'polyfit' directory, and then run './install'. 'polyfit.c' and 'polydrive.c' will then be compiled and installed into your local 'bin' directory. You will need to have the gcc compiler installed.
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 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.
The untared source files are polyfit.c and demo program polydrive.c
'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.
|