# Pass a 3D function with parameters to the minimizer

Hello,

I am trying to pass a 3-D function with parameters to the minimizer. I define my 3D function like:

``````double MinimizeXYZROOT(double *xx, double *params){

const Double_t x = xx[0];
const Double_t y = xx[1];
const Double_t z = xx[2];

double *p = (double *)params;
.............
.............
return value;
}
``````

This is the way I am trying to do the minimisation right now:

``````..................................
.................................

TF3 *f3=new TF3("",MinimizeXYZROOT,-500,500,-500,500,0.00001,1000);

ROOT::Math::WrappedTF3 f(f3);

for(int ipar=0;ipar<iEnt;ipar++){
f3->SetParameter(ipar,ParArray[ipar]);
}

minimum->SetFunction(f);
.........................
.........................
``````

Can someone please guide me on how to pass a 3D function with parameters to the minimizer?

Thanks,
Uzair

ROOT Version: 6.20/04
Platform: Ubuntu 20.04.1
Compiler: 9.3.0

Hi,

I presume you want to minimize `f(x,y,z|p)` given the parameters `p` and not finding the best values of the paramters `p`, given some `x,y,z` values.

In that case, you have two possibilities:

1. Create a TF3 class and then call TF3::GetMinimumXYZ(), see
In this case Minuit is used by default but you can change the Minimizer by calling before,
`ROOT::Math::MinimizerOptions::SetDefaultMinimizer( minimizerName)`

Possible choices for minimizerName are:

``````- Minuit, Minuit2, GSLMultiMin, GSLSimAn, Genetic
``````

The correct way to create a TF3 is also

``````TF3 *f3=new TF3("",MinimizeXYZROOT,-500,500,-500,500,0.00001,1000, npar);
``````

where `npar` is the number of function parameters

2. You use directly the MInimizer interface as shown in this tutorial:
https://root.cern/doc/master/NumericalMinimization_8C.html

In this case, it is still convenient to create a `TF3` and then you pass the TF3 to the minimizer, by
wrapping using the `ROOT::Math::WrappedMultiTF1’ class. For example doing:

``````TF3 *f3=new TF3("",MinimizeXYZROOT,-500,500,-500,500,0.00001,1000, npar);
f3->SetParameters(......)
ROOT::Math::WrappedMultiTF1 f(*f3);
ROOT::Math::Minimizer* minimum = ROOT::Math::Factory::CreateMinimizer(minName, algoName);
minimum->SetFunction(f);
// use same code as tutorial (set variables passing initial values and step sizes)
minimum->SetVariable(0,"x",variable[0], step[0]);
minimum->SetVariable(1,"y",variable[1], step[1]);
minimum->SetVariable(2,"z",variable[2], step[2]);
// minimize function
minimum->Minimize();
``````

Cheers

Lorenzo

Thank you Lorenzo! The first case worked perfectly. Regarding the second case I am having a problem. When I say:

ROOT::Math::WrappedMultiTF1 f(f3);

Root says that it is not defined and there is no such class. Can you please guide me here?

Thanks,
Uzair

Hi

Sorry , there was an error in my code (I have updated now). The line should be:

``````ROOT::Math::WrappedMultiTF1 f(*f3);
``````

Actually `ROOT::Math::WrappedMultiTF1` is a typedef to `ROOT::Math::WrappedMultiTF1Templ<double>`, but it should be recognized by ROOT and work fine

Lorenzo

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.