# Fitting an integral function

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:

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.

your not setting the parameters of the TF2 in function

something like this should work:

``````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;
}``````

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]

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

Same result as before.

Could you be more explicit? what is the problem?

Rene

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.

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

Thanks!

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.