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

I would like to add constraints on a model and its derivatives and then use FindFit to fit some parameters. An example is below:

modelt[a_?NumberQ, k_?NumberQ] := (modelt[a, k] =
First[x /.NDSolve[{x'[t] == (1/2)*y[t]*(Sin[x[t] + a] + Cos[x[t] + a]),
y'[t] == k Sin[x[t]], x[0] == Pi/2, y[0] == 1/2}, {x,  y}, {t, 0, 1000}]])

FindFit[{1000, Pi/6}, {modelt[a, k][t]}, {a, k}, t,
Method -> {NMinimize, Method -> "SimulatedAnnealing"}]


Is there a way to add constraints to FindFit such as x'[1000] == 0, x''[1000] < 0,. How do you go about doing this? Thanks!

• Formally, you can add additional constraints to second argument of FindFit (see help). And because x[t] is modelt[a, k][t] you can write smth. like FindFit[{1000, Pi/6}, {modelt[a, k][t], modelt[a, k]'[1000]==0,modelt[a, k]''[1000]<0}, {a, k}, t, Method -> {NMinimize, Method -> "SimulatedAnnealing"}].
– Alx
Commented Oct 16, 2019 at 12:25
• Hey @Alx ! Thanks for the help, you were correct in your suggestion! I will add it as a solution. Commented Oct 17, 2019 at 9:02
• @Alx, I have started working on combining your solution with another popular question on FindFit but have come to a bit of a roadblock, if you have a second let me know! Here is the link to it: mathematica.stackexchange.com/questions/208054/… Commented Oct 17, 2019 at 9:55

As @Alx suggested in the comments, adding constraints onto the model such as modelt[a, k]'[1000]==0,modelt[a, k]''[1000]<0 was the correct method. Thanks to him!