How can I fit a differential equation to experimental data?

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 == 1.22173, y' == 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.

• Have a look at this – b.gates.you.know.what Feb 18 '18 at 9:20
• @kglr Please have a look. – Omkar Vaidya Feb 18 '18 at 10:34
• How do I import your data in Mathematica? – anderstood Feb 18 '18 at 13:09
• @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. – Omkar Vaidya Feb 18 '18 at 13:36
• 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. – Michael E2 Feb 18 '18 at 14:48

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 == data[[1, 2]], y' == 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} 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 == data[[1, 2]], y' == 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} 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 == data[[1, 2]], y' == 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} • Hi, I entered your code, but I get the following errors: prntscr.com/igfays – Omkar Vaidya Feb 18 '18 at 15:16
• 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 – Omkar Vaidya Feb 18 '18 at 15:18
• l and m are wrong values. So I fit them also. These are the values {k -> 1.25031, l -> 0.298879, m -> 10.5371} – OkkesDulgerci Feb 18 '18 at 15:20
• Actually m is very off. l is fine.. I don't know how you find m – OkkesDulgerci Feb 18 '18 at 15:22
• Remove l and m from your code and use Clear[l,m] – OkkesDulgerci Feb 18 '18 at 15:24

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 == inter, y' == inter'};
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}]
• is it possible to graph this over the data? – Omkar Vaidya Feb 18 '18 at 14:48