Why does my objective function return complex?

조회 수: 10 (최근 30일)
Snoopy
Snoopy 2022년 2월 28일
댓글: Walter Roberson 2022년 3월 2일
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
Torsten
Torsten 2022년 2월 28일
log(x), x^a and sqrt(x) are the most common sources to get complex numbers.
David Goodmanson
David Goodmanson 2022년 2월 28일
also asin and acos

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2022년 2월 28일
편집: Matt J 2022년 2월 28일
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
Snoopy
Snoopy 2022년 3월 2일
편집: Snoopy 2022년 3월 2일
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 2022년 3월 2일
suggest you add a conditional breakpoint to trigger if any(imag(VALUE))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by