Problem in user defined function

:confused:
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[0]+par[1]*x[0]; }

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

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

void test7(){

Double_t ipar[3] ={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[0],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[0],par) where x is a Double_t* and thus x[0] 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[0]+par[1]*x[0]; }

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

Double_t fn3(Double_t *x, Double_t *par)
{
//return sqrt( fn2(fn1(x[0],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[0],vfn1,vfn2);
return sqrt(TMath::Abs(vfn2)) ;
}

void test7(){

Double_t ipar[3] ={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]