using fsolve to solve the golden ratio equation, the numerical solution have a extraneous root,but it displays -1,why?

3 ビュー (過去 30 日間)
f=@(x)x-sqrt(1+x);
x1=fsolve(f,1)
x2=fsolve(f,-1)
x1=1.61803399144948 is correct.
But why x2=-1 ? What's more ,if
x2=fsolve(f,-4)
The answer may different,how to explain it?

回答 (3 件)

Roger Stafford
Roger Stafford 2014 年 6 月 15 日
編集済み: Roger Stafford 2014 年 6 月 15 日
If you make a plot of your 'f' function, it is possible to see why 'fsolve' gets into trouble with the initial estimate of x = -1. The value of f(x) reaches a minimum at x = -.75 and between that value and x = -1 it has a negative derivative. If you give an initial estimate of x within this range, 'fsolve' will naturally tend to decrease x in order to increase f(x). However, past x = -1 the value of f(x) becomes complex because the square root of a negative number is imaginary and as the 'fsolve' documentation states, "fsolve only handles real variables". It therefore becomes confused at that point. It cannot reach zero by decreasing x without running into complex values. To add to 'fsolve' difficulties, the derivative at x = -1 becomes minus infinity which adds to the confusion.
By the way, the "root" x = -0.618033988749895 is not a true solution in the form you have expressed the function 'f'. The square root of a non-negative real number is understood to be a non-negative real number, so this value of x cannot possibly satisfy your equation.
  8 件のコメント
Star Strider
Star Strider 2014 年 6 月 15 日
Thanks, Matt.
Neither fsolve nor fzero have problems with the polynomial definition, even with initial parameter estimates of +1 or -1.
John D'Errico
John D'Errico 2014 年 6 月 15 日
They would not have a problem with a polynomial form, since there is no sqrt branch to deal with. But that just says that IF the goal is to compute the golden ratio using some iterative method applied to some other function than the one in this question, that those methods work.
However, if ones goal is simply to compute the golden ratio, why not just do this?
phi = (1 + sqrt(5))/2
phi =
1.61803398874989
For that matter, fzero works nicely on the problem posed too:
x=fzero(@(x) x-sqrt(1+x),[-1,5])
x =
1.61803398874989
I think things have gotten off track here. Remember that the question posed here was to understand WHY does fsolve fail in this case? The question was not how to compute the golden ratio using some other favorite algorithm. In that case, the direct and simple formula above seems best.

サインインしてコメントする。


Star Strider
Star Strider 2014 年 6 月 13 日
編集済み: Star Strider 2014 年 6 月 13 日
Use roots:
GoldenRatio = roots([1 -1 -1])
produces:
GoldenRatio =
-0.618033988749895
1.618033988749895

Matt J
Matt J 2014 年 6 月 13 日
fsolve can fail. It should have given you an output message saying "No Solution Found". You should also be checking the exitflag of all Optimization Toolbox solvers,
>> [x2,fval,exitflag]=fsolve(@(x)x-sqrt(1+x),-1); exitflag
exitflag =
-2

カテゴリ

Help Center および File ExchangeSolver 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