# How fit my data to sinus?

I have problem with fit sinus to my date, because this sin is very tiny. This is my code:

DANE = ReadList[
"C:\\Users\\student\\Desktop\\jd100\\projekt\\wynik.txt", Real];
k = Length[DANE]/2;
xx = DANE[[1 ;; k]];
yy = DANE[[k + 1 ;; 2 k]];

dane2 = Transpose[{xx, yy}];

fit2 = FindFit[dane2, a0 + a1* Sin[a2* x + a3], {a0, a1, a2, a3}, x];

Show[{ListPlot[dane2 , PlotRange -> {{-500, 500}, {-500, 500}}],
Plot[fit, {x, -500, 500},
PlotLabel -> "Automatyczne dopasowanie wielomianu",
AxesLabel -> {"x", "y"}]}];
Show[{ListPlot[dane2 , PlotRange -> {{0, 1000}, {-100, 100}}],
Plot[a0 + a1 Sin[a2 *x + a3] /. fit2, {x, -1000, 1000},
PlotLabel -> "Automatyczne dopasowanie wielomianu",
AxesLabel -> {"x", "y"}]}]
Show [ ListPlot[dane2 , PlotRange -> {{0, 1000}, {-100, 100}}]]


And this is how this sinus looks like

Thanks for help

• Please have a look at NonlinearModelFit. Dec 4, 2023 at 11:26
• @b.gates.you.know.what i am starting learning mathematica and do you want from to change Fit to NonlinearFit? Dec 4, 2023 at 11:29
• @UlrichNeumann docs.google.com/document/d/… it is that Dec 4, 2023 at 11:34
• @UlrichNeumann is it possible do it btw? Beacouse i am still trying and nothing :( Dec 4, 2023 at 11:53

Assuming your data dane2 is known:

With Method->"NMinimize" NonlinearModelFit works fine

fit  = NonlinearModelFit[dane2,a0 + a1*Sin[a2*x + a3], {a0, a1, a2, a3},x,Method -> "NMinimize"]


modified axes ranges

Show[{ListPlot[dane2, PlotRange -> {{0, 5000}, {-60, 60}}],
Plot[fit[x], {x, 0, 5000}, PlotStyle -> Red, PlotLabel -> "Automatyczne dopasowanie wielomianu", AxesLabel -> {"x", "y"}]}]


Findfit with modified Method works too:

fit1 = FindFit[dane2, a0 + a1*Sin[a2*x + a3], {a0, a1, a2, a3}, x,
Method -> "NMinimize"]
Plot[a0 + a1*Sin[a2*x + a3] /. fit1, {x, 0, 5000}]


Try this:

dane2 = Table[{x, 50 Sin[x/100] + RandomReal[{-4, 4}]}, {x, 0, 1000,
20}];(*Generowanie danych -> dane2*)

fit2 = FindFit[dane2, a0 + a1*Sin[a2*x + a3], {a0, a1, a2, a3}, x, Method -> NMinimize]

Show[{ListPlot[dane2, PlotStyle -> PointSize[0.015]],
Plot[Evaluate[a0 + a1 Sin[a2*x + a3] /. fit2], {x, 0, 1000},
PlotStyle -> Black]},
PlotLabel -> "Automatyczne dopasowanie wielomianu",
AxesLabel -> {"x", "y"}]