# Fit function is not right?

**URL:** <https://root-forum.cern.ch/t/fit-function-is-not-right/41921>\
**Category:** Newbie\
**Created:** [October 20, 2020, 4:08am UTC](https://root-forum.cern.ch/t/fit-function-is-not-right/41921 "2020-10-20T04:08:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [October 20, 2020, 4:08am UTC](https://root-forum.cern.ch/t/fit-function-is-not-right/41921/1 "2020-10-20T04:08:27Z")

</div>

I have a data set. I tried to fit with Root, but it was not successful. However, when I process this data with Excel, there is not any problems. Could you help me, please?  
Here is my code

```auto
#include <TF1.h>
#include <TGraph.h>
#include <TMarker.h>
#include <TAxis.h>

void heso()
{
    TGraph *g0 = new TGraph("factor.txt");
    g0->SetLineColor(kRed);
    g0->SetMarkerStyle(8);
    g0->SetMarkerColor(kRed);
    g0->SetMarkerSize(1);
    g0->Draw("ACP");
    g0->GetYaxis()->SetRangeUser(0, 300000);

    TF1 *fit = new TF1("fit", "[0]*(x^[1])", 5, 30);
    fit->SetLineColor(kBlue);
    g0->Fit("fit");

}

```

[factor.txt](https://root-forum.cern.ch/uploads/short-url/Y0vtN0FkcJ5SADWi1d51f6Uwag.txt) (616 Bytes)

 ![root](https://root-forum.cern.ch/uploads/default/original/3X/7/a/7ad1ddcae2f396fb659cb90edcf4ebf589115007.png)  
 ![root2](https://root-forum.cern.ch/uploads/default/original/3X/6/c/6c11265bf27e38b9aacec9053cfbc6e32fab37fb.png)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 20, 2020, 6:35am UTC](https://root-forum.cern.ch/t/fit-function-is-not-right/41921/2 "2020-10-20T06:35:02Z")

</div>

> [@hammiu](#):
>
> ```auto
> TF1 *fit = new TF1("fit", "[0]*(x^[1])", 5, 30);
> 
> ```

Try with:

```auto
TF1 *fit = new TF1("fit", "[0]+(x^[1])", 5, 30);

```

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [October 20, 2020, 6:44am UTC](https://root-forum.cern.ch/t/fit-function-is-not-right/41921/3 "2020-10-20T06:44:58Z")

</div>

Thank you. It is a good different choice. But I want to fit the data set by function `[0]*(x^[1])`  
Here is a result obtained by Excel and the fit function is good for the data set.

 ![Screenshot_1](https://root-forum.cern.ch/uploads/default/original/3X/a/4/a4500b13e51f94989e78d562802a9876dd6a6b4c.png)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 20, 2020, 7:00am UTC](https://root-forum.cern.ch/t/fit-function-is-not-right/41921/4 "2020-10-20T07:00:33Z")

</div>

Then maybe @moneta can help you

---

<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 20, 2020, 7:32am UTC](https://root-forum.cern.ch/t/fit-function-is-not-right/41921/5 "2020-10-20T07:32:58Z")

</div>

[`fit->SetParameters(13., 3.) /* good initial values */; g0->Fit(fit);`](https://root-forum.cern.ch/search?q=%22Setting%20initial%20conditions%22%20order%3Alatest)

---

<div class="post-metadata">

**Author:** ![hammiu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hammiu/32/20395_2.png) [@hammiu](https://root-forum.cern.ch/u/hammiu)\
**Post date:** [October 20, 2020, 7:47am UTC](https://root-forum.cern.ch/t/fit-function-is-not-right/41921/6 "2020-10-20T07:47:32Z")

</div>

Thanks a lot. It works fine. Could you explain what the reason here is?
