using fsolve to solve the golden ratio equation, the numerical solution have a extraneous root,but it displays -1,why?
이전 댓글 표시
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
2014년 6월 15일
편집: Roger Stafford
2014년 6월 15일
3 개 추천
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
John D'Errico
2014년 6월 15일
A good, clear, complete answer.
Star Strider
2014년 6월 15일
My understanding of the ‘golden ratio’ is that it is the positive root of the observation that:
x = 1/(x-1)
so the negative root is a valid solution to the polynomial equation. I simply suggested that taking the roots of the polynomial was a more direct way of finding the answer. Obviously not all roots of polynomials make physical sense in the context of the problem.
John D'Errico
2014년 6월 15일
편집: John D'Errico
2014년 6월 15일
But the point is that in the particular form it has been presented, with the sqrt in the expression, -0.618... is not a valid solution because sqrt always uses the positive branch. The negative solution is a solution of the quadratic polynomial form, but this NOT what has been presented to us, and not what the question really asked. Thus had the function been supplied as
f=@(x)x + sqrt(1+x);
it should converge to the negative solution. We can see this using ezplot.
ezplot(f)
grid on

Instead, with the original form
f=@(x)x - sqrt(1+x);
ezplot(f)
grid on

Here we see the positive solution will be arrived at. The figures also suggest why only one of the solutions is generated. When x goes below -1, in either case we begin to see complex results from the expression.
The latter figure also shows what Roger said, that when you start at -1 with that function, a derivative based solver will try to move in the negative direction. Of course, this immediately creates complex results, which will in turn cause fsolve to burp.
All of this points out the care one must take when a sqrt is involved in comparing seemingly equivalent forms.
Star Strider
2014년 6월 15일
... and (probably) the reason I was taught the polynomial definiton of the ‘Golden Ratio’. We didn’t have fsolve back then.
I do think it makes more sense to use a polynomial solver when applicable. However, assuming the idea is to find the Golden Ratio, and you did insist on using an iterative algorithm, you would not choose a negative initial guess, nor even an arbitrary positive guess like +1. You would cut down on iterations by initializing with a ratio of large Fibonacci numbers (e.g., the following takes only 1 iteration to complete)
>> x=fsolve(@(x) x-sqrt(1+x), 144/89)
x =
1.618033988885993
Thus, if you are doing normal, practical things, ill-defined square roots shouldn't really be a problem.
John D'Errico
2014년 6월 15일
My guess is that the problem posed is a homework one, and that -1 was chosen specifically to show how it would fail in that case with THIS particular form. -1 is too special a case of starting value to be a random choice.
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
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
2014년 6월 13일
편집: Star Strider
2014년 6월 13일
Use roots:
GoldenRatio = roots([1 -1 -1])
produces:
GoldenRatio =
-0.618033988749895
1.618033988749895
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
카테고리
도움말 센터 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!