NSolve
is unpredictably unreliable. I change a parameter that has no effect on the solution and get one of four outcomes: 1) both correct answers, 2) one correct answer, 3) it just returns the command back, or 4) it hangs for a long time.
Do[
Print[r, " ",
NSolve[
{n1 == E^(r (1 - n1 - 0.6 n2)) n1, n2 == E^(r (1 - 1.2 (0.6 n1 + n2))) n2}
, {n1, n2}]
]
, {r, 1.0, 2.0, 0.1}]
gives
1. {{n1->1.,n2->0.},{n1->0.78125,n2->0.364583}}
1.1 NSolve[{n1==E^(1.1 (1-n1-0.6 n2)) n1,n2==E^(1.1 (1-1.2 (0.6 n1+n2))) n2},{n1,n2}]
1.2 {{n1->0.78125,n2->0.364583}}
1.3 {{n1->0.78125,n2->0.364583}}
1.4 {{n1->0.78125,n2->0.364583}}
1.5 {{n1->1.,n2->0.},{n1->0.78125,n2->0.364583}}
1.6 {{n1->0.78125,n2->0.364583}}
1.7 {{n1->0.78125,n2->0.364583}}
1.8 {{n1->0.78125,n2->0.364583}}
1.9 NSolve[{n1==E^(1.9 (1-n1-0.6 n2)) n1,n2==E^(1.9 (1-1.2 (0.6 n1+n2))) n2},{n1,n2}]
and hangs on r=2.0
when I run it on Mathematica 10.4.1 on MacOS 10.11.6. I tried all of the NSolve
Methods
from this answer but they make no difference.
Bug? Possible workaround (not for this particular problem, but this phenomenon in general)?
Reals
it finds all four in some cases but still hangs on others. $\endgroup$NSolve
on unknown arguments that might fail. Anyhow, I'll think about changing that function to not rely so heavily onNSolve
. $\endgroup$