/* * ===================================================================================== * * Filename: polyfit.cpp * * Description: Least squares fit of second order polynomials * Test program using gsl and direct calculation * Version: 1.0 * Created: 2017-07-17 09:32:55 * Compiler: gcc * * Author: Bernhard Brunner, brb_blog@epr.ch * References: This code was merged, adapted and optimized from these two sources: * http://www.cplusplus.com/forum/general/181580/ * https://stackoverflow.com/questions/36522882/how-can-i-use-gsl-to-calculate-polynomial-regression-data-points * Build: compile and link using * g++ -c -o polifitgsl.o polifitgsl.cpp * gcc polifitgsl.o -lgsl -lm -lblas -o polifitgsl * * Profiling: * valgrind --tool=callgrind ./polifitgsl * kcachegrind * * polynomialfit takes 5.22s for 1000 calls * findQuadCoefficients takes 0.08s for 1000 calls * 65x faster * ===================================================================================== */ #include #include "polifitgsl.h" double x[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; double y[] = { 98.02, 98.01, 98.01, 98.02, 97.98, 97.97, 97.96, 97.94, 97.96, 97.96, 97.97, 97.97, 97.94, 97.94, 97.94, 97.92, 97.96, 97.9, 97.85, 97.9 }; #define NP (sizeof(x)/sizeof(double)) // 20 #define DEGREE 3 double coeff[DEGREE]; bool findQuadCoefficients(double timeArray[], double valueArray[], double *coef, double &critPoint, int PointsNum){ const double S00=PointsNum;//points number double S40=0, S10=0, S20=0, S30=0, S01=0, S11=0, S21 = 0; // const double MINvalue = valueArray[0]; // const double MINtime = timeArray[0]; for (int i=0; i