[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
9.7.5 Fitting sample
C++ code
mglData rnd(100), in(100), res; rnd.Fill("0.4*rnd+0.1+sin(2*pi*x)", gr->Min, gr->Max); in.Fill("0.3+sin(2*pi*x)", gr->Min, gr->Max); gr->Axis(mglPoint(-1,-2), mglPoint(1,2)); gr->Plot(rnd, ". "); gr->Box(); float ini[3] = {1,1,3}; gr->Fit(res, rnd, "a+b*sin(c*x)", "abc", ini); gr->Plot(res, "r"); gr->Plot(in, "b"); gr->Text(mglPoint(-1, -1.3), "fitted:", "L:r", -1); gr->PutsFit(mglPoint(0, -1.8), "y = ", "C:r", -1); gr->Text(mglPoint(0, 2.2), "initial: y = 0.3+sin(2\\pi x)", "C:b", -1);
MGL code
new rnd 100 fill rnd '0.4*rnd+0.1+sin(2*pi*x)' new in 100 fill in '0.3+sin(2*pi*x)' yrange -2 2 plot rnd '. ' box list ini 1 1 3 fit res rnd 'a+b*sin(c*x)' 'abc' ini plot res 'r' plot in 'b' text -1 -1.3 'fitted:' 'L:r' -1 putsfit 0 -1.8 'y = ' 'C:r' text 0 2.2 'initial: y = 0.3+sin(2\pi x)' 'C:b' -1
Pure C code
HMDT rnd,in,res; float ini[3] = {1,1,3}; rnd = mgl_create_data_size(100,1,1); in = mgl_create_data_size(100,1,1); res = mgl_create_data(); mgl_data_modify(rnd,"0.4*rnd+0.1+sin(4*pi*x)",0); mgl_data_modify(in,"0.3+sin(4*pi*x)",0); mgl_set_axis_2d(gr,-1.,-2.,1.,2.); mgl_plot(gr,rnd,". "); mgl_box(gr,1); mgl_fit_1(gr,res,rnd,"a+b*sin(c*x)","abc",ini); mgl_plot(gr,res,"r"); mgl_plot(gr,in,"b"); mgl_puts_ext(gr,-1.,-1.3,0.,"fitted:","L:r",-1.,'t'); mgl_puts_fit(gr,0.,-1.8,0.,"y = ","C:r",-1.); mgl_puts_ext(gr,0.,2.2,0.,"initial: y = 0.3+sin(2\\pi x)","C:b", -1., 't');
Fortran code
integer rnd,in,res, mgl_create_data_size real ini(3) ini(1)=1; ini(2)=1; ini(3)=3; rnd = mgl_create_data_size(100,1,1); in = mgl_create_data_size(100,1,1); res = mgl_create_data(); call mgl_data_modify(rnd,"0.4*rnd+0.1+sin(4*pi*x)",0); call mgl_data_modify(in,"0.3+sin(4*pi*x)",0); call mgl_set_axis_2d(gr,-1.,-2.,1.,2.); call mgl_plot(gr,rnd,". "); call mgl_box(gr,1); call mgl_fit_1(gr,res,rnd,"a+b*sin(c*x)","abc",ini); call mgl_plot(gr,res,"r"); call mgl_plot(gr,in,"b"); call mgl_puts_ext(gr,-1.,-1.3,0.,"fitted:","L:r",-1.,'t'); call mgl_puts_fit(gr,0.,-1.8,0.,"y = ","C:r",-1.); call mgl_puts_ext(gr,0.,2.2,0.,"initial: y = 0.3+sin(2\\pi x)","C:b", -1., 't');
Python
rnd, In, res, ini = mglData(100), mglData(100), mglData(), mglData(3); rnd.Modify("0.4*rnd+0.1+sin(4*pi*x)"); In.Modify("0.3+sin(4*pi*x)"); gr.SetRanges(-1,1,-2,2); gr.Plot(rnd,". "); gr.Box(); ini[0], ini[1], ini[2] = 1, 1, 3; gr.Fit(res,rnd,"a+b*sin(c*x)","abc",ini); gr.Plot(res,"r"); gr.Plot(In,"b"); gr.Puts(-1,-1.3,0,"fitted:","L:r",-1); gr.PutsFit(0,-1.8,0,"y = ","C:r",-1); gr.Puts(0,2.2,0,"initial: y = 0.3+sin(2\\pi x)","C:b",-1);
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |