Get argument values using an output function when Objective returns NaN

I'm using fmincon to minimize a function with arguments X. I get the message on a given iteration and the code continues:
"Objective function returned NaN; trying a new point..."
I want the optimizer to STOP and tell me what that argument value is. I have attempted to look at the confusing output function documentation and attempted to make my own output function:
function [stop,wat] = outfun2(x,optimValues,~)
stop=false;
if isnan(optimValues.fval)==1
stop=true;
wat=x;
end
I know this is definitely not correct. The fmincon code just ignores this and continues the optimization.
What is the best way to just get the argument values from an optimizer?
Can I also interrupt the optimization (with control-c) and still get the arguments at the current iteration?
SOLVED: So the answer that Walter gave and the comments helped me figure it out: So the output function stuff is totally unnecessary. Just add a global variable (inside and outside the objective function) and inside the objective let the global variable equal the parameters. Then add 'FunValCheck','on' to your options. This will cause the minimization to stop and pop out your global variable with the last iteration where it failed.

 Respuesta aceptada

Did you try setting the FunValCheck option?

3 comentarios

Walter,
I added that and now the optimization stops, but I still don't get any output (meaning I don't see the value of the parameter at the last iteration). Any assistance on that?
In your objective function, write a success status and current t and y values to a shared variable (possibly a global variable) that you can check afterwards.
Alright that helped. I put what I did in the question.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

CJ
el 15 de Sept. de 2017

Comentada:

CJ
el 19 de Sept. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by