# Fit TGraphErrors with an Inverse function

Hello, I’m new to Root. My question is if it is possible to fit some points with error bars using the inverse of a user-defined function. Here is the code:

``````const Int_t n1 = 15;
Float_t x1[]  = {0.51, 0.54, 0.56, 0.58, 0.57, 0.64, 0.66, 0.7, 0.75, 0.8, 0.83, 0.92, 0.95, 0.98, 0.99};
Float_t y1[]  = {0.11, 0.2, 0.31, 0.47, 0.75, 3.4, 4.6, 10, 15, 30, 50, 150, 210, 250, 275};
Float_t ex1[]  = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Float_t ey1[]  = {0.0029, 0.0029, 0.0029, 0.0029, 0.0289, 0.0289, 0.0289, 2.8868, 2.8868, 2.8868, 2.8868, 2.8868, 2.8868, 2.8868, 2.8868};
TGraphErrors *gr1 = new TGraphErrors(n1,x1,y1,ex1,ey1);
gr1->Draw("ap");

TF1 *f = new TF1("f", "TMath::Log(x/[0])+[1]*x",.45,1.05);
``````

The point is if I necessarily have to change the x with the y of the points in order to use directly the “f” function or is there a method for doing that without swapping the points’ coordinates?

ROOT Version: 5.23/36
Platform: Windows 10
Compiler: CINT/ROOT C/C++ Intepreter version 5.18.00

You mean that you would like to fit a new graph with `x1` and `y1` swapped using the same function `f` ?

Note: you are using a very old version of ROOT.

That is the B plan.
The A plan is to use the inverse function of `f` for fitting that graph because I prefer not to have the coordinates swapped.
I hope I expressed myself better.

You can try. You need to define f^{-1}.
@moneta may help also.

Ok, but how? I have no clue…

may be this can help ?

According to Wolfram alpha, the inverse of your function is this and is related to the Lambert function .
So your definition should be something like

``````TF1 * f= new TF1("f","[0]*ROOT::Math::lambert_W0([1]*x)",0,300)
``````

Best,
Stefano

Wow, thank you all!!

``````{
TF1 *f1 = new TF1("f1",
"TMath::Log(x / [a]) + [b] * x",
1., 10.);
f1->SetParameters(1., 0.001);
// https://www.wolframalpha.com/input?i=inverse+y%3Dlog%28x%2Fa%29%2Bb*x
TF1 *f2 = new TF1("f2",
"ROOT::Math::lambert_W0([a] * [b] * TMath::Exp(x)) / [b]",
f1->GetMinimum(), f1->GetMaximum());
f2->SetParameters(f1->GetParameters());
TCanvas *c = new TCanvas("c", "c");
c->Divide(1, 2);