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