# Error to fit a sin curve

**URL:** <https://root-forum.cern.ch/t/error-to-fit-a-sin-curve/41890>\
**Category:** Newbie\
**Created:** [October 17, 2020, 2:07pm UTC](https://root-forum.cern.ch/t/error-to-fit-a-sin-curve/41890 "2020-10-17T14:07:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsubhro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dsubhro/32/24279_2.png) [@dsubhro](https://root-forum.cern.ch/u/dsubhro)\
**Post date:** [October 17, 2020, 2:07pm UTC](https://root-forum.cern.ch/t/error-to-fit-a-sin-curve/41890/1 "2020-10-17T14:07:39Z")

</div>

ROOT Version:6.22  
\_Platform:Ubuntu

* * *

I tried to fit a data which follows sine wave to a sin function(between -10 to 10). The fitted curve is a straight line.  
Here is the code and the output curve is on c1.pdf  
{  
double x[]={-10, -9.33333, -8.66667, -8, -7.33333, -6.66667, -6, -5.33333, -4.66667, -4, -3.33333, -2.66667, -2, -1.33333, -0.666667, -1.0842e-19, 0.666667, 1.33333, 2, 2.66667, 3.33333, 4, 4.66667, 5.33333, 6, 6.66667, 7.33333, 8, 8.66667, 9.33333, 10};  
double y[] = {0.544021, -0.0913172, -0.687551, -0.989358, -0.867497, -0.374151, 0.279415, 0.813329, 0.998955, 0.756802, 0.190568, -0.457273, -0.909297, -0.971938, -0.61837, -1.0842e-19, 0.61837, 0.971938, 0.909297, 0.457273, -0.190568, -0.756802, -0.998955, -0.813329, -0.279415, 0.374151, 0.867497, 0.989358, 0.687551, 0.0913172, -0.544021};  
f1 = new TGraph(30, x, y);  
f1 -\> SetMarkerStyle(3);  
f2 = new TF1(“sine”, “[0]\*sin([1]\*x)”,-10,10);  
f1 -\> Fit(f2);  
f1-\>Draw(“AP”);  
}  
The output fitted curve  
[c1.pdf](https://root-forum.cern.ch/uploads/short-url/jaaCp6sf0gSoKcP7qXWA4WJMhem.pdf) (14.9 KB)

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 17, 2020, 5:56pm UTC](https://root-forum.cern.ch/t/error-to-fit-a-sin-curve/41890/2 "2020-10-17T17:56:18Z")

</div>

Hi,

the problem is that `[0]*sin([1]*x)` is not a default fitting function, so you should preset `[0]` and `[1]` with some initial values, e.g., `1` and `10`, respectively:

```auto
        ...
        f2 = new TF1("sine", "[0]*sin([1]*x)",-10,10);
        f2->SetParameters(1, 10);
        f2->SetLineColor(kRed);
        f1 -> Fit(f2);
        ...

```

---

<div class="post-metadata">

**Author:** ![dsubhro](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dsubhro/32/24279_2.png) [@dsubhro](https://root-forum.cern.ch/u/dsubhro)\
**Post date:** [October 17, 2020, 9:44pm UTC](https://root-forum.cern.ch/t/error-to-fit-a-sin-curve/41890/3 "2020-10-17T21:44:45Z")

</div>

Hi,  
Thank you so much… It worked. You showed me how to set the parameters for fitting a curve.  
Regards
