Why does my objective function return complex?

5 vues (au cours des 30 derniers jours)
Snoopy
Snoopy le 28 Fév 2022
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 commentaires
Torsten
Torsten le 28 Fév 2022
log(x), x^a and sqrt(x) are the most common sources to get complex numbers.
David Goodmanson
David Goodmanson le 28 Fév 2022
also asin and acos

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 28 Fév 2022
Modifié(e) : Matt J le 28 Fév 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 commentaires
Snoopy
Snoopy le 2 Mar 2022
Modifié(e) : Snoopy le 2 Mar 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.
Walter Roberson
Walter Roberson le 2 Mar 2022
suggest you add a conditional breakpoint to trigger if any(imag(VALUE))

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by