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[1000] == 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[0] == 3, k[0] == 1/3}, {x, k}, {t, 0, 

FindFit[{x[1000] == 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?

  • 1
    $\begingroup$ 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. $\endgroup$ Oct 15, 2019 at 8:43
  • $\begingroup$ 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! $\endgroup$
    – Cameron F.
    Oct 15, 2019 at 8:49

1 Answer 1


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!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.