0

How to stop fminsearch when objective function exceeded certain value (minima or maxima)

options = optimset('MaxFunEvals',9999);
[x,fval,exitflag,output]  =  fminsearch(@(var)objectiveFunction(variables), changingParameters,options);

How to stop the function if I reach certain objective function value (for example 1000) [within the 9999 iterations]

I've tried 'TolFun' , I am not sure if this is correct

options = optimset('MaxFunEvals',999,'TolFun',1000);
[x,fval,exitflag,output]  =  fminsearch(@(var)objectiveFunction(variables), changingParameters,options);
Adnan j
  • 109
  • 10

1 Answers1

1

You can manually stop the search procedure by placing an appropriate function in the options.OutputFcn input struct. This function is called in every iteration of the search, and permits to signal back that the search is to be terminated. For example, you could define

function stop = custom_stop_fun(~, optimValues, ~)
if optimValues.fval >= 1000
    stop = true;
else
    stop = false;
end
end

and then set it via

options.OutputFcn = @custom_stop_fun;

Check out the full OutputFcn documentation

Robert
  • 575
  • 3
  • 12