# Stopping condition for FindMinimum / FindMaximum

This seems like it should be a simple question -- but I am looking to use a "home made" stopping condition with FindMaximum, while evaluating a very complex function.

Printing out the successive changes in the results for my problem (which cannot be boiled down to a quick code sample) I see that the relative differences in about 100 params are on the order of 10^-3 between steps (and often much smaller) which is more than enough for this application -- but it just continues to dance around the "right" answer until I hit MaxIterations, and adjusting Precision and Accuracy does not seem to help.

Q: Is there a way to implement a "stopping condition" option for these functions?

You could use StepMonitor to check if we reached our goal and then Throw / Catch to stop.

Here is test example, where we print x,y, and the change of the function.

fun[x_, y_] = -x^4 - y^4 + x y + x^2 y;
last = {0, 0};
FindMaximum[fun[x, y], {x, y},
StepMonitor :> Print[{x, y, Norm[last - {x, y}]}]]


We now add Throwand Catchto stopp when the difference is smaller than eps:

eps = 0.1;
Catch@FindMaximum[fun[x, y], {x, y},
StepMonitor :>
If[Print[{x, y, Norm[last - {x, y}]}]; Norm[last - {x, y}] < eps,
Throw[{x, y}], last = {x, y}]]


• Very elegant solution; have it implemented and working. Commented Apr 9, 2021 at 12:02