Example of nonlinear fit.
MGL code:
new dat 100 '0.4*rnd+0.1+sin(2*pi*x)' new in 100 '0.3+sin(2*pi*x)' list ini 1 1 3:fit res dat 'a+b*sin(c*x)' 'abc' ini title 'Fitting sample':yrange -2 2:box:axis:plot dat 'k. ' plot res 'r':plot in 'b' text -0.9 -1.3 'fitted:' 'r:L' putsfit 0 -1.8 'y = ' 'r':text 0 2.2 'initial: y = 0.3+sin(2\pi x)' 'b'
C++ code:
void smgl_fit(mglGraph *gr) // nonlinear fitting { mglData dat(100), in(100), res; gr->Fill(dat,"0.4*rnd+0.1+sin(2*pi*x)"); gr->Fill(in,"0.3+sin(2*pi*x)"); double ini[3] = {1,1,3}; mglData Ini(3,ini); res = gr->Fit(dat, "a+b*sin(c*x)", "abc", Ini); if(big!=3) gr->Title("Fitting sample"); gr->SetRange('y',-2,2); gr->Box(); gr->Plot(dat, "k. "); gr->Axis(); gr->Plot(res, "r"); gr->Plot(in, "b"); gr->Puts(mglPoint(-0.9, -1.3), "fitted:", "r:L"); gr->PutsFit(mglPoint(0, -1.8), "y = ", "r"); gr->Puts(mglPoint(0, 2.2), "initial: y = 0.3+sin(2\\pi x)", "b"); // gr->SetRanges(mglPoint(-1,-1,-1),mglPoint(1,1,1)); gr->SetOrigin(0,0,0); }