# Error Propagation on a Fit

**URL:** <https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644>\
**Category:** Newbie\
**Created:** [June 17, 2019, 10:21am UTC](https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644 "2019-06-17T10:21:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alessandro\_Breghi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ebca7d/32.png) [@Alessandro\_Breghi](https://root-forum.cern.ch/u/Alessandro_Breghi)\
**Post date:** [June 17, 2019, 10:21am UTC](https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644/1 "2019-06-17T10:21:14Z")

</div>

Hi everyone,  
I’m Doing An Experiment to calculate the elastic constant of a spring.  
Now I’ve used TGraphErros and fitted data with the formula:  
T=2\*TMath::Pi()\*TMath::Sqrt((x+( **0.0507** /3))/[k]  
creating a TF1 class function.  
I would like to set an error on the value 0.507 that is the mass of the spring, so I Can have the correct error on K.  
I’ve Tried to set a fix Parameter but then I can’t give it an error.  
I’ve looked for some answer in the forum but I didn’t understand them because I’ve started using root recently and I haven’t a great knowledge of programming language  
Is there a “simple way” to do it  
Thanks everyone

---

<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:** [June 17, 2019, 6:29pm UTC](https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644/2 "2019-06-17T18:29:06Z")

</div>

I guess you could try to fit the “mass of the spring” using `T=2.*TMath::Pi()*TMath::Sqrt((x+([m]/3.))/[k])` (setting the initial value `your_fit_function->SetParameter("m", 0.0507);`);

---

<div class="post-metadata">

**Author:** ![Alessandro\_Breghi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ebca7d/32.png) [@Alessandro\_Breghi](https://root-forum.cern.ch/u/Alessandro_Breghi)\
**Post date:** [June 17, 2019, 8:27pm UTC](https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644/3 "2019-06-17T20:27:49Z")

</div>

Wile\_E\_Coyote Thanks for the answer.  
I’ve done it but the problem is that root does no get any value of k.  
I show you the macro

```auto
TGraphErrors *g=new TGraphErrors("$ROOTSYS/macros/Hook1.txt");
g->SetTitle("Metodo Dinamico");
g->GetXaxis()->SetTitle("m(Kg)");
g->GetYaxis()->SetTitle("T(s)");
g->SetMarkerStyle(10);
g->SetMarkerColor(kGreen-8);
g->SetMarkerSize(1);
TF1 *f=new TF1("f","2*TMath::Pi()*TMath::Sqrt((x+([M]/3.))/[k])",0.,0.05);
f->SetParameter(0,0.507);
f->SetParError(0,0.001);
g->Fit("f");
gStyle->SetOptFit(1);
g->GetFunction("f")->SetLineColor(kMagenta);
g->GetFunction("f")->SetLineWidth(2);
g->Draw("AP");

```

And the result is this

 ![problem](https://root-forum.cern.ch/uploads/default/original/3X/9/1/9154be6833cc4cb34b71220db7fd5d45ac0e3f61.png)

I’ve also tried to give a value k as close as possible to expected but then root changes also the value of M.  
What i would like to do is to is to keep M constant, associating it an error, and get the final correct error valutation on k

---

<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:** [June 17, 2019, 8:55pm UTC](https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644/4 "2019-06-17T20:55:30Z")

</div>

Try:

```cpp
TF1 *f = new TF1("f", "TMath::TwoPi()*TMath::Sqrt((x+([M]/3.))/[k])", 0., 0.1);
f->SetParameter("M", 0.0507);
f->SetParameter("k", 1.);
g->Fit(f, "WQN"); // "initial pre-fit"
g->Fit(f, ""); // "final fit"

```

---

<div class="post-metadata">

**Author:** ![Alessandro\_Breghi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ebca7d/32.png) [@Alessandro\_Breghi](https://root-forum.cern.ch/u/Alessandro_Breghi)\
**Post date:** [June 17, 2019, 9:22pm UTC](https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644/5 "2019-06-17T21:22:33Z")

</div>

Thanks Wile now the result is this

 ![2](https://root-forum.cern.ch/uploads/default/original/3X/5/c/5c27a3e94935f903cab2483221fd0568bc9a4cf5.png)  
The problem is I don’t want root to fit the parameter M but only the parameter k

---

<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:** [June 17, 2019, 9:25pm UTC](https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644/6 "2019-06-17T21:25:57Z")

</div>

```cpp
f->FixParameter(f->GetParNumber("M"), 0.0507);

```

---

<div class="post-metadata">

**Author:** ![Alessandro\_Breghi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ebca7d/32.png) [@Alessandro\_Breghi](https://root-forum.cern.ch/u/Alessandro_Breghi)\
**Post date:** [June 17, 2019, 9:31pm UTC](https://root-forum.cern.ch/t/error-propagation-on-a-fit/34644/7 "2019-06-17T21:31:21Z")

</div>

Ok Wile Thanks Very Much Now It works!  
Last Question:  
Is there a way to associate M an error? because doing like that the error is 0-
