/* poly_fit.c given n data points (xi,yi) */ /* exactly fit a n-1 order polynomial */ /* y = c0 + c1*x + c2*x^2 + ... + cn-1 * x^(n-1) */ /* */ /* create system of equations: */ /* */ /* | 1 x1 x1^2 ... x1^n | | c0 | | y1 | */ /* | 1 x2 x2^2 ... x2^n | | c2 | | y2 | */ /* | ... | * | ... | = |... | */ /* | ... | | ... | |... | */ /* | 1 xn xn^2 ... xn^n | | cn-1 | | yn | */ /* */ /* solve for c0, c1, ... , cn-1 */ #include #include "simeq.h" double peval(int n, double x, double c[]); int main(int argc, char *argv[]) { int n = 4; /* number of equations */ double X[4] = { 0.0, 0.25, 0.5, 1.0 }; /* (x,y) points to fit */ double Y[4] = { 1.0, 0.0, 0.0, 1.0 }; double A[16]; /* matrix */ double C[4]; /* coefficients */ double xx, yy, err; int i, j, k; printf("poly_fit.c running \n"); for(i=0; i0; i--) y = (c[i]+y)*x; return y+c[0]; } /* end peval */ /* end poly_fit.c */