# Why derivate negative?

Hi,

I’m struggling over a certain issue for now more than an hour and I can’t figure it out…

I want to use a polynom fit of a second order for six data points (for four it works perfectly) but in about half of the fits it returns a negative derivate. This seems to be coming from the fact that the “GetX” function does not work properly in this case.

The code:

double m_corresponding_Ubias_fit;
double certain_data_points_M[6];
double certain_data_points_Ubias[6];
(…)
TGraphErrors* raw_err = new TGraphErrors(6, certain_data_points_Ubias, certain_data_points_M);

TF1* fit_raw = new TF1(“Raw data fit”, “pol2”,330,399);
fit_raw->SetLineColor(kRed);

raw_err->SetMarkerStyle(22);
raw_err->SetMarkerColor(kBlue-2);
raw_err->SetMarkerSize(2);
raw_err->GetXaxis()->SetTitle(“U_{bias}”);
raw_err->GetYaxis()->SetTitle(“M”);
raw_err->GetXaxis()->SetRangeUser(330, 399);
raw_err->GetYaxis()->SetRangeUser(1, 210);

raw_err->Fit(fit_raw, “Q”);

double a_Ubias=0;
a_Ubias=fit_raw->GetX(150.0);
cout << "a_Ubias: " << a_Ubias << endl;
m_corresponding_Ubias_fit=a_Ubias;

double a_dUbias=0;
a_dUbias=fit_raw->Derivative(m_corresponding_Ubias_fit);
a_dUbias=a_dUbias/150.0;
a_dUbias=a_dUbias*100.0;
m_fitted_fit_dM=a_dUbias;

(…)

The data points resp. the arrays are:

certain data points Ubias[0]: 383.992 | certain data points M: 114.783
certain data points Ubias[1]: 385.991 | certain data points M: 127.06
certain data points Ubias[2]: 387.992 | certain data points M: 141.75
certain data points Ubias[3]: 389.991 | certain data points M: 159.531
certain data points Ubias[4]: 391.991 | certain data points M: 181.529
certain data points Ubias[5]: 393.992 | certain data points M: 209.467

The fitted values are then:
a_Ubias: 369.503
dM: -6.22787 Ubias: 369.503

So, according to the arrays and the graphics I really can’t figure out what’s going on… at the recquired y-value of 150 the corresponding x-value is not 369! Why does it result this value ?
Thank you very much in advance!

PS: In case of very similar data points:

certain data points Ubias[0]: 383.978 | certain data points M: 114.047
certain data points Ubias[1]: 385.976 | certain data points M: 126.245
certain data points Ubias[2]: 387.978 | certain data points M: 140.813
certain data points Ubias[3]: 389.978 | certain data points M: 158.523
certain data points Ubias[4]: 391.976 | certain data points M: 180.379
certain data points Ubias[5]: 393.979 | certain data points M: 208.212

a_Ubias: 389.072
dM: 6.25921 Ubias: 389.072

which is correct. And in case of the fit with four data points it is always working correctly.

The “pol2” is a parabola, so for each “y” you may have two different “x” values.
Either limit the range of your “fit_raw” function: `TF1 *fit_raw = new TF1("Raw_data_fit", "pol2", (certain_data_points_Ubias[0] - 1.0), (certain_data_points_Ubias[5] + 1.0));` or: `TF1 *fit_raw = new TF1("Raw_data_fit", "pol2", (raw_err->GetX()[0] - 1.0), (raw_err->GetX()[(raw_err->GetN() - 1)] + 1.0));` or limit the range in which you “GetX”: `a_Ubias=fit_raw->GetX(150.0, (certain_data_points_Ubias[0] - 1.0), (certain_data_points_Ubias[5] + 1.0));` or: `a_Ubias=fit_raw->GetX(150.0, (raw_err->GetX()[0] - 1.0), (raw_err->GetX()[(raw_err->GetN() - 1)] + 1.0));`

1 Like

Unbelievable… I just noticed that I did the same in case of the 4 points-fit. But only because of plotting issues so it was only coincidence that this didn’t happen there.

Unbelievable… cannot say how much I have to thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.