Message Boards Message Boards

Apply conditions on a model in FindFit?

Posted 5 years ago

Hey! I am working with FindFit to do some model fitting but am having a hard time combining two different approaches. I will show those working examples and then ask why the combined example failed.

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?

Info:

  • I believe the issue arises from the fact that model in the second example no longer refers to the solution of the model as it did in the first.

  • There is also perhaps a slight issue with asking for a time derivative of the output from ParametricNDSolveValue, I haven't been able to ask for a time derivative of the output and then find a numerical value. In principle, the problem could be fixed if I could figure

POSTED BY: Cameron F
2 Replies
Posted 5 years ago

Hey Neil!

Thanks so much for the help :)

Pattern matching was never something I had done in Mathematica before, so I took the idea from a StackExchange post. However, it is very clear that your implementation is much smoother than the one I was using before. Thanks again!

Cameron

POSTED BY: Cameron F

Cameron,

You can do the same thing but your function for model in the second example is not compatible the way you wrote it (using Through).

Try:

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_?NumericQ, f_?NumericQ][i_?NumericQ, 
   t_?NumericQ] := (ModelSol[a, f][[i]])[t];

modelPrime[a_?NumericQ, f_?NumericQ][i_?NumericQ, 
   t_?NumericQ] := (ModelSol[a, f][[i]])'[t];


FindFit[{{1, 5000, \[Pi]}}, {model[a, f][i, t], 
  modelPrime[a, f][1, 5000] < 1000}, {a, f}, {i, t}, 
 Method -> {NMinimize, Method -> "SimulatedAnnealing"}]

I was also not clear why you used that complex way to put constraints on your pattern matching. Am I missing something?

Regards

Neil

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract