MATLAB: How to get and plot current “x” and “fval” during an fmincon optimization embedded in a GUI

fminconguioutput functionplot

Hi,
I am writing an optimization code embedded in a GUI. Optimization works well at the moment, but I would like to improve the functionality of my GUI. In that context, I would like to plot a dynamic graph of "current point" and "current function value" of fmincon iterations on my GUI. My problems are:
1) I couldn't achieve to write an working Output function.
2) About GUI plot; I am not sure if I have to write plot command in my optimization function or in GUI callback
Thanks in advance
Sercan

Best Answer

  1. I don't know what the problem was with the OutputFcn, but this link gives an example almost identical to what you're talking about. In the OutputFcn in this example, the "history" structure dynamically accumulates the x and fval data and plots the x's.
  2. Since you say you want a "dynamic graph" I assume you want it to occur while the optimization is in progress. Therefore, the OutputFcn, and not the callback (and also not in your objective function), is the place to do that. The callback will only reacquire control of the program once the optimization has finished.