Looking to combine the two approaches from these two posts here:

How to Add a Constraint on the Derivative of the Model in FindFit

How to fit 3 data sets to a model of 4 differential equations?

They use two different techniques to fit a model using FindFit and I am trying to combine the two to accomplish both adding a constraint on the derivative of the model and modelling more equations than I have sets of data.

First, lets say I want to apply conditions on my model for a FindFit function. This can be done in the following way:

modelt[a_?NumberQ, k_?NumberQ] := (modelt[a, k] = 

   First[x /. 

     NDSolve[{x'[t] == (k/2)*(Sin[x[t] + a] + Cos[x[t] + a]), 

       x[0] == Pi/2}, {x}, {t, 0, 1000}]])

FindFit[{1000, Pi/6}, {modelt[a, k][t], modelt[a, k]''[1000] < 0}, {a,

   k}, t, Method -> {NMinimize, Method -> "SimulatedAnnealing"}]

where modelt[a, k]''[1000] < 0 returns the value of the second derivative of model for optimal parameter values.

However, lets say I have a system of two equations and still only one dataset. I can change the above to handle that:

ModelSol = 

  ParametricNDSolveValue[{x'[t] == 

     k[t]/2 (Sin[x[t] + a] + Cos[x[t] + a]), k'[t] == f Sin[-x[t]], 

    x[0] == \[Pi]/2, k[0] == 1/3}, {x, k}, {t, 0, 5000}, {a, f}];

model[a_, f_][i_, t_] := 

  Through[ModelSol[a, f][t], List][[i]] /; 

   And @@ NumericQ /@ {a, f, i, t};

FindFit[{{1, 5000, \[Pi]}}, {model[a, f][i, t]}, {a, f}, {i, t}, 

 Method -> {NMinimize, Method -> "SimulatedAnnealing"}]

Now this allows me to handle multiple equations even with only that one data point, but I can no longer specify constraints on the derivatives of model or ModelSol. Is there a way to allow this? That is, change one of the above examples to both handle multiple equations with one data point and allow for constraints on the derivative?


  • I believe the issues is with asking for a time derivative of the output from ParametricNDSolveValue, I am not entirely sure if it is possible to take the time derivative of the output from this function. However, in principle, the problem I am having could be fixed if I could figure out how to take a derivative of the output from ParametricNDSolveValue just as has been done with NDSolve.
  • $\begingroup$ If you don't supply constraints in the fitting function, are the constraints not (or never?) satisfied with the result you get? I suspect that must be the case but you might want to state that explicitly. $\endgroup$ – JimB Oct 17 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.