Search code examples
matlaboptimization

Ending fminsearch on a whim


Id like to be able to cancel fminsearch (eg using ctrl+c) and retaining the best solution so far. Is there a way to do this? I dont know exactly what sort of tolerance I want to set, I'd much rather "play by ear" and ctrl+c when I'm satisfied with the solution. Matlab keeps going onnn and on and on for what seems to be no gain.


Solution

  • fminsearch can be terminated early by both output functions and plot functions. To get an interactive demand from the user, use of graphical interfaces that don't wait until code is finished executing to update is basically essential, so using a plot function would be the most straightforward way of achieving this. The default plot function figure has "Stop" and "Pause" buttons built in, as seen in the documentation's example for calling:

    fminsearch(funfcn,x,optimset('PlotFcns',@optimplotfval))
    

    Screenshot of fminsearch example minimization of Rosenbrock's function with a plotting function while running

    If the Stop button is clicked, fminsearch returns the result of its latest iteration and by default displays the information:

    Optimization terminated prematurely by user.