## From \$ROOTSYS/tutorials/fit/Ifit.C

```//
//   Example of a program to fit non-equidistant data points
//   =======================================================
//
//   The fitting function fcn is a simple chisquare function
//   The data consists of 5 data points (arrays x,y,z) + the errors in errorsz
//   More details on the various functions or parameters for these functions
//   can be obtained in an interactive ROOT session with:
//    Root > TMinuit *minuit = new TMinuit(10);
//    Root > minuit->mnhelp("*")  to see the list of possible keywords
//    Root > minuit->mnhelp("SET") explains most parameters
//Author: Rene Brun

#include "TMinuit.h"

Float_t z[5],x[5],y[5],errorz[5];

//______________________________________________________________________________
Double_t func(float x,float y,Double_t *par)
{
Double_t value=( (par[0]*par[0])/(x*x)-1)/ ( par[1]+par[2]*y-par[3]*y*y);
return value;
}

//______________________________________________________________________________
void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
{
const Int_t nbins = 5;
Int_t i;

//calculate chisquare
Double_t chisq = 0;
Double_t delta;
for (i=0;i<nbins; i++) {
delta  = (z[i]-func(x[i],y[i],par))/errorz[i];
chisq += delta*delta;
}
f = chisq;
}

//______________________________________________________________________________
void Ifit()
{
// The z values
z[0]=1;
z[1]=0.96;
z[2]=0.89;
z[3]=0.85;
z[4]=0.78;
// The errors on z values
Float_t error = 0.01;
errorz[0]=error;
errorz[1]=error;
errorz[2]=error;
errorz[3]=error;
errorz[4]=error;
// the x values
x[0]=1.5751;
x[1]=1.5825;
x[2]=1.6069;
x[3]=1.6339;
x[4]=1.6706;
// the y values
y[0]=1.0642;
y[1]=0.97685;
y[2]=1.13168;
y[3]=1.128654;
y[4]=1.44016;

TMinuit *gMinuit = new TMinuit(5);  //initialize TMinuit with a maximum of 5 params
gMinuit->SetFCN(fcn);

Double_t arglist[10];
Int_t ierflg = 0;

arglist[0] = 1;
gMinuit->mnexcm("SET ERR", arglist ,1,ierflg);

// Set starting values and step sizes for parameters
static Double_t vstart[4] = {3, 1 , 0.1 , 0.01};
static Double_t step[4] = {0.1 , 0.1 , 0.01 , 0.001};
gMinuit->mnparm(0, "a1", vstart[0], step[0], 0,0,ierflg);
gMinuit->mnparm(1, "a2", vstart[1], step[1], 0,0,ierflg);
gMinuit->mnparm(2, "a3", vstart[2], step[2], 0,0,ierflg);
gMinuit->mnparm(3, "a4", vstart[3], step[3], 0,0,ierflg);

// Now ready for minimization step
arglist[0] = 500;
arglist[1] = 1.;

// Print results
Double_t amin,edm,errdef;
Int_t nvpar,nparx,icstat;
gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);
//gMinuit->mnprin(3,amin);

}

```
Ifit.C:1
Ifit.C:2
Ifit.C:3
Ifit.C:4
Ifit.C:5
Ifit.C:6
Ifit.C:7
Ifit.C:8
Ifit.C:9
Ifit.C:10
Ifit.C:11
Ifit.C:12
Ifit.C:13
Ifit.C:14
Ifit.C:15
Ifit.C:16
Ifit.C:17
Ifit.C:18
Ifit.C:19
Ifit.C:20
Ifit.C:21
Ifit.C:22
Ifit.C:23
Ifit.C:24
Ifit.C:25
Ifit.C:26
Ifit.C:27
Ifit.C:28
Ifit.C:29
Ifit.C:30
Ifit.C:31
Ifit.C:32
Ifit.C:33
Ifit.C:34
Ifit.C:35
Ifit.C:36
Ifit.C:37
Ifit.C:38
Ifit.C:39
Ifit.C:40
Ifit.C:41
Ifit.C:42
Ifit.C:43
Ifit.C:44
Ifit.C:45
Ifit.C:46
Ifit.C:47
Ifit.C:48
Ifit.C:49
Ifit.C:50
Ifit.C:51
Ifit.C:52
Ifit.C:53
Ifit.C:54
Ifit.C:55
Ifit.C:56
Ifit.C:57
Ifit.C:58
Ifit.C:59
Ifit.C:60
Ifit.C:61
Ifit.C:62
Ifit.C:63
Ifit.C:64
Ifit.C:65
Ifit.C:66
Ifit.C:67
Ifit.C:68
Ifit.C:69
Ifit.C:70
Ifit.C:71
Ifit.C:72
Ifit.C:73
Ifit.C:74
Ifit.C:75
Ifit.C:76
Ifit.C:77
Ifit.C:78
Ifit.C:79
Ifit.C:80
Ifit.C:81
Ifit.C:82
Ifit.C:83
Ifit.C:84
Ifit.C:85
Ifit.C:86
Ifit.C:87
Ifit.C:88
Ifit.C:89
Ifit.C:90
Ifit.C:91
Ifit.C:92
Ifit.C:93
Ifit.C:94
Ifit.C:95
Ifit.C:96
Ifit.C:97
Ifit.C:98
Ifit.C:99
Ifit.C:100