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

2 views (last 30 days)
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
Roger Stafford on 15 Jun 2014
Edited: Roger Stafford on 15 Jun 2014
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
Star Strider
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
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.

Sign in to comment.


Star Strider
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
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

Tags

Products

Community Treasure Hunt

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

Start Hunting!