fsolve yields wrong answer

조회 수: 4 (최근 30일)
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2021년 11월 9일
댓글: Pooneh Shah Malekpoor 2021년 11월 10일
Hello
It is an example of many equations in my lengthy code. I want to use fsolve to solve it as it is more efficient; however, it yields wrong answer:
F = @(x)((x-20.6667)^2+57.89);
opts = optimoptions('fsolve', 'Display', 'off');
x(1) = fsolve(@(x) F(x), 0, opts);
in this case it yields x(1)=20.6667 ; howver, it should not yield any solution in real domain!!!!I want to get real domain roots by fsolve.
What should i do? I dont want to use solve as it results in longer run times.
Bests
Pooneh

채택된 답변

Star Strider
Star Strider 2021년 11월 9일
Give fsolve a complex initial estimate to get a complex root —
F = @(x)((x-20.6667).^2+57.89);
opts = optimoptions('fsolve', 'Display', 'off');
x0 = 1 + 1i;
x(1) = fsolve(@(x) F(x), x0, opts)
x = 20.6667 + 7.6085i
.
  댓글 수: 6
Star Strider
Star Strider 2021년 11월 10일
The fsolve function will find a complex root if given a complex initial estimate, or if a purely imaginary root is the only root.
One option to eliminate complex numbers could be to take their absolute values —
x = 20.6667 + 7.6085i;
xa = abs(x)
xa = 22.0228
and the other option of course is just to use the real part.
However changing the code to accommodate complex numbers may be the best option, because it will likely provide the most accurate results.
.
Matt J
Matt J 2021년 11월 10일
Give fsolve a complex initial estimate to get a complex root —
But note that this will only lead to a proper complex-domain solution under certain conditions:

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

추가 답변 (2개)

Matt J
Matt J 2021년 11월 10일
I want to use fsolve to solve it as it is more efficient; however, it yields wrong answer:
In your example, the function is a polynomial. Is this always the case for you? If so, it is more efficient to use roots(). Moreover, roots() will find all solutions, both real and complex, whereas fsolve can find only one solution.
  댓글 수: 1
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2021년 11월 10일
Thank you so much Matt J! your comment made a huge positive impact on my code and its run time has decreased significantly. This is highly appreciated!

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


Matt J
Matt J 2021년 11월 10일
편집: Matt J 2021년 11월 10일
parts=@(z) [real(z);imag(z)];
complx=@(x) complex(x(1),x(2));
F = @(x)parts((complx(x)-20.6667)^2+57.89);
opts = optimoptions('fsolve', 'Display', 'off');
x = complx( fsolve(F, [0,0], opts))
x = 20.6667 + 7.6085i

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by