Fails fitting fuction

Hi
I am working with the TF1 formula. i want to fir a pol5 into some data I have. Then what I did first is before I modified my main program writen in c++, I wrote a small scrip in root to make sure I was coding it right. i even used the same values from my data to fit the polynomial. Then after the root script was working, I modified my main program and it happens that I had the following error:

x: 1.200000 y: 1466.705322
x: 1.100000 y: 879.823364
x: 1.000000 y: 512.609497
x: 0.900000 y: 272.953857
x: 0.900000 y: 275.149445
x: 0.800000 y: 136.832687
x: 0.700000 y: 60.892311
x: 0.700000 y: 60.892311
x: 0.600000 y: 25.235888
x: 0.500000 y: 9.096155
x: 0.400000 y: 2.264582
Error in TDecompChol::Decompose(): matrix not positive definite
Error in TDecompChol::Solve(): Decomposition failed
Error in TLinearFitter::Eval: Matrix inversion failed
Parameters:
NO. VALUE ERROR
0 0.000000e+00 0.000000e+00
1 0.000000e+00 0.000000e+00
2 0.000000e+00 0.000000e+00
3 0.000000e+00 0.000000e+00
4 0.000000e+00 0.000000e+00
5 0.000000e+00 0.000000e+00
Normal termination…

bash-3.1$
{End of output}

Here next I have attached the relevant c++ code where I want to introduce the TF1 object for fitting and plotting. In the last part of this post is the root script I wrote. As you noticed both are the pretty-close the same. Does anyone knows why I get this error?

Relevant code in c++

c1->cd(2);
color_marker=2;
//this for loop loads chooses the energy peak of interest. Here “+24”
//I have choosen only one energy i.e this loop only runs once
for (ii=MEAN;ii<PK_A;ii=ii+NO_OUT_PARAM+24){
g25 = fitResults[6][ii]/fitResults[7][ii];
float gain[]={1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, g25, g25, g25, g25}; //LaBr3

//This loop load all the col values of the matrix into array y
//note that array "volts" is a copy of "x"
for (jj=0;jj<(sizeof(x)/sizeof(x[0]));jj++){
  y[jj]  = fitResults[jj][ii]*gain[jj]; //MEAN 
  volts[jj]=x[jj];
}       
fPol = new TF1("fPol","pol5",volts[0],volts[(sizeof(x)/sizeof(x[0]))]);
gr=new TGraph((sizeof(x)/sizeof(x[0])),volts,y);    
//gr->Draw("ap");   //<-I tried this
gr->Fit(fPol,"R","",volts[(sizeof(x)/sizeof(x[0]))],volts[0]);
//for(k=0;k<(sizeof(x)/sizeof(x[0]));k++)printf("x: %f \t y: %f\n",volts[k],y[k]);
fPol->Draw("l");
gr->SetMarkerColor(color_marker++);
gr->SetMarkerStyle(21);

mg->Add(gr);
}

mg->Draw("apl");
mg->GetXaxis()->SetTitle("Voltage [kV]");
mg->GetXaxis()->CenterTitle(); 
mg->GetYaxis()->SetTitle("FWHM/E [channel/channel]"); //Resolution
mg->GetYaxis()->CenterTitle();

{end c++ code}
Next in the script code run in root:
==================================================root fitPol.C
void fitPol(){
int i;
TCanvas *c1=new TCanvas(“c1”,"",200,10,700,500);
TGraph *g;

c1->Divide(2,1);
c1->cd(1);
c1->SetGrid();
Float_t x[] = {1.2, 1.1, 1.0, 0.9, 0.9, 0.8, 0.7, 0.7, 0.6, 0.5, 0.4};
int n=(sizeof(x)/sizeof(x[0]));
//Float_t y[n];
//for(i=0;i<n;i++)y[i]=0.5*x[i]**5;
Float_t y[]={1466.70,879.82,512.61,272.95,275.15,136.83,60.89,60.89,25.23,9.10,2.26};

TF1 *f =new TF1(“fp”,“pol5”,1.2,0.4);
gr=new TGraph(n,x,y);
gr->Draw(“ap”);
gr->SetMarkerStyle(21);
c1->cd(2);
gr->Fit(f,“R”,"",x[n-1],x[0]);
f->Draw(“l”);
}
data.tar.gz (380 KB)
MainFrame.tar (30 KB)
fitPol.C (1.58 KB)

I started looking at your problem, but your C-like code is hard to follow and anyway cannot work as it is:
-your NFILES should be 12 and not 20
-NF1=NF2=0. As a result fileList[NF2} overwrites fileList[NF1]

I suggest to write your TGraph to a ROOT file and simply write a 3 lines long script reading and fitting this graph.

Rene