// nonlinear_fit_test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include int _tmain(int argc, _TCHAR* argv[]) { int m; int n; int k; ap::real_1d_array y; ap::real_2d_array x; ap::real_1d_array c; lsfitreport rep; lsfitstate state; int info; double epsf; double epsx; int maxits; int i; int j; n = 1000; m = 1; k = 3; y.setlength(n); x.setlength(n, m); c.setlength(k); // formula: asy - a * exp(-b * x); for(i = 0; i <= n-1; i++) { x(i,0) = 10.0 * ap::randomreal() - 5.0; // from -5 to 5 y(i) = 12.0 - 4.0 * exp(-0.5 * x(i,0)); // asy = 12, a = 4.0, b = 0.5 } c(0) = 5.0; c(1) = 2.0; c(2) = 5.5; epsf = 0.000002; epsx = 0.0000000001; maxits = 0; lsfitnonlinearfg(x, y, c, n, m, k, epsf, epsx, maxits, true, state); while(lsfitnonlineariteration(state)) { double asy = state.c(0); double a = state.c(1); double b = state.c(2); double x = state.x(0); if( state.needf ) { state.f = asy - a * exp(-b * x); } if( state.needfg ) { state.f = asy - a * exp(-b * x); state.g(0) = 1.0; state.g(1) = -exp(-b * x); state.g(2) = -a * x * state.g(1); } } lsfitnonlinearresults(state, info, c, rep); printf("asy %0.10lf, a %0.10lf, b %0.10lf\n", double(c(0)), double(c(1)), double(c(2)) ); printf("rms.err: %0.3lf\n", double(rep.rmserror)); printf("max.err: %0.3lf\n", double(rep.maxerror)); printf("Termination type: %0ld\n", long(info)); printf("\n\n"); };