// Double_t myfunc(Double_t *x, Double_t *p) { Double_t myfunc(Double_t x[], Double_t p[]) { return p[0] * sin( (x[0]-p[2]) ) / (x[0]-p[2]) + p[1] ; // p[0] : κλίμακα // p[1] : μετατόπιση στο y // p[2] : μετατόπιση στο x } void sinc() { int np=301; double x[np]; double y[np]; double ex[np]; double ey[np]; // x: -10 .. 10 double x1=-10; double x2= 10; // y: sin(x)/x +- gaussian noise TTimeStamp *t = new TTimeStamp(); gRandom->SetSeed(t->GetNanoSec()); for (int i=0 ; iGaus(0,0.5) -3.2 + 10* ( (x[i]-1.5)==0 ? 1 : sin(x[i]-1.5)/(x[i]-1.5) ) ; ex[i] = (x2-x1)/(np-1)/3.; ey[i] = 0.02+fabs(0.01*y[i]); } TCanvas *c1 = new TCanvas("canvas11","sinc data simulation and fit",1200,800); gStyle->SetOptFit(1); c1->SetGridx(); c1->SetGridy(); c1->Draw(); TGraphErrors *g = new TGraphErrors(np,x,y,ex,ey); g->SetName("g"); g->SetTitle("fitted data"); g->SetMarkerColor(8); g->SetMarkerStyle(20); g->SetMarkerSize(1); g->Draw("AP"); TF1 *fitfunc = new TF1( "fname", myfunc, -10, 10, 3); fitfunc->SetLineWidth(2); fitfunc->SetParameter(0, 0.8); fitfunc->SetParameter(1, 0.5); fitfunc->SetParameter(2, 0.5); fitfunc->SetParLimits(0, 0, 10); fitfunc->SetParLimits(1, -100, 100); fitfunc->SetParLimits(2, -10, 10); g->Fit(fitfunc); }