# 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[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.

• Have a look at this Commented Feb 18, 2018 at 9:20
• @kglr Please have a look. Commented Feb 18, 2018 at 10:34
• How do I import your data in Mathematica? Commented Feb 18, 2018 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. Commented Feb 18, 2018 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. Commented Feb 18, 2018 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[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}

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}

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}

• Hi, I entered your code, but I get the following errors: prntscr.com/igfays Commented Feb 18, 2018 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 Commented Feb 18, 2018 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} Commented Feb 18, 2018 at 15:20
• Actually m is very off. l is fine.. I don't know how you find m Commented Feb 18, 2018 at 15:22
• Remove l and m from your code and use Clear[l,m] Commented Feb 18, 2018 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[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}]

• is it possible to graph this over the data? Commented Feb 18, 2018 at 14:48