I,m trying to fit a simple straight line (y=ax+b) using the class tminuit. However, the coefficients results are different from that obtained through gnuplot.
In gnuplot, I have obtained:
a=3.0 ;
b=-0.001
in root, I get
a=2.22E-16
b=-3.0
root macro:
#include<iostream>
#include<TMinuit.h>
#include<TApplication.h>
#include"TF1.h"
#include"TF2.h"
#include"TMath.h"
using namespace std;
Float_t x[6],y[6];
Double_t func(float x,float y, Double_t *par)
{
Double_t value=( par[1] + par[0]*x ) ;
return value;
}
//______________________________________________________________________________
void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
{
const Int_t nbins = 6;
Int_t i;
for (i=0;i<nbins; i++) {
f = func(x[i],y[i],par) ;
}
}
void run(){
// the x values
x[0]=0.0;
x[1]=0.5;
x[2]=1.0;
x[3]=1.5;
x[4]=2.0;
x[5]=2.5;
// the y values
y[0]=0.0;
y[1]=1.5;
y[2]=3.0;
y[3]=4.5;
y[4]=6.0;
y[5]=7.5;
const int npar=2;
TMinuit minuit(npar);
minuit.SetFCN(fcn);
Double_t arglist[2];
Int_t ierflg = 0;
double par[npar];
double stepSize[npar];
double minVal[npar];
double maxVal[npar];
string parName[npar];
par[0]=2.0;
stepSize[0]=0.01;
minVal[0]=0.0;
maxVal[0]=4.0;
parName[0]="x";
par[1]=-2.0;
stepSize[1]=0.01;
minVal[1]=-3.0;
maxVal[1]=3.0;
parName[1]="y";
arglist[0] = 500.0;
arglist[1] = 1.0;
minuit.mnexcm("MIGRAD", arglist ,1,ierflg);
for(int i=0; i<npar; i++){
minuit.DefineParameter(i, parName[i].c_str(),
par[i],stepSize[i],minVal[i],maxVal[i]);
}
minuit.Migrad();
double outpar[npar],err[npar];
for(int i=0; i<npar; i++){
minuit.GetParameter(i,outpar[i],err[i]);
cout<<"Fitted parameter is:" << i <<
" "<<outpar[i]<<" "<< "+/-" <<
" " << err[i] <<endl;
}
}