0
$\begingroup$

I would like to add a fit to the following data. Here is the differential equation I am using. g=9.81 l=0.2 m=0.09

{((-g*Sin[y[x]])/(l)) - ((k)(y'[x]))/(m) == (y''[x]), y[0] == 1.22173, y'[0] == 0}, y, {x, 0, 3*Pi}

I need to find values for k.

Here is the data

I've also added the data over here.

You can copy-paste this data into mathematica, and it will work.

$\endgroup$
  • $\begingroup$ Have a look at this $\endgroup$ – b.gates.you.know.what Feb 18 '18 at 9:20
  • $\begingroup$ @kglr Please have a look. $\endgroup$ – Omkar Vaidya Feb 18 '18 at 10:34
  • $\begingroup$ How do I import your data in Mathematica? $\endgroup$ – anderstood Feb 18 '18 at 13:09
  • $\begingroup$ @anderstood I believe you could just copy-paste it into mathematica? Does that work? I tried pasting the data here, but slashes appear at the end of each line. $\endgroup$ – Omkar Vaidya Feb 18 '18 at 13:36
  • 2
    $\begingroup$ The data can be retrieved with Most@ToExpression[ "{" <> Import["http://m.uploadedit.com/bbtc/1518933165520.txt"] <> "}"] -- there are minor syntax errors that are fixed with the braces and Most, at least currently. $\endgroup$ – Michael E2 Feb 18 '18 at 14:48
1
$\begingroup$

The following finds the best fit (in some sense defined in the cost function). However based on the data and model you provided, the result is poor --- which is good for you, because having the solution directly from someone else is not very interesting anyway...

l = 0.2; g = 9.81; m = 0.9;
eq[k_] := -g/l*Sin[y[x]] - k*y'[x]/m == y''[x]; 
inter = Interpolation[data];
ic = {y[0] == inter[0], y'[0] == inter'[0]};
cost[k_?NumericQ] := Block[{sol},
  sol = NDSolveValue[{eq[k]}~Join~ic, y, {x, 0, 3}];
  NIntegrate[(sol[x] - inter[x])^2, {x, 0., 3}]]
FindMinimum[cost[k], {k, 4}]
$\endgroup$
  • $\begingroup$ is it possible to graph this over the data? $\endgroup$ – Omkar Vaidya Feb 18 '18 at 14:48
2
$\begingroup$

When we assume g,l and m are known fit is not good.

g = 9.81; l = 0.2 ; m = 0.09;

model[k_?NumberQ] := (model[k] = 
   Module[{y, x}, 
    NDSolveValue[{y''[x] == -g*Sin[y[x]]/l - k y'[x]/m, 
      y[0] == data[[1, 2]], y'[0] == 0}, y, {x, 0, 3*Pi}]])

nlm = NonlinearModelFit[data, model[k][x], k, x, 
   Method -> {NMinimize, Method -> "DifferentialEvolution"}];

Plot[nlm[x], {x, 0, data[[-1, 1]]}, 
 Epilog -> {Red, PointSize[Medium], Point[data]}, PlotRange -> All, 
 Frame -> True]
nlm["BestFitParameters"]

{k -> 0.405883}

enter image description here

We can do better than this when we assume we only know g.

 g = 9.81;

model[k_?NumberQ, l_?NumberQ, 
  m_?NumberQ] := (model[k, l, m] = 
   Module[{y, x}, 
    NDSolveValue[{y''[x] == -g*Sin[y[x]]/l - k y'[x]/m, 
      y[0] == data[[1, 2]], y'[0] == 0}, y, {x, 0, 3*Pi}]])

nlm = NonlinearModelFit[data, model[k, l, m][x], {k, l, m}, x, 
   Method -> {NMinimize, Method -> "DifferentialEvolution"}];

Plot[nlm[x], {x, 0, data[[-1, 1]]}, 
 Epilog -> {Red, PointSize[Medium], Point[data]}, PlotRange -> All, 
 Frame -> True]
nlm["BestFitParameters"]

{k -> 1.25031, l -> 0.298879, m -> 10.5371}

enter image description here

g = 9.81;

model[k_?NumberQ, 
  l_?NumberQ] := (model[k, l] = 
   Module[{y, x}, 
    NDSolveValue[{y''[x] + k y'[x] == -g*Sin[y[x]]/l, 
      y[0] == data[[1, 2]], y'[0] == 0}, y, {x, 0, 3*Pi}]])

nlm = NonlinearModelFit[data, model[k, l][x], {k, l}, x, 
   Method -> {NMinimize, Method -> "DifferentialEvolution"}];

Plot[nlm[x], {x, 0, data[[-1, 1]]}, 
 Epilog -> {Red, PointSize[Medium], Point[data]}, PlotRange -> All, 
 Frame -> True]
nlm["BestFitParameters"]

{k -> 0.119336, l -> 0.29895}

enter image description here

$\endgroup$
  • $\begingroup$ Hi, I entered your code, but I get the following errors: prntscr.com/igfays $\endgroup$ – Omkar Vaidya Feb 18 '18 at 15:16
  • $\begingroup$ Also, does it give a value for k? g, l, m are known constants, with g=9.81, l=0.2 and m=0.09 $\endgroup$ – Omkar Vaidya Feb 18 '18 at 15:18
  • $\begingroup$ l and m are wrong values. So I fit them also. These are the values {k -> 1.25031, l -> 0.298879, m -> 10.5371} $\endgroup$ – OkkesDulgerci Feb 18 '18 at 15:20
  • $\begingroup$ Actually m is very off. l is fine.. I don't know how you find m $\endgroup$ – OkkesDulgerci Feb 18 '18 at 15:22
  • $\begingroup$ Remove l and m from your code and use Clear[l,m] $\endgroup$ – OkkesDulgerci Feb 18 '18 at 15:24

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.