# 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:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![eodwyer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eodwyer](https://root-forum.cern.ch/u/eodwyer)\
**Post date:** [June 22, 2009, 2:00pm UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/1 "2009-06-22T14:00:21Z")

</div>

I’m trying to fit a function of the form f(x)=integral(g(s)exp(-(x-s)^2/2)ds), but am having some difficulty. I found a parital solution that looks like:

> [@](#):
>
> #include   
> #include   
> #include “TROOT.h”  
> #include “TBaseClass.h”  
> #include “TCanvas.h”  
> #include “TFile.h”  
> #include “TH1.h”  
> #include “TObject.h”  
> #include “TString.h”  
> #include “TF1.h”  
> #include “TMath.h”  
> #include “TStyle.h”  
> #include “TH1F.h”  
> #include “TChain.h”  
> #include “TF2.h”  
> #include “TF12.h”
> 
> Double\_t funct(Double\_t \*x, Double\_t \*p){
> 
> Double\_t X1=p[1]\*p[0]\*pow(x[0]-x[1],2);  
> return X1;
> 
> }
> 
> Double\_t function(Double\_t \*x, Double\_t \*p)  
> {
> 
> TF2 \*f1=new TF2(“f1”,funct,0,3000,0,3000,2);  
> TF12 \*f12=new TF12(“f12”,f1,x[0],“x”);  
> Double\_t sum=f12-\>Integral(0,3000);  
> return sum;
> 
> void spectrum(){  
> Double\_t alpha =1;  
> Double\_t k=.4;  
> TF1 \*A\_t = new TF1(“A\_t”,function,0,3000,2);  
> A\_t-\>SetParameters(alpha,k);  
> A\_t-\>Draw();  
> }

which works if I remove the parameters p[0], p[1], but otherwise produces a flat line at zero.(I chose an arbitrary function with two variables, as the function I wish to fit is much more complicated.) Is there a way I can get this to work with parameters, or is this method hopeless? Thanks.

---

<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:** [June 22, 2009, 5:37pm UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/2 "2009-06-22T17:37:33Z")

</div>

your not setting the parameters of the TF2 in function

something like this should work:

```auto
Double_t function(Double_t *x, Double_t *p) 
{ 
 TF2 *f1=new TF2("f1","[1]*[0]*exp(x-y)",0,3,0,3); 
 f1->SetParameters(p[0],p[1]);
 TF12 *f12=new TF12("f12",f1,x[0],"x"); 
 Double_t sum=f12->Integral(0,3); 
 return sum;
}
```

---

<div class="post-metadata">

**Author:** ![eodwyer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eodwyer](https://root-forum.cern.ch/u/eodwyer)\
**Post date:** [July 2, 2009, 6:11pm UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/3 "2009-07-02T18:11:03Z")

</div>

Your suggestion seemed to work the first time I tried it, but the old problem has returned again and i’m not sure what’s going on.

[quote]  
Double\_t function(Double\_t \*x, Double\_t \*p)  
{

TF2 _f1=new TF2(“f1”,"[0]_[1]\*exp(x-y)",0,3,0,3);  
f1-\>SetParameters(p[0],p[1]);  
TF12 \*f12=new TF12(“f12”,f1,x[0],“x”);

Double\_t sum=f12-\>Integral(0,3);  
return sum;  
}

void spectrumtester(){  
Double\_t alpha=2;  
Double\_t k=1;

TF1 \*A\_t = new TF1(“A\_t”,function,0,3.00,2);

```
A_t->SetParameters(alpha,k);

A_t->Draw();

```

}[/quote]

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [July 2, 2009, 7:52pm UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/4 "2009-07-02T19:52:47Z")

</div>

Could you try the following:

[code]TF2\* f1;  
Double\_t function(Double\_t \*x, Double\_t \*p) {  
TF12 \*f12=new TF12(“f12”,f1,x[0],“x”);  
Double\_t sum=f12-\>Integral(0,3);  
delete f12;  
return sum;  
}

void spectrumtester(){  
Double\_t alpha=2;  
Double\_t k=1;

f1=new TF2(“f1”,"[0]\*[1]\*exp(x-y)",0,3,0,3);  
f1-\>SetParameters(alpha,k);  
TF1 \*A\_t = new TF1(“A\_t”,function,0,3.00,2);

A\_t-\>SetParameters(alpha,k);

A\_t-\>Draw();

}[/code]

Rene

---

<div class="post-metadata">

**Author:** ![eodwyer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eodwyer](https://root-forum.cern.ch/u/eodwyer)\
**Post date:** [July 3, 2009, 1:22am UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/5 "2009-07-03T01:22:58Z")

</div>

Same result as before.

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [July 3, 2009, 4:40am UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/6 "2009-07-03T04:40:00Z")

</div>

Could you be more explicit? what is the problem?

Rene

---

<div class="post-metadata">

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

</div>

If I run the code without the parameters [0], [1] I get the expected curve. If I run it with the parameters, I get a flat line at zero.

---

<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

---

<div class="post-metadata">

**Author:** ![eodwyer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/b4bc9f/32.png) [@eodwyer](https://root-forum.cern.ch/u/eodwyer)\
**Post date:** [July 3, 2009, 3:20pm UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/9 "2009-07-03T15:20:17Z")

</div>

Thanks!

---

<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, 5:31pm UTC](https://root-forum.cern.ch/t/fitting-an-integral-function/8517/10 "2009-07-03T17:31:33Z")

</div>

just noticed:  
you should delete f1 in function after it is used or create it on stack or use way 3  
otherwise you have a pretty severe memory leak.
