using fsolve to solve the golden ratio equation, the numerical solution have a extraneous root,but it displays -1,why?
Show older comments
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?
Answers (3)
Roger Stafford
on 15 Jun 2014
Edited: Roger Stafford
on 15 Jun 2014
3 votes
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 Comments
John D'Errico
on 15 Jun 2014
A good, clear, complete answer.
Star Strider
on 15 Jun 2014
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
on 15 Jun 2014
Edited: John D'Errico
on 15 Jun 2014
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
on 15 Jun 2014
... 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
on 15 Jun 2014
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
on 15 Jun 2014
Thanks, Matt.
Neither fsolve nor fzero have problems with the polynomial definition, even with initial parameter estimates of +1 or -1.
John D'Errico
on 15 Jun 2014
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
on 13 Jun 2014
Edited: Star Strider
on 13 Jun 2014
Use roots:
GoldenRatio = roots([1 -1 -1])
produces:
GoldenRatio =
-0.618033988749895
1.618033988749895
Matt J
on 13 Jun 2014
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
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!