brun’s code doesn’t work on my pc either.
It works when I add the SetParameters(p[0],p[1]) line i suggested before;
complete working example:
3 Possible solutions. way 3 is bruns version with a setparameters. All work (root 5.22)
Double_t ef(Double_t *x, Double_t *p)
{
return exp(x[0]-x[1])*p[0]*p[1];
}
//TF2 * f1; // way 3
Double_t function(Double_t *x, Double_t *p)
{
TF2 *f1=new TF2("f1","[1]*[0]*exp(x-y)",0,3,0,3); //way 1
//TF2 *f1=new TF2("f1",ef,0,3,0,3,2); //way 2
f1->SetParameters(p[0],p[1]);
TF12 *f12=new TF12("f12",f1,x[0],"x");
Double_t sum=f12->Integral(0,3);
delete f1; // way 1 and 2
return sum;
}
int root()
{
int x[] = {0,1,2,3,4};
int y[] = {858.686,315.893,116.21,42.7515,15.7274};
TGraph * gr = new TGraph(5,x,y);
gr->Draw("A*");
//f1=new TF2("f1",ef,0,3,0,3,2); //way3
TF1 *fitfkt=new TF1("fitfkt",function,0,4,2);
fitfkt->SetParameters(4,1);
gr->Fit(fitfkt);
//delete f1; //way 3
return 0;
}
edit: fixed memory leak