Fsolve produces comlex numbers

조회 수: 17 (최근 30일)
Bismark
Bismark 2023년 5월 29일
댓글: Walter Roberson 2023년 5월 30일
When i run my code, the result are complex numbers. Is there anyway i can locate the cause or any suggestions on how to obtian non-complex numbers will be helpful.
  댓글 수: 2
Rik
Rik 2023년 5월 29일
Have a read here and here. It will greatly improve your chances of getting an answer.
Bismark
Bismark 2023년 5월 29일
Okay Rik. Thank you

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

답변 (2개)

Torsten
Torsten 2023년 5월 29일
편집: Torsten 2023년 5월 29일
The usual reasons why solvers like "fsolve" produce complex numbers are:
  • you take the square-root of a negative number (sqrt(x) with x negative)
  • you exponentiate a negative number (x^a with x negative)
  • you take the logarithm of a negative number (log(x) with x negative)
  • or similar operations with functions that give complex results ...

John D'Errico
John D'Errico 2023년 5월 29일
Remember that fsolve does not allow constraints. Not even bound constraints. So when one of the variables tries to go into a bad place, where, perhaps you are taking the sqrt of a negative number, then complex results start getting created. That commonly happens when a log, a sqrt, a fractional power are applied to a negative number. But other operations surely cause problems of this sort too.
I would strongly suggest you learn to use the debugger. Use it to watch your objective function, and see when something strange happens.
You might decide to set breakpoints in your function. Or, you might just decide to dump the current set of parameters at each iteration to the command window, as well as the objective. Watch for when a problem arises, and you will see that what we have said is surely true.
help debug
DEBUG List MATLAB debugging functions dbstop - Set breakpoint. dbclear - Remove breakpoint. dbcont - Resume execution. dbdown - Change local workspace context. dbmex - Enable MEX-file debugging. dbstack - List who called whom. dbstatus - List all breakpoints. dbstep - Execute one or more lines. dbtype - List file with line numbers. dbup - Change local workspace context. dbquit - Quit debug mode. When a breakpoint is hit, MATLAB goes into debug mode, the debugger window becomes active, and the prompt changes to a K>>. Any MATLAB command is allowed at the prompt. To resume program execution, use DBCONT or DBSTEP. To exit from the debugger use DBQUIT. Documentation for debug doc debug
Can you stop fsolve from going to the bad places in your parameter space? Sadly, no. You can use other optimizers. But even there, you need to know where it is trying to go, and what constraints you need to apply. You can also decide to transform the parameters, to avoid the problem from happening. Of course, since we don't see any code, it becomes impossible to offer any useful help.
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 5월 30일
Debugging tip:
On the first executable line after you calculate the value to be returned, put in a conditional breakpoint. The command line form of it would be like
dbstop at LINENUMBER if any(imag(VARIABLE)~=0)
or you can right-click when pointing to the dot before the line in the editor and insert a conditional breakpoint with expression any(imag(VARIABLE)~=0)
This requires that there is at least one executable line in the function before the function is finished. The easiest way to arrange that is to use an end statement matching the function header. (Note that all functions in the same file must be the same style -- either they all have end matching function or else none of them do.)

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by