Why does my objective function return complex?

I have a likelihood function that I need to minimize. I use fmincon to search for the minimum. Iterations of fmincon sometimes show "Objective function returned complex; trying a new point…" But in my code I do not take square root of any variable. Why am I then getting this error? A second question is whether there is a counterpart of
dbstop if naninf
for "complex" like the
dbstop if complex
command? Or would
dbstop if error
do?

2 comentarios

Torsten
Torsten el 28 de Feb. de 2022
log(x), x^a and sqrt(x) are the most common sources to get complex numbers.
also asin and acos

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 28 de Feb. de 2022
Editada: Matt J el 28 de Feb. de 2022
There is no "dbstop if complex", but you can set a conditional breakpoints in your objective function to detect when theany particular value is complex.Or, insert a keyboard() command.
function fval=myobjective(x)
fval=....
if ~isreal(fval)
keyboard
end
end

4 comentarios

Snoopy
Snoopy el 2 de Mzo. de 2022
At https://nl.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html#buvvmfu-1, Pause on Errors to pause on all errors looks like an alternative solution.
Torsten
Torsten el 2 de Mzo. de 2022
Getting complex is not an error.
Snoopy
Snoopy el 2 de Mzo. de 2022
Editada: Snoopy el 2 de Mzo. de 2022
OK. So "Objective function returned complex; trying a new point..." is not an error and MATLAB will not pause when this warning is displayed.
suggest you add a conditional breakpoint to trigger if any(imag(VALUE))

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Debugging and Analysis en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Feb. de 2022

Comentada:

el 2 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by