# Dependent variable on fitting

**URL:** <https://root-forum.cern.ch/t/dependent-variable-on-fitting/33186>\
**Category:** Newbie\
**Tags:** root\
**Created:** [March 15, 2019, 7:18pm UTC](https://root-forum.cern.ch/t/dependent-variable-on-fitting/33186 "2019-03-15T19:18:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elena\_573](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/d26b3c/32.png) [@elena\_573](https://root-forum.cern.ch/u/elena_573)\
**Post date:** [March 15, 2019, 7:18pm UTC](https://root-forum.cern.ch/t/dependent-variable-on-fitting/33186/1 "2019-03-15T19:18:21Z")

</div>

Hi everyone!  
Well I want to create a fitting function which has a variable that is x and y dependent. I’m posting what I did, which is wrong, just to give you the idea. Variable is u. I was wondering if I could turn it to function through a loop or something else. The idea is that u=u(x,y) so it changes for each x,y value. I would like root to print out these u\_values and also to fit Tgrapherror with the running u.

```auto
{
   c1 = new TCanvas("c1","Gamma/Z Interferance/ Cross section vs Energy ",200,10,700,500);
   c1->SetFillColor(41);
   c1->SetGrid();
   c1->GetFrame()->SetFillColor(21);
   c1->GetFrame()->SetBorderSize(12);
   const Int_t n = 20;
   Double_t x[n] = {20, 30, 34.4, 40, 50, 60, 70, 80, 83, 85, 88, 90, 91, 93, 95, 97, 100, 105, 110, 120};
   Double_t y[n] = {256.8,120.6,93.42,70.54,46.92,34.46,28.91,36.58,50.69,73.88,206.9,752.2,1414,687.1,306.6,186.2,114.3,68.49,48.41,30.2};
   Double_t ex[n] = {.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0,.0};

   Double_t ey[n] = {0.0562,0.04337,0.03265,0.02115,0.01525,0.01294,0.01749,0.02653,0.03903,0.11,0.4007,
0.7527,0.3525,0.139,0.0867,0.06234,0.03734,0.02657,0.01483};

   gr = new TGraphErrors(n,x,y,ex,ey);
   gr->SetTitle("Cross Section (pb) vs Energy (GeV), #gamma*/Z Interference");
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(8);
   gr->GetXaxis()->SetTitle("#sqrt{s} (GeV)");
   gr->GetYaxis()->SetTitle("#sigma (pb)");
   gr->Draw("ALP");

   Double_t b_zero[x,y] = (48*TMath::Pi()*(x**2)*y)-0.0367;
   Double_t b_one[x] = ((x**2)*0.0158)/(((x**2-91.1876**2)**2+(91.1876*2.4952)**2)**(1/2));
   Double_t b_two[x] = ((x**2)*0.0003)/((x**2-91.1876**2)**2+(91.1876*2.4952)**2);
   Double_t u = (-b_two+(b_two**2+4*b_one*b_two)**(1/2))/(2*b_one);
   

   TF1 *f1 = new TF1("f1","([0]/x**2) + (([1]*(u**2)*(x**2))/((x**2 - [2]**2)**2 + ([2]*[3])**2)) + (([4]*u)/((x**2 - [2]**2)**2 + ([2]*[3])**2)**(1/2))", 20., 130.);
   
   f1->SetParameters(0.0002433744, 9.58775074077431E-06, 91.1876, 2.4952, 6.0180462856623E-07);
    gr->Fit("f1","R");
    gr->Fit("f1","R");
    gr->Fit("f1","R");
    gr->Fit("f1","R");
    gr->Fit("f1","R");
     
   gr->Draw();

   c1->Update();
   c1->GetFrame()->SetFillColor(19);
   c1->GetFrame()->SetBorderSize(12);
   c1->Modified();
    
   return c1;
}

```

---

<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:** [March 18, 2019, 3:53pm UTC](https://root-forum.cern.ch/t/dependent-variable-on-fitting/33186/2 "2019-03-18T15:53:40Z")

</div>

I guess @moneta can help you with this.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [March 18, 2019, 9:55pm UTC](https://root-forum.cern.ch/t/dependent-variable-on-fitting/33186/3 "2019-03-18T21:55:43Z")

</div>

Hello @elena_573,

first, the lines

> [@elena\_573](#):
>
> ```auto
> Double_t b_zero[x,y] = (48*TMath::Pi()*(x**2)*y)-0.0367;
> Double_t b_one[x] = ((x**2)*0.0158)/(((x**2-91.1876**2)**2+(91.1876*2.4952)**2)**(1/2));
> Double_t b_two[x] = ((x**2)*0.0003)/((x**2-91.1876**2)**2+(91.1876*2.4952)**2);
> Double_t u = (-b_two+(b_two**2+4*b_one*b_two)**(1/2))/(2*b_one);
> 
> ```

Will probably not work. Did you try putting this into a script and loading/executing it? If you do that, cling will tell you what’s not ok. You are trying to do a computation on an array, but this kind of syntax doesn’t work in C++.

Concerning your question of dependent variables:  
There certainly is a way to achieve this: substitution. Let’s say you have  
`x + y`  
and `y = 2x`  
after substituting you have:  
`x + 2x`

If you follow this through completely, you can compose a fit function that will work with TF1.

---

<div class="post-metadata">

**Author:** ![elena\_573](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/d26b3c/32.png) [@elena\_573](https://root-forum.cern.ch/u/elena_573)\
**Post date:** [March 18, 2019, 11:46pm UTC](https://root-forum.cern.ch/t/dependent-variable-on-fitting/33186/4 "2019-03-18T23:46:26Z")

</div>

Thank you very much!  
Yes, I put it in a script and executed it. I had also tried the substitution and it worked but I was wondering if there was an easier way so that I could avoid such a complex function.

Στις Τρί, 19 Μαρ 2019, 00:05 ο χρήστης Stephan Hageboeck [root.discourse@cern.ch](mailto:root.discourse@cern.ch) έγραψε:
