# Fitting an integral function

**URL:** <https://root-forum.cern.ch/t/fitting-an-integral-function/8517>\
**Category:** ROOT\
**Created:** [June 22, 2009, 2:00pm UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517 "2009-06-22T14:00:21Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![MadCow](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@MadCow](https://root-forum.cern.ch/u/MadCow)\
**Post date:** [July 3, 2009, 2:46pm UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/8 "2009-07-03T14:46:46Z")

</div>

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)

```auto
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

---

_[View the full topic](https://root-forum.cern.ch/t/fitting-an-integral-function/8517)._
