# Question on nonlinear least square curve fitting

Please excuse a ROOT newbie for a simple question. I googled and searched on this forum, but somehow I cannot find the right answer.

I want to do a nonlinear least square fitting of a set of general data (x_i, y_i, y_err_i) (NOT a histogram) to an equation y = f(x). There are many APIs in ROOT: FitPanel, Fit(), RooFit, etc. I got confused which one I should use. I don’t need GUI, so it seems that the ROOT::Math::Minimizer is the newest interface. There is a simple example: root.cern.ch/drupal/content/nume … nimization
but it does not explain how to use it for least square curve fitting, such as how to fix the parameters, how to set upper and lower bounds, how to calculate the errorbars of the fitting parameters.

I used to do it easily with matlab (lsqnonlin). But now I’m trying to use ROOT. Could some provide some code example? I cannot find a good one from the the tutorial nor the documents. Thanks a million!

Try the source code from the bottom of the web page that you cite, from the “Creating a Minimizer via the Plug-In Manager” section.

See “SetVariable”, “SetLowerLimitedVariable”, “SetUpperLimitedVariable”, “SetLimitedVariable”, “SetFixedVariable” (and all another methods) in http://project-mathlibs.web.cern.ch/project-mathlibs/sw/Minuit2/html/classROOT_1_1Math_1_1Minimizer.html

As far as I am aware, unfortunately, if you “limit” any variable, you cannot easily “free” it. You have to call the Minimizer::Clear() method to clear ALL the parameter states and re-set (re-define) ALL of them again.
The opposite, setting first a parameter “free” and then “limited” is possible, just by calling Minimizer::SetLimitedVariable the second time.

Thanks a million! I managed to fit by modifying the “Creating a Minimizer via the Plug-In Manager” section.

However, I have one more question: how can pass my data to chi2? ROOT::Math::Functor f(&chi2,2) seems only accept one parameter. Now I make the data variables global, as suggested for TMinuit in the following link: root.cern.ch/drupal/content/how- … ata-points
In TMinuit, the only way is to make the data variables global.

I figured out a way to pass the variable by implanting a FCN class, and then wrapped it with Functor, taking from the “testMinimize.cxx” example. root.cern.ch/viewvc/trunk/math/m … iew=markup

Are there any better ways to do it?

[quote=“Pepe Le Pew”]Try the source code from the bottom of the web page that you cite, from the “Creating a Minimizer via the Plug-In Manager” section.

See “SetVariable”, “SetLowerLimitedVariable”, “SetUpperLimitedVariable”, “SetLimitedVariable”, “SetFixedVariable” (and all another methods) in http://project-mathlibs.web.cern.ch/project-mathlibs/sw/Minuit2/html/classROOT_1_1Math_1_1Minimizer.html

As far as I am aware, unfortunately, if you “limit” any variable, you cannot easily “free” it. You have to call the Minimizer::Clear() method to clear ALL the parameter states and re-set (re-define) ALL of them again.
The opposite, setting first a parameter “free” and then “limited” is possible, just by calling Minimizer::SetLimitedVariable the second time.

I’m afraid I have a simple function:
Double_t chi2(const Double_t *x)
which internally uses some “global data”, which I then use as:
ROOT::Math::Functor *functor = new ROOT::Math::Functor(&chi2, 10); // “chi_2” with 10 variables
ROOT::Math::Minimizer *minimizer = ROOT::Math: :CreateMinimizer(minName, algoName);
minimizer->SetFunction(*functor);

I end up by encapsulating the data in a functor class.

ROOT::Math::Minimizer *minimizer = ROOT::Math: :CreateMinimizer(minName, algoName);