# FindMinimum and WhenEvent

Is there any way to incorporate WhenEvent[] into the FindMinimumfunction ?

I'm currently trying to minimize an interpolating function with FindMinimum (since the origional function is to cumbersome to use). So the command that I use is:

$\Delta$ /. Last[FindMinimum[ interpolation[$\Delta$], {$\Delta$,$\Delta_0$}]]

Where $\Delta$ is my interpolation variable and $\Delta_0$ is my first guess for the minimum (based on the list I used to make the interpolation).

Now in general, when this value for $\Delta$ becomes smaller than some tolerance (I call this variable "tol") it should become equal to zero. So I try to add

WhenEvent[$\Delta<\mathrm{tol}$,$\Delta\rightarrow 0$]

Now wherever I place this command in the FindMinimum procedure, it gives different kinds of errors. I don't know if there is any way to overcome these errors and just get the program to do what I want?

from the documentation of WhenEvent:

WhenEvent expressions can be used in NDSolve, NDSolveValue, ParametricNDSolve, ParametricNDSolveValue, DSolve, and DSolveValue.

so I think no, you can't use WhenEvent within FindMinimum. You might be able to do some things similar to what WhenEvent can be used for with the EvaluationMonitor or StepMonitor options of FindMinimum, though. Redefinition of the variables is not something easily done with them, but wouldn't an extra definition for your interpolating function achieve the same goal?

f[x_?NumericQ]:=Which[x<tol,interpolation,interpolation[x]]
FindMinimum[interpolation[Δ], {Δ,Δ0}]

• Thanks for the fast response! I should have spotted that one! I now just put everything to zero using an If statement, don't know if that's the most efficient way to do it ? The problem with my origional function is that it's pretty hard to calculate to 0, but the extrapolation of the interpolating function behaves nice enough to use. – Nick Jun 12 '15 at 12:50
• Usually all the functions that do some math (be it symbolic or numeric) like functions better which are "mathematical" compared to "programmatical" stuff. So you sometimes get better results faster when you use e.g. UnitStep and similar functions than If or Which, which both are not something that can mathematically understood. I don't think it makes a difference here, at least not if you ensure evaluation only for numeric arguments, but you might try a UnitStep variant and see whether that makes a difference... – Albert Retey Jun 12 '15 at 12:55
• @Nick, you might also consider Chop, which has an option for tolerance specification. – Virgil Jun 12 '15 at 13:02

WhenEvent is not supported by FindMinimum. It is supported by methods such as NDSolve and ParametricNDSolve which produce interpolation functions themselves. So without a more specific function to work with I can suggest you to do the following (I am doing something similar right now)

1. Write your interpolation as a result of one of the Solve methods so that you obtain an output that produces solution=Δ→NDSolve Interpolation Function. You can put your WhenEvent in this step and you will have an output.

MWE: this is literally mostly copied from my own working code

solPar = ParametricNDSolve[Join[eqns, con, tempini], Join[variables], {t, 0, tend}, parameters, MaxSteps -> 10^6];

again, note that this could be done with NDSolve too. This solves the system of equations in eqns and produces interpolation functions for the list of parameters, which is something like {f1, f2,.. f50}. The interpolation is produced for the range between 0 and tend. tempini is the list of initial conditions.

The event switches are included in con as a table produced automatically, but here Il'll just show one

con=WhenEvent[t == timeEv, {f[t] -> fnexp}];

1. Then do something like FindMinimum[ Δ /.solution, {Δ,Δ0}]

In my case optimum=NMinimize[-h[parameters, tend]/.solPar, constrs, parameters]

where constrs is a list of constraints as I am doing a constrained optimization case, but if I had to do it uncontrained like you I would do FindMinimum[ h[parameters, tend] /.solPar, {h,h0}]