# Root SetParameters

**URL:** <https://root-forum.cern.ch/t/root-setparameters/52800>\
**Category:** Newbie\
**Created:** [December 16, 2022, 5:06pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800 "2022-12-16T17:06:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [December 16, 2022, 5:06pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/1 "2022-12-16T17:06:21Z")

</div>

If I try to fit a function to a given data, and if i already define the parameters of the function , after fitting I am getting different parameters ?? I did not understand this part.  
and also If I fit the function without setting parameters , after fitting I did not get the same parameters which I got earlier (not too much variation though).

```auto
void plot(){
    
    TGraph *gr = new TGraph("data.txt");
    
    TF1 *fn = new TF1("fn","[2]*x*x + [1]*x + [0]" , -10 , 10);
 fn->SetParameter(0,5);
    fn->SetParameter(1,-2);
    fn->SetParameter(2,4);
gr->Fit("fn");
gr->Draw("A*");
} 

```

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/5/3/533b5a51431e6c10f46a39afdbc8b6fa3690a623.png)

---

<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:** [December 16, 2022, 5:09pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/2 "2022-12-16T17:09:36Z")

</div>

> [@Subhu18](#):
>
> ` TGraph *gr = new TGraph("data.txt");`

Can you post `data.txt` ?

---

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [December 16, 2022, 5:11pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/3 "2022-12-16T17:11:32Z")

</div>

[data.txt](https://root-forum.cern.ch/uploads/short-url/1J6jVuHVGezKzZOQwpMFsGYVZhR.txt) (255 Bytes)

---

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [December 16, 2022, 5:16pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/4 "2022-12-16T17:16:46Z")

</div>

Also what it means to get new parameters for a fit, if we have already defined parameters?

---

<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:** [December 16, 2022, 5:38pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/5 "2022-12-16T17:38:29Z")

</div>

I get this:

```auto
Processing plot.C...

****************************************
Minimizer is Minuit / Migrad
Chi2 = 0.000397134
NDf = 19
Edm = 3.36476e-22
NCalls = 67
p0 = 0.0266482 +/- 0.0253677   
p1 = 0.595907 +/- 0.259264    
p2 = 5.39565 +/- 0.644357    

```

 ![Screenshot 2022-12-16 at 18.37.10](https://root-forum.cern.ch/uploads/default/original/3X/f/3/f36419d44938bff8563f7e0466fa605b9ad1d4e5.jpeg)

It seems ok seems to me. Minuit adjusted the parameters to fit the dtata points.

---

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [December 16, 2022, 5:53pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/6 "2022-12-16T17:53:19Z")

</div>

yes but ain’t these parameters the coefficients of x^2 , x, constant?? which we have already set in our code. so these parameters should be same as our our set parameters.  
also if you fit the same thing without setting parameters in the code, we get different parameters.

---

<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:** [December 16, 2022, 6:02pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/7 "2022-12-16T18:02:42Z")

</div>

Your initial parameters are:

```auto
root [0] TF1 *fn2 = new TF1("fn2","4*x*x -2*x + 5" , -10 , 10);

```

For instance, at 0.2 you get:

```auto
root [1] fn2->Eval(0.2)
(double) 4.7600000

```

As you see that does not correspond to your plot. Minuit adjusted the parameter so they fit the data … that is the idea of the fitting.

---

<div class="post-metadata">

**Author:** ![omartarif](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/omartarif/32/31881_2.png) [@omartarif](https://root-forum.cern.ch/u/omartarif)\
**Post date:** [December 17, 2022, 1:21pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/8 "2022-12-17T13:21:29Z")

</div>

I would like to add that by setting the parameters you do not hold them fixed you just initialize them for that point for your fit.

---

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [December 20, 2022, 2:15pm UTC](https://root-forum.cern.ch/t/root-setparameters/52800/9 "2022-12-20T14:15:35Z")

</div>

ahh now I get it , thanks.
