void main() { //* Initialize data to be fit. Data is quadratic plus random number. Matrix c(3); cout << "Curve fit data is created using the quadratic" << endl; cout << " y(x) = c(1) + c(2)*x + c(3)*x^2" << endl; cout << "Enter the coefficients:" << endl; cout << "c(1) = "; cin >> c(1); cout << "c(2) = "; cin >> c(2); cout << "c(3) = "; cin >> c(3); double alpha; cout << "Enter estimated error bar: "; cin >> alpha; int i, N = 50; // Number of data points long seed = 1234; // Seed for random number generator Matrix x(N), y(N), sigma(N); for( i=1; i<=N; i++ ) { x(i) = i; // x = [1, 2, ..., N] y(i) = c(1) + c(2)*x(i) + c(3)*x(i)*x(i) + alpha*randn(seed); sigma(i) = alpha; // Constant error bar } //* Fit the data to a straight line or a more general polynomial cout << "Enter number of fit parameters (=2 for line): "; int M; cin >> M; Matrix a_fit(M), sig_a(M), yy(N); double chisqr; if( M == 2 ) //* Linear regression (Straight line) fit linreg( x, y, sigma, a_fit, sig_a, yy, chisqr); else //* Polynomial fit pollsf( x, y, sigma, M, a_fit, sig_a, yy, chisqr); //* Print out the fit parameters, including their error bars. cout << "Fit parameters:" << endl; for( i=1; i<=M; i++ ) cout << " a(" << i << ") = " << a_fit(i) << " +/- " << sig_a(i) << endl; cout << "Chi square = " << chisqr << "; N-M = " << N-M << endl; //* Print out the plotting variables: x, y, sigma, yy ofstream xOut("x.txt"), yOut("y.txt"), sigmaOut("sigma.txt"), yyOut("yy.txt"); for( i=1; i<=N; i++ ) { xOut << x(i) << endl; yOut << y(i) << endl; sigmaOut << sigma(i) << endl; yyOut << yy(i) << endl; } }
void cvLineFitter::fitLine(ofxPoint2f * pts, int nPts, float &slope, float &intercept, float &chiSqr){ Matrix x(nPts), y(nPts), sigma(nPts); for(int i=1; i<=nPts; i++ ) { x(i) = pts[i-1].x; y(i) = pts[i-1].y; sigma(i) = 1.0f; //error bar? what is this? } Matrix a_fit(2), sig_a(2), yy(nPts); double chiSqrd; linreg( x, y, sigma, a_fit, sig_a, yy, chiSqrd); chiSqr = (double)chiSqrd; intercept = a_fit(1); slope= a_fit(2); chiSqr = sig_a(1); //printf("fit: %f %f \n", a_fit(1), sig_a(1)); //printf("sig 1: %f %f\n", sig_a(1), sig_a(2)); }