# FindFit or NMinimize? Fitting a System of ODEs to Long Time Behaviour

I am looking to fit a model composed of a system of ODEs to a long term behaviour given by a single data point of the final steady state of the system (e.g., x == pi/2). I have tried using both FindFit and NMinimize but haven't gotten lucky with either. Below are some examples:

Using the FindFit documentation, I have attempted to make a model function, solve it within the function, and then feed that back into FindFit. However, I get some errors saying that the function value is not a real number and that the evaluation of the gradient has failed:

model[a_?NumericQ, b_?NumericQ,
c_?NumericQ] := (model[a, b, c] =
First[x /.
NDSolve[{x'[t] == 0.1 + k[t]*(Sin[x[t] + a] + Sin[-x[t] + b]),
k'[t] == c*Sin[x[t]], x == 3, k == 1/3}, {x, k}, {t, 0,
1000}]]);

FindFit[{x == 3.1415/2}, {model[a, b, c][t], {0 < a < 2*3.1415,
0 < b < 2*3.1415, c > 0}}, {a, b, c}, t]


It just returns the FindFit statement.

I have also taken a look at NMinimize and was able to successfully use it for the case of constant k, but unable to use it for the time-dependent k. The case of constant k is below.

f = x'[t] == 0.1 + k*(Sin[x[t] + a] + Sin[-x[t] + b]);
ObjCon = {0, Abs[(f /. x -> 3.1415/2) - 0]^2 < tol && (D[f, k] /. x -> 3.1415/2) < 0};
Params = {a, b, k};
tol = 10^-6;
ParamSols = NMinimize[ObjCon, Params]


So my question is, does anyone know either how to modify my NMinimize set up to allow for the dynamic k or does anyone see what has gone wrong in the FindFit attempt? Is one method 'better' than another in this case?

• Your use of FindFit does not follow its required syntax. The first argument should be a set of data (i.e., a numerical matrix) while you specify an equation. If you change it to {{1000, 3.1415/2}}, it will give a result. Oct 15 '19 at 8:43
• Hey @SjoerdSmit ! Thanks for the response, you are right. That has fixed the errors. I will post it as an answer for anyone else - thanks! Oct 15 '19 at 8:49

As @SjoerdSmit mentioned in the comments, I had a syntax error for the 'data' section of my FindFit function. It should read {{1000, Pi/2}} instead of how I had written it above. This is because the first argument of FindFit should be a SET of data. Thanks to Sjoerd!