0
$\begingroup$

this is the data that i use to plot. data_damped_osccilation

this is my code so far, however it still give me error

datadamped = Import["Datadamped.txt", "Data"]
qqq = ListPlot[datadamped]
i[c_, d_, e_, t_, h_] := c *Exp[(-d*e*t) Sin[(Sqrt[1 - d^2]) e*t + h]]
datadx = NonlinearModelFit[datadamped, i[c, d, t, e, h], {c, d, e, h},
   t]
Show[ListPlot[datadamped], Plot[datadx[t], {t, -2 Pi, 2 Pi}]]
NonLdataFit["BestFitParameters"];

enter image description here

How to find maximum and minimum? secondly how can i find first minimum and the vaues of t where this occur? thirdly how to know the exact coordinates of the second maximum at t=1.0 ?

all help is appreciated. thank you.

$\endgroup$
  • $\begingroup$ Your fitting model does not seem to reproduce the behavior of your data at least for some values of the parameters. Consider for instance: Plot[i[1, 0.01, 6, t, 1], {t, 0, 5}, PlotRange -> All], which returns this: image. $\endgroup$ – MarcoB Apr 5 '16 at 16:15
  • $\begingroup$ Do you really need a fit or you just need the maximums/minimums? $\endgroup$ – BlacKow Apr 5 '16 at 16:32
4
$\begingroup$
datadamped = 
  Import["http://comsics.usm.my/tlyoon/teaching/ZCE111_1516SEM2/data/\
data_A6Q2.dat", "Data"];

max = datadamped[[#]] &@FindPeaks[datadamped[[All, 2]], 0][[All, 1]]
min = datadamped[[#]] &@FindPeaks[-datadamped[[All, 2]], 0][[All, 1]]

This gives you list of maximums and minimums. If you need a fit you probably want to simplify the fitting space and optimize for different fit parameters:

g = NonlinearModelFit[datadamped, 
  A Exp[-b x] Sin[c x + h], {A, b, c, h}, x]
Show[ListPlot[{datadamped, max, min}, 
  PlotStyle -> {Blue, Red, Green}], 
 Plot[g[x], {x, 0, 5}, PlotStyle -> {{Dashed, Orange}}]]

enter image description here

Your data doesn't look real, since the fit was too good to be true :) For real data you probably want to play with parameters for FindPeaks. If you trust your fit better than your data, you can find peaks from fitted function now:

minfit = {x, g[x]} /. 
  Solve[g'[x] == 0 && g''[x] > 0 && x > 0 && x < 5, x]
maxfit = {x, g[x]} /. 
  Solve[g'[x] == 0 && g''[x] < 0 && x > 0 && x < 5, x]

And finally you can use FindMinimum if you really want to use this function:

FindMinimum[g[x], {x, 0}]

And it gives the same answer as minfit[[1]]

Now you probably want to find out your original parameters set $d,e$

params = g["BestFitParameters"]
Solve[-d e == b && Sqrt[1 - d^2] e == c /. params, {d, e}]

{A -> -0.5, b -> 0.628319, c -> -6.25169, h -> 4.71239}

{{d -> 0.1, e -> -6.28319}}

And $e$ really looks suspiciously close to $2\pi$

| improve this answer | |
$\endgroup$
  • $\begingroup$ FindPeaks is a useful command. thank you. @Blackow $\endgroup$ – Nabil Apr 6 '16 at 3:02
  • 1
    $\begingroup$ @Nabil FindPeaks is useful, but don't use it blindly. When you have real data, you almost always need to deal with noise. FindPeaks can add smoothing to your data, see more here $\endgroup$ – BlacKow Apr 6 '16 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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