0
$\begingroup$

I have a set of data:

data1={0.438947, 0.421796, 0.257979, -0.0113562, -0.304228, -0.434637, -0.244146, 0.103227, 0.476592, 0.688494, 0.544246, 0.148493, 0.172431, 0.0354982, -0.331193, -0.344282, -0.00962893, 0.373348, 0.679059, 0.58302, 0.184679, -0.236453, 0.166275, 0.175942, 0.0251669, 0.100451, 0.511637, 0.637384, 0.430186, -0.0118105, -0.292176, -0.184197, 0.199443, 0.150381, 0.16512, 0.001854, -0.260791, 0.0622488, 0.425933, 0.599488, 0.47702, 0.0970701, -0.209052, -0.220244, 0.173392, 0.134602, 0.240986, 0.338698, 0.0220978, -0.224613, -0.109723, 0.176388, 0.454605, 0.557959, 0.302164, 0.17482, 0.19531, 0.40853, 0.214071, 0.00811249, -0.0842581, -0.00985491, 0.200964, 0.430605, 0.440398, 0.243201, 0.167068, 0.148824, 0.160405, 0.290912, 0.350251, 0.278384, 0.116044, 0.0321556, 0.0705789, 0.174297, 0.28836, 0.194055, 0.162784, 0.199375, 0.202781, 0.13725, 0.142673, 0.14058, 0.174253, 0.193957, 0.20005, 0.151823, 0.159523, 0.171071, 0.129125, 0.128131, 0.153288, 0.17511, 0.231478, 0.228416, 0.203896, 0.169086, 0.0966604, 0.158022}

which has decaying sinusoidal oscillations within an envelope. If I try to fit this using the NonLinearModelFit command:

l = Table[l, {l, 0, 99}];
Data1 = MapThread[List, {l, data1}];

model=NonlinearModelFit[Data1, s Exp[-a t] (Sin[b t + c]) + d, {{s, 0.4}, {a, 0}, {b, 0.7}, {c, 2}, {d, 0.15}}, t] 
Show[ListLinePlot[Data1, PlotRange -> All, Frame -> True, PlotStyle -> {Thick, Purple}, BaseStyle -> {FontFamily -> "Calisto MT", 20, Bold}], Plot[model[t], {t, 0, 99}, PlotLabel -> Framed[model[t]], Frame -> True, PlotRange -> All, PlotStyle -> {Thick, Black, Dashed}, BaseStyle -> {FontFamily -> "Calisto MT", 20, Bold}]]

I get the graph in the Figure below. enter image description here

where the purple line is the data and the black dashed line is the fit. This is clearly not a very good fit to the data. Is there a more accurate way of fitting data like this?

$\endgroup$
6
  • 1
    $\begingroup$ NonLinearModelFit is a fine tool, but you won't get a good fit unless your function can be made to match the data. I don't see an exponentially decaying sinusoid here, and neither does NonLinearModelFit. Perhaps a more realistic physical model is in order. $\endgroup$
    – John Doty
    Commented May 22, 2018 at 14:05
  • $\begingroup$ To echo @JohnDoty 's comment: Do you need just to "describe" the data or approximate the process that generates the data? If the former, then you'll likely need some nonparametric regression technique (kernel regression, gams, loess, etc.). If the latter, then you'll need to describe the process that generates the data which would account for the varying amplitudes and frequencies. The point is that it takes more than just having data. $\endgroup$
    – JimB
    Commented May 22, 2018 at 14:13
  • $\begingroup$ @AntonAntonov. I think I fixed the code by adding in model = NonlinearModelFit.... $\endgroup$
    – JimB
    Commented May 22, 2018 at 14:17
  • 1
    $\begingroup$ @JimB, yes, I figured model is missing. I sort of wanted JJH to fix it... $\endgroup$ Commented May 22, 2018 at 14:45
  • $\begingroup$ @JimB, the latter is what I'm hoping to model. I'm currently trying a model where I split the data into small sections and fit then individually. $\endgroup$
    – JJH
    Commented May 23, 2018 at 8:04

1 Answer 1

5
$\begingroup$

It looks like there may be a problem with your sampling frequency. For example, consider this sampling of a sinusoid, producing the data in purple.

If you can increase your sampling frequency your data might be better to fit.

tab = Table[{x, Cos[x]}, {x, 0, 22 Pi, 0.01}];

Show[lp = ListLinePlot[First /@ Partition[tab, 306],
   PlotStyle -> {Thick, Purple}],
 Plot[Cos[x], {x, 0, 22 Pi}]]

enter image description here

Nevertheless, poor sampling does not stop NonlinearModelFit.

nlm = NonlinearModelFit[tab, {a Exp[-b t] Cos[c t + d]}, {a, b, c, d}, t];

Show[lp, Plot[nlm[t], {t, 0, 22 Pi}]]

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.