polyfit — "C" function that fits a polynomial to a set of points
#include "polyfit.h"
int polyfit( int pointCount, double *xValues, double *yValues, int coefficientCount, double *coefficientResults );
The polyfit() function regresses a line or a higher-order polynomial to a set of points, using the Method of Least Squares. Its design goals were simplicity and ease of porting, as opposed to run-time efficiency with large data.
pointCount — input. The total number of points in the set. For the algorithm to work, this must be greater than or equal to coefficientCount.
xValues — input. Points to an array of the X coordinates of the points. There should be pointCount X coordinates.
yValues — input. Points to an array of the Y coordinates of the points. There should be pointCount Y coordinates.
coefficientCount — input. The number of coefficients to be computed, equal to the degree of the polynomial plus 1. For instance, if fitting a line — a first degree polynomial — then coefficientCount would be 2, and for fitting a parabola — a second degree polynomial — coefficientCount would be 3.
results — input. Points to where the computed coefficients will be stored. There should be space for coefficientCount coefficients. These coefficients are ordered from the highest-order monomial term to the lowest, such that for instance the polynomial:
5x² 3x - 7
is represented as:
[ 5, 3, -7 ]
In addition to setting the coefficient results, the polyfit() function returns 0 on success.
./src/polyfit.c — defines the polyfit() function.
./inc/polyfit.h — declares the polyfit() function's prototype.
To support unit testing on Linux with gcc, the repo has these additional files:
./src/test.c — exercises polyfit() and provides examples of usage.
./Makefile — allows the make command to build an executable, ./bin/polytest, that tests polyfit().