# Fit TGraphErrors with an Inverse function

**URL:** <https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803>\
**Category:** Newbie\
**Created:** [October 3, 2022, 5:16pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803 "2022-10-03T17:16:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![AirBlock](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@AirBlock](https://root-forum.cern.ch/u/AirBlock)\
**Post date:** [October 3, 2022, 5:16pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/1 "2022-10-03T17:16:01Z")

</div>

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:

```auto
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?  
Thanks in advance

_ROOT Version:_ 5.23/36  
_Platform:_ Windows 10  
_Compiler:_ CINT/ROOT C/C++ Intepreter version 5.18.00

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 4, 2022, 5:53am UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/2 "2022-10-04T05:53:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AirBlock](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@AirBlock](https://root-forum.cern.ch/u/AirBlock)\
**Post date:** [October 4, 2022, 2:07pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/3 "2022-10-04T14:07:57Z")

</div>

> [@couet](#):
>
> You mean that you would like to fit a new graph with `x1` and `y1` swapped using the same function `f` ?

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.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 4, 2022, 2:21pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/4 "2022-10-04T14:21:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AirBlock](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@AirBlock](https://root-forum.cern.ch/u/AirBlock)\
**Post date:** [October 4, 2022, 4:25pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/5 "2022-10-04T16:25:10Z")

</div>

> [@couet](#):
>
> You can try. You need to define f^{-1}.

Ok, but how? I have no clue…

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 5, 2022, 8:23am UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/6 "2022-10-05T08:23:12Z")

</div>

may be [this](https://www.wolframalpha.com/input?i=inverse+function+of+log%28a*x%29) can help ?

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [October 5, 2022, 11:26am UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/7 "2022-10-05T11:26:29Z")

</div>

According to Wolfram alpha, the inverse of your function is [this](https://www.wolframalpha.com/input?i=inverse+f%28x%29%3D+log%28x%2Fa%29+%2B+bx) and is related to the [Lambert function](https://root.cern/doc/master/group__SpecFunc.html#ga88f87570bf22c200de2b10efc463f293).  
So your definition should be something like

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

```

Best,  
Stefano

---

<div class="post-metadata">

**Author:** ![AirBlock](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@AirBlock](https://root-forum.cern.ch/u/AirBlock)\
**Post date:** [October 5, 2022, 3:45pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/8 "2022-10-05T15:45:03Z")

</div>

Wow, thank you all!! 😃

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 5, 2022, 5:41pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/9 "2022-10-05T17:41:52Z")

</div>

```cpp
{
  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);
  c->cd(1); f1->Draw(); gPad->SetGrid(1, 1);
  c->cd(2); f2->Draw(); gPad->SetGrid(1, 1);
  c->cd(0);
}

```

---

<div class="post-metadata">

**Author:** ![AirBlock](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@AirBlock](https://root-forum.cern.ch/u/AirBlock)\
**Post date:** [October 5, 2022, 6:01pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/10 "2022-10-05T18:01:28Z")

</div>

I tryed with the W function, but I think it is defined only in newer Root versions.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/e/8e5bf7b9e20a113319fa1b0bf3a5fee625bac408.png)

After the update I will let you know if it work or not.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 5, 2022, 6:25pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/11 "2022-10-05T18:25:39Z")

</div>

I guess you need [ROOT 6.26/06](https://root.cern.ch/releases/release-62606/) (the “[latest release](https://root.cern/install/all_releases/latest/)” as of today) or some newer version.

---

<div class="post-metadata">

**Author:** ![AirBlock](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@AirBlock](https://root-forum.cern.ch/u/AirBlock)\
**Post date:** [October 5, 2022, 7:07pm UTC](https://root-forum.cern.ch/t/fit-tgrapherrors-with-an-inverse-function/51803/12 "2022-10-05T19:07:42Z")

</div>

ok thanks
