# Problem in user defined function Dear ROOT

I have the difficulty in making a user-defined function having another
user defined function as the input.
Please refer to the following script and point out what’s wrong…

=>Following is my test script

Double_t fn1(Double_t *x, Double_t *par)
{ return par+par*x; }

Double_t fn2(Double_t *x, Double_t *par)
{ return par*cos(x); }

Double_t fn3(Double_t *x, Double_t *par)
{
return sqrt( fn2(fn1(x,par),par)) ;
}

void test7(){

Double_t ipar ={1,2,3};
TF1 *f1 = new TF1(“f1”,fn3,0,10,3);
for(int i=0;i<3;i++)
{ f1->SetParameter(i,ipar[i]);}
f1.Draw();
}

=> result of the script
Error: Function fn1(x,par) is not defined in current scope test7.C:9:

Hi,

I am not well in c++, but I think the problem is that
your function “Double_t fn2(Double_t *x, Double_t *par)” as an argument
takes a Double_t pointers, but in your program fn1 returns simple Double_t.

Hi,

very good, rafopar! It’s that and it starts even earlier: you call fn1(x,par) where x is a Double_t* and thus x is a Double_t. But there is only a fn1 taking a Double_t* - so you should call fn1(x,par) or change the signature of fn1 to take Double_t instead of Double_t*.

Cheers, Axel.

Do something like shown below. However note that your function fn2 can return negative values, so you cannot compute the sqrt(fn2).

Rene

[code]Double_t fn1(Double_t *x, Double_t *par)
{ return par+par*x; }

Double_t fn2(Double_t *x, Double_t *par)
{ return par*cos(x); }

Double_t fn3(Double_t *x, Double_t *par)
{
//return sqrt( fn2(fn1(x,par),par)) ;
double vfn1 = fn1(x,par);
double vfn2 = fn2(&vfn1,par) ; //vfn2 has negative values
printf(“x=%g, vfn1=%g, vfn2=%g\n”,x,vfn1,vfn2);
return sqrt(TMath::Abs(vfn2)) ;
}

void test7(){

Double_t ipar ={1,2,3};
TF1 *f1 = new TF1(“f1”,fn3,0,10,3);
for(int i=0;i<3;i++) { f1->SetParameter(i,ipar[i]);}
f1->Draw();
}
[/code]