Different quadratic fit(Data from file)

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20/02
_Compiler: gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)

Hello everyone!

I have some data in a .txt file and I want to have a quadratic fit.
I know that my data does not seems to match so much with this fit (However I want this fit. I run the code many times with different data, so i tried to have the best starting point for my data, that’s why I have these numbers at SetParameters ).
When I do the fit in excel or mathematica (with the same data) I have different parameters from those found in root! I can not understand why!

(I add a piece of my code and the data)

TGraph *MyGraph1=new TGraph("selectedX.txt");
TF1 *  f2 = new TF1("f2","pol2");

Thanks a lot!
selectedX.txt (101 Bytes)

depending on the exact fitting algorithm results might be different, especially for fits that are expected to be bad. It could also be a local optima behavior. @moneta might be able to comment further.


1 Like


It is true that Minuit does not work very well with that data, this is caused by some numerical error.
It is true If you run the linear fitter (default for polynomials if you don’t use the B option), you get a better result, probably the same in Mathematica.
For doing a linear fitter do:


However, I would translate the x value and define better the function to minimise the numerical error and have parameters which do not have such large values. I would fit a function defined as:

TF1 *  f2 = new TF1("f2","[a]*(x-80.)*(x-80.)+[b]*(x-80.)+[c]");


1 Like

Hello Lorenzo,

I tried


The parameters from the fit are much better now (They are not similar with mathematica, but they are pretty close!)


TF1 *  f2 = new TF1("f2","[a]*(x-80.)*(x-80.)+[b]*(x-80.)+[c]");

I am not sure that I can figure out what values do I have to set for [a], [b], [c]

Thank you very much!

You can figure out from the plot, but (since it is a polynomial) you can try from {1,1,1}.
Note, that if using the linear fitting you don’t need to set initial values


Yeah, yeah, I missed that!
Thank you very much!