HP Forums

Full Version: Oddity with fsolve, nSolve, etc.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In CAS view,

there appears to be a problem with displaying the zeros of functions with multiple roots, e.g., I ran into this oddity recently:

fsolve(x^3-3*x^2+4=0, x=-1.5..2.5)

nSolve(x^3-3*x^2+4=0, x=-1.5..2.5)

both return [-1 0. 2.] Here the solution 0. is clearly incorrect.

I also find it strange that both the wrong solution 0 and the double root 2 are displayed approximately as 0. and 2., whereas the single root of -1 is displayed exactly (as it should be).

BTW,

solve(x^3-3*x^2+4=0,x) returns {-1,2}, which is OK, but better would be {-1,2,2}. Same for zeros(x^3-3*x^2+4=0,x) returns [2 -1], but again, better would be [2 2 -1].
(01-11-2014 01:06 AM)Helge Gabert Wrote: [ -> ]In CAS view,

there appears to be a problem with displaying the zeros of functions with multiple roots, e.g., I ran into this oddity recently:

fsolve(x^3-3*x^2+4=0, x=-1.5..2.5)

nSolve(x^3-3*x^2+4=0, x=-1.5..2.5)

both return [-1 0. 2.] Here the solution 0. is clearly incorrect.

I don't think nSolve is meant to return multiple solutions, but only a single solution based on a guess that is closest to that root. So,

nSolve(x^3-3*x^2+4=0, x=0) returns the single value -1.000

and

nSolve(x^3-3*x^2+4=0, x=0.5) returns the single value 2.000

both assuming Number Format is Fixed 3.
No, both nSolve and fsolve are designed to return multiple solutions, if one selects an interval, like I did in my original post!

However, if one only selects one value as the starting point, as you did, then the (presumably) nearest solution will be returned.
(01-11-2014 02:37 AM)Helge Gabert Wrote: [ -> ]No, both nSolve and fsolve are designed to return multiple solutions, if one selects an interval, like I did in my original post!

However, if one only selects one value as the starting point, as you did, then the (presumably) nearest solution will be returned.

Yes, but I selected zero, and it did not find zero. Instead it found -1, so I think that shows that it does not return a false root if used with a single guess. Where in the documentation do you see the interval form for nSolve ? I only see it shown for fSolve.
P.S. For example, for fsolve, just look up p. 393 of the HP Prime User Guide. You will see that intervals, returning multiple solutions, are clearly envisioned.

"With the optional third argument you can specify a guess for the solution or an interval within which it is expected that the solution will occur."
P.P.S.

for nSolve, look in the XCAS documentation.

Chapter 4.4, p.359 of the XCAS manual "solve equations with fsolve nSolve". Clearly fsolve and nSolve use the same syntax and argument structure.
(01-11-2014 02:48 AM)Helge Gabert Wrote: [ -> ]P.S. For example, for fsolve, just look up p. 393 of the HP Prime User Guide. You will see that intervals, returning multiple solutions, are clearly envisioned.

"With the optional third argument you can specify a guess for the solution or an interval within which it is expected that the solution will occur."

I don't see anything indicating multiple solutions, just a single solution occurring in a particular interval. The example shown just shows a single solution. Perhaps when you provide an interval that encompasses more than one valid solution, it gets confused. If you plot that function, you'll notice that it changes slope at x=0, which may confuse the solver. Also, the root at x=2 is unique, since the function does cross the ordinate axis. This is why Solve and Zeros do not list the root 2 twice.
I don't understand what you are trying to say, and where you are coming from.

1) Clearly, fsolve and nSolve return an incorrect root (namely 0.), when called with the arguments as I posted in my original posting.

2) Whether the examples show a single solution or not, the fact is that multiple solutions are returned for a particular interval (if they exist), which is actually very valuable and is welcomed, at least in my book.

3) What do you think is the third root of x^3-3*x^2+4 (if you believe 2 is not a double root)?

But if you think that there is no problem, then that is fine with me. I just hope that HP listens in and takes a look at an incorrectly returned root with fsolve and nSolve.
(01-11-2014 03:05 AM)Helge Gabert Wrote: [ -> ]for nSolve, look in the XCAS documentation.

Chapter 4.4, p.359 of the XCAS manual "solve equations with fsolve nSolve". Clearly fsolve and nSolve use the same syntax and argument structure.

But, is XCAS fully implemented in the Prime ? IIRC, CAS in the Prime is not quite the same animal as full blown XCAS.

BTW, your syntax for fSolve is incorrect. It should be fSolve(Expr,Var,[Guess],[Method]), where the second parameter is the variable and the third is the optional guess or range. So:

fsolve(x^3-3*x^2+4=0,x), which yields the correct result [-1.000 1.998 2.002]

Also:

solve(x^3-3*x^2+4=0,x) yields the same correct result {-1.000 1.998 2.002}

Now, this was with Exact unchecked in CAS Settings, but if I check Exact, then I get {-1,2} for solve, but still get [-1.000 1.998 2.002] for fsolve, so solve is affected by Exact but fsolve is not.

So, if you want to see all the roots, including double roots, then you just need uncheck Exact in CAS Settings.
No, not all of XCAS is implemented on the Prime. But so what? Again, fact is, on the Prime, nSolve can be run with the exact same syntax and arguments as fsolve. That is all that matters to me for this problem.
Please read my edited reply regarding fSolve.
Yes, there should be an "x" as the second argument; that is correct; however, you can write "x=single value" or x="range" as a shorthand.

And fsolve(x^3-3*x^2+4,x,-1.5..2.5) still gives the same incorrect result [-1 0. 2].

Do you believe me now that 2 is a double root? (Also called a degenerate root, by the way). And that multiple roots can be returned by fsolve and nSolve?

But thanks for pointing out that fsolve(eq, x) returns all three roots (even though 2 is not returned exactly, but since fsolve is a numerical command, that is close enough for me).
Reference URL's