Complex function to fit data : how to enter it?

Hi everyone,

I would like to use a complicated function to fit data. In order to write it in a lighter way, I would like to enter it like this :

f=[0]+ ([1]*x+[2]xx)^n
with n=([1]+[2])/[0]

How can I do that?
f is juste a simple example to explain more clearly what I want to do.

thanks

see this example:

{ TF1 *f = new TF1("f","[0]+TMath::Power([1]*x+[2]*x*x,([1]+[2])/[0])",0,1); f->SetParameters(1,1,1); f->Draw(); }

Rene

OK, because this example is simple … but what about if n is more complicated?

I cannot declare n seperately from f if I understand your answer.

see the many examples that we give about the use of the TF1 classes.
For example you can recode my simple example as follow

double myfunc(double *x, double *par) { double temp = par[1]*x[0] +par[2]*x[0]*x[0]; double n = (par[1]+par[2])/par[0]; double f = par[0]+TMath::Power(temp,n); return f; } void test() { TF1 *f = new TF1("f",myfunc,0,1,3); f->SetParameters(1,1,1); f->Draw(); }

You should be able from this code to extend it to more complex cases.

Rene

I tried te way you suggest :

double test (double* x, double* par)
{
 double n  = (x[0]-par[1]*par[3]/(2*par[2])-par[0])/par[1];
 double f = par[0]*pow(1+pow(n,2),-par[3]);
 return f;
}

void MyGraph()
{
 ...
TF1 *fit10c = new TF1("ft10c",test,1,1500,4)
 ...
}

When I want to use fit10c to fit data using the Fit Panel, I have this error :

Error in TF1::Compile:Bad numerical expression : “test”

I don’t understand what is wrong in “test”.

Thanks for your help

Hi,

Please try with a less common name than ‘test’, this is likely already being used for a different purpose.

Cheers,
Philippe

I tried with other less common names … problem is the same :frowning:

Hi,

I copy/pasted you example and can not reproduce your problem. Also the error seems to indicate that rather than new TF1(“ft10c”,test,1,1500,4) you may have type new TF1(“ft10c”,“test”,1,1500,4) [note the extra quotes].

Cheers,
Philippe.

well I’ve just tried with extra quotes but … still the same problem.

I maybe try on another computer…

Could you post the shortest possible RUNNING script explaining your problem?

Rene

Well I tried to fit my histogram within the macro :

h1->Fit(fit10c,“r”)

and then it works. Seems that the problem comes with the FitPanel that does not recognize fit10c, except if in the macro I write :

h1->Fit(fit10c,“r+”)

and then I can use fit10c within the FitPanel.

Is there an explanation for this?

sorry but the computer I use for root is not connected to the network so it’s hard to copy codes to the forum (transfers from that machine are not allowed.)

Hi,

I would need a running script to reproduce the problem. In addition I would like to know also the ROOT version are you using. Many changes have been applied recently in the FitPanel,

Best Regards

Lorenzo