Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How do I fit damped harmonic motion data (x vs. t) using Mathematica?

Tags:
  1. Oct 19, 2010 #1
    How do I fit the data given below into the standard form (ignore the part saying to use MathCad):

    [PLAIN]http://screensnapr.com/u/apmqkd.png [Broken]

    Code (Text):
    {{{0.002, 0.726}, {0.022, 0.739}, {0.042, 0.75}, {0.062,
       0.759}, {0.082, 0.768}, {0.102, 0.776}, {0.121, 0.785}, {0.141,
       0.794}, {0.161, 0.802}, {0.181, 0.81}, {0.201, 0.818}, {0.221,
       0.826}, {0.24, 0.834}, {0.26, 0.84}, {0.28, 0.847}, {0.3,
       0.854}, {0.32, 0.859}, {0.339, 0.864}, {0.359, 0.869}, {0.379,
       0.873}, {0.399, 0.877}, {0.419, 0.879}, {0.439, 0.882}, {0.458,
       0.883}, {0.478, 0.884}, {0.498, 0.885}, {0.518, 0.884}, {0.538,
       0.883}, {0.557, 0.882}, {0.577, 0.88}, {0.597, 0.877}, {0.617,
       0.874}, {0.636, 0.87}, {0.656, 0.865}, {0.676, 0.86}, {0.696,
       0.855}, {0.716, 0.849}, {0.735, 0.843}, {0.755, 0.836}, {0.775,
       0.829}, {0.795, 0.822}, {0.815, 0.814}, {0.834, 0.806}, {0.854,
       0.799}, {0.874, 0.791}, {0.894, 0.783}, {0.913, 0.774}, {0.933,
       0.766}, {0.953, 0.759}, {0.973, 0.751}, {0.993, 0.743}, {1.012,
       0.736}, {1.032, 0.729}, {1.052, 0.722}, {1.072, 0.716}, {1.091,
       0.71}, {1.111, 0.704}, {1.131, 0.699}, {1.151, 0.694}, {1.171,
       0.689}, {1.19, 0.685}, {1.21, 0.683}, {1.23, 0.68}, {1.25,
       0.675}, {1.27, 0.676}, {1.289, 0.675}, {1.309, 0.673}, {1.329,
       0.676}, {1.349, 0.674}, {1.369, 0.675}, {1.388, 0.68}, {1.408,
       0.68}, {1.428, 0.683}, {1.448, 0.689}, {1.468, 0.693}, {1.487,
       0.698}, {1.507, 0.703}, {1.527, 0.708}, {1.547, 0.714}, {1.567,
       0.719}, {1.586, 0.725}, {1.606, 0.732}, {1.626, 0.739}, {1.646,
       0.746}, {1.666, 0.753}, {1.686, 0.76}, {1.705, 0.768}, {1.725,
       0.775}, {1.745, 0.782}, {1.765, 0.789}, {1.785, 0.796}, {1.804,
       0.803}, {1.824, 0.81}, {1.844, 0.816}, {1.864, 0.822}, {1.884,
       0.828}, {1.904, 0.834}, {1.923, 0.839}, {1.943, 0.843}, {1.963,
       0.848}, {1.983, 0.852}, {2.003, 0.855}, {2.023, 0.858}, {2.042,
       0.861}, {2.062, 0.863}, {2.082, 0.864}, {2.102, 0.865}, {2.121,
       0.865}, {2.141, 0.865}, {2.161, 0.865}, {2.181, 0.863}, {2.201,
       0.861}, {2.221, 0.859}, {2.24, 0.857}, {2.26, 0.854}, {2.28,
       0.85}, {2.3, 0.846}, {2.319, 0.841}, {2.339, 0.837}, {2.359,
       0.832}, {2.379, 0.826}, {2.399, 0.82}, {2.418, 0.815}, {2.438,
       0.808}, {2.458, 0.802}, {2.478, 0.795}, {2.498, 0.789}, {2.517,
       0.782}, {2.537, 0.776}, {2.557, 0.769}, {2.577, 0.762}, {2.596,
       0.756}, {2.616, 0.75}, {2.636, 0.744}, {2.656, 0.738}, {2.675,
       0.731}, {2.695, 0.727}, {2.715, 0.722}, {2.735, 0.717}, {2.755,
       0.712}, {2.774, 0.708}, {2.794, 0.705}, {2.814, 0.702}, {2.834,
       0.699}, {2.854, 0.697}, {2.873, 0.695}, {2.893, 0.694}, {2.913,
       0.693}, {2.933, 0.693}, {2.953, 0.693}, {2.972, 0.694}, {2.992,
       0.695}, {3.012, 0.696}, {3.032, 0.698}, {3.052, 0.701}, {3.071,
       0.704}, {3.091, 0.707}, {3.111, 0.71}, {3.131, 0.712}, {3.151,
       0.718}, {3.17, 0.724}, {3.19, 0.728}, {3.21, 0.733}, {3.23,
       0.739}, {3.25, 0.744}, {3.27, 0.75}, {3.289, 0.756}, {3.309,
       0.762}, {3.329, 0.768}, {3.349, 0.774}, {3.369, 0.78}, {3.388,
       0.786}, {3.408, 0.792}, {3.428, 0.797}, {3.448, 0.803}, {3.468,
       0.808}, {3.488, 0.813}, {3.507, 0.818}, {3.527, 0.822}, {3.547,
       0.827}, {3.567, 0.831}, {3.587, 0.835}, {3.606, 0.838}, {3.626,
       0.841}, {3.646, 0.844}, {3.666, 0.846}, {3.686, 0.847}, {3.705,
       0.848}, {3.725, 0.849}, {3.745, 0.85}, {3.765, 0.85}, {3.785,
       0.849}, {3.804, 0.848}, {3.824, 0.847}, {3.844, 0.845}, {3.864,
       0.843}, {3.884, 0.84}, {3.903, 0.837}, {3.923, 0.834}, {3.943,
       0.831}, {3.963, 0.827}, {3.983, 0.823}, {4.002, 0.818}, {4.022,
       0.814}, {4.042, 0.809}, {4.062, 0.804}, {4.081, 0.798}, {4.101,
       0.793}, {4.121, 0.788}, {4.141, 0.782}, {4.161, 0.777}, {4.18,
       0.771}, {4.2, 0.766}, {4.22, 0.761}, {4.24, 0.755}, {4.26,
       0.75}, {4.279, 0.745}, {4.299, 0.741}, {4.319, 0.736}, {4.339,
       0.732}, {4.358, 0.728}, {4.378, 0.724}, {4.398, 0.721}, {4.418,
       0.718}, {4.438, 0.716}, {4.457, 0.713}, {4.477, 0.711}, {4.497,
       0.71}, {4.517, 0.708}, {4.537, 0.707}, {4.556, 0.706}, {4.576,
       0.705}, {4.596, 0.704}, {4.616, 0.705}, {4.636, 0.707}, {4.655,
       0.708}, {4.675, 0.712}, {4.695, 0.712}, {4.715, 0.715}, {4.735,
       0.72}, {4.754, 0.723}, {4.774, 0.727}, {4.794, 0.731}, {4.814,
       0.735}, {4.834, 0.739}, {4.854, 0.744}, {4.873, 0.749}, {4.893,
       0.753}, {4.913, 0.758}, {4.933, 0.763}, {4.953, 0.768}, {4.972,
       0.773}, {4.992, 0.778}, {5.012, 0.783}}}
    Graph display of above data:
    [PLAIN]http://screensnapr.com/u/9k82rt.png [Broken]

    My attempt in Mathematica:
    Code (Text):
    FindFit[data,
     A*E^(-(R/(2 m))*t)*Cos[Sqrt[k/m - (R/(2 m))^2*t]] + B + c*t, {A, R,
      m, k, B, c}, t]
    I get this error:
    "FindFit::fitd: First argument {{{0.002,0.726},{0.022,0.739},{0.042,0.75},<<5>>,{0.161,0.802},{0.181,0.81},<<244>>}} in FindFit is not a list or a rectangular array. >>"



    Can this be done using Mathematica?

    Thank you!
     
    Last edited by a moderator: May 5, 2017
  2. jcsd
  3. Oct 19, 2010 #2
    First, your error message: Notice you have an extra layer of {} so you do not have {{x1,y1},{x2,y2}...} but {{{x1,y1},{x2,y2}...}. Get rid of that extra layer and you will make the error go away. One way of doing this is to substitute data[[1]] for data in your Mathematica code, that is what I did. Even with that fix I couldn't get your code to converge.

    Now on to bigger things. I don't believe FindFit allows you to specify initial values. If you can find a way to do that then maybe that is your solution. You can include constraints along with your equation in FindFit and you could try to use that to force each of the parameters into a range, but I'm not convinced.

    FindMinimum allows you to specify initial values, in fact it demands them. Perhaps that will help you. But that raises the question "what do you want to find the minimum of?"

    Ponder this for a while:
    y[r_, m_, a_, k_, b_, c_, t_] := a E^(-r/(2m))Cos[Sqrt[k/m - (r/(2m))^2t]] + b + c t;
    squarederror = Total[Map[(y[r, m, a, k, b, c, #[[1]]] - #[[2]])^2 &, data[[1]]]];
    FindMinimum[squarederror, {{r, .1}, {m, 1}, {a, 1}, {k, 2}, {b, .75}, {c, 0}}, MaxIterations -> 100]

    But thus far I haven't been able to find initial values that will get it to converge.
     
  4. Oct 20, 2010 #3

    Hepth

    User Avatar
    Gold Member

    Are you sure that equation is right? I htink there should be a phi_0 phase parameter as well if Im not mistaken. Also, the "t" in the cosine MUST be outside that square root, otherwise itll have units.
    [tex]
    \cos \left(\left(\sqrt{\frac{k}{m}-\left(\frac{R}{2 m}\right)^2}\right) t + \phi \right)
    [/tex]

    model = A*E^(-(R/(2 m))*t)*Cos[Sqrt[k/m - (R/(2 m))^2] t + \[Phi] ] +
    B + c t
    sln = FindFit[data,
    model, {A, {R, 1/5}, {m, 1/2}, {k, 5}, B, {\[Phi], -1.5}, c}, t];
    Show[ListPlot[data], Plot[model /. sln, {t, 0, 5}]]
    sln




    That fits quite nicely.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook