4
$\begingroup$

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

enter image description here

Thanks for help

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

2 Answers 2

4
$\begingroup$

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"}]}] 

enter image description here

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}] 
$\endgroup$
0
1
$\begingroup$

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"}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.