# NumericalMinimization¶

Example on how to use the new Minimizer class in ROOT Show usage with all the possible minimizers. Minimize the Rosenbrock function (a 2D -function) This example is described also in http://root.cern.ch/drupal/content/numerical-minimization#multidim_minim input : minimizer name + algorithm name randomSeed: = <0 : fixed value: 0 random with seed 0; >0 random with given seed

Author: Lorenzo Moneta
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, October 05, 2022 at 09:46 AM.

Definition of a helper function:

In :
%%cpp -d

#include "Math/Minimizer.h"
#include "Math/Factory.h"
#include "Math/Functor.h"
#include "TRandom2.h"
#include "TError.h"
#include <iostream>

double RosenBrock(const double *xx )
{
const double x = xx;
const double y = xx;
const double tmp1 = y-x*x;
const double tmp2 = 1-x;
return 100*tmp1*tmp1+tmp2*tmp2;
}


Arguments are defined.

In :
const char * minName = "Minuit2";
const char *algoName = "";
int randomSeed = -1;


create minimizer giving a name and a name (optionally) for the specific algorithm possible choices are: minName algoName Minuit /Minuit2 Migrad, Simplex,Combined,Scan (default is Migrad) Minuit2 Fumili2 Fumili GSLMultiMin ConjugateFR, ConjugatePR, BFGS, BFGS2, SteepestDescent GSLMultiFit GSLSimAn Genetic

In :
ROOT::Math::Minimizer* minimum =
ROOT::Math::Factory::CreateMinimizer(minName, algoName);
if (!minimum) {
std::cerr << "Error: cannot create minimizer \"" << minName
<< "\". Maybe the required library was not built?" << std::endl;
return 1;
}


set tolerance , etc...

In :
minimum->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
minimum->SetMaxIterations(10000);  // for GSL
minimum->SetTolerance(0.001);
minimum->SetPrintLevel(1);


create function wrapper for minimizer a IMultiGenFunction type

In :
ROOT::Math::Functor f(&RosenBrock,2);
double step = {0.01,0.01};


starting point

In :
double variable = { -1.,1.2};
if (randomSeed >= 0) {
TRandom2 r(randomSeed);
variable = r.Uniform(-20,20);
variable = r.Uniform(-20,20);
}

minimum->SetFunction(f);


Set the free variables to be minimized !

In :
minimum->SetVariable(0,"x",variable, step);
minimum->SetVariable(1,"y",variable, step);


do the minimization

In :
minimum->Minimize();

const double *xs = minimum->X();
std::cout << "Minimum: f(" << xs << "," << xs << "): "
<< minimum->MinValue()  << std::endl;

Minuit2Minimizer: Minimize with max-calls 1000000 convergence for edm < 0.001 strategy 1
Minuit2Minimizer : Valid minimum - status = 0
FVAL  = 1.84172281656905818e-08
Edm   = 1.8496132831401442e-08
Nfcn  = 174
x	  = 0.999903	 +/-  1.00396
y	  = 0.999796	 +/-  2.01024
Minimum: f(0.999903,0.999796): 1.84172e-08


expected minimum is 0

In :
if ( minimum->MinValue()  < 1.E-4  && f(xs) < 1.E-4)
std::cout << "Minimizer " << minName << " - " << algoName
<< "   converged to the right minimum" << std::endl;
else {
std::cout << "Minimizer " << minName << " - " << algoName
<< "   failed to converge !!!" << std::endl;
Error("NumericalMinimization","fail to converge");
}

return 0;

Minimizer Minuit2 -    converged to the right minimum