Error to fit a sin curve

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 (14.9 KB)

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:

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

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