1
$\begingroup$

I noticed that NSolve isn't running properly when I have some seemingly harmless numbers in my expression. Here is a simple example:

NSolve[1/x == 10^15, x]
(* {} *)

Interestingly, if I run this over a list of different powers many of them are failing (but not all):

Cases[#, {x_, {}}] &@ Table[{i, NSolve[1/x == 10^i, x]}, {i, Range[1, 50]}]
(* {{15, {}}, {18, {}}, {21, {}}, {23, {}}, {24, {}}, {25, {}},
 {26, {}}, {29, {}}, {30, {}}, {32, {}}, {34, {}}, {39, {}},
 {42, {}}, {43, {}}, {45, {}}, {49, {}}} *)

Why is this happening and is there is a generic workaround?

Note: I'm using Mathematica on Linux x86 10.0.2.0

$\endgroup$
  • 1
    $\begingroup$ Huh. If you set WorkingPrecision to anything other than the default value, it works. $\endgroup$ – J. M. will be back soon Apr 25 '16 at 17:57
  • $\begingroup$ I can confirm this behavior on MM 10.2.0.0 on Mac OS. What's more, I get a different set of non-working value if I use (10.0)^i instead: {{15, {}}, {18, {}}, {21, {}}, {24, {}}, {30, {}}, {34, {}}, {37, {}}, {43, {}}, {46, {}}} I wonder if the answer is related to the binary (floating-point) representation of 1/10 or something like that. $\endgroup$ – Michael Seifert Apr 25 '16 at 20:20
  • $\begingroup$ Its not about the exact integer, this gives null results too: Cases[Table[{v, NSolve[1/x == v, x]}, {v, RandomReal[10, 1000] 10^13}], {x_, {}}] $\endgroup$ – george2079 Apr 25 '16 at 20:25
  • $\begingroup$ Simpler is to use Solve, which works fine. $\endgroup$ – David G. Stork Apr 25 '16 at 20:48
3
$\begingroup$

If all your problems are of this sort, simply use Solve. Solve acts by manipulating the symbols, which in these cases is trivial, so that

Solve[1/x == 10^(-50), x]

gives the proper answer directly. Mathematica recognizes this equation as $1/x = a$ and solves, yielding $a = 1/x = 10^{(-(-50))} = 10^{50}$.

In contrast, NSolve performs a numerical solution, and thus at some stage likley exceeding MachinePrecision, leading to numerical errors.

$\endgroup$
  • $\begingroup$ Thanks for your response. Unfortunately, I am unable to use Solve for my actual problem (which is more intricate then the above equation). $\endgroup$ – JeffDror Apr 26 '16 at 2:50
  • $\begingroup$ @Jeff, did you try increasing the setting of WorkingPrecision instead? $\endgroup$ – J. M. will be back soon Apr 26 '16 at 3:04
  • $\begingroup$ I did. While it sometimes fixed the toy problem above (I found it depended on the choice of WorkingPrecision), it didn't fix my actual issue. Not sure if its because there are multiple problems at hand. $\endgroup$ – JeffDror Apr 26 '16 at 3:07
  • 1
    $\begingroup$ Well, @JeffDror, an important lesson: Always ask the simplest version of a problem whose answer will actually help you. It is much better to ask your true question, and I urge you to do so if you want help from us. $\endgroup$ – David G. Stork Apr 26 '16 at 16:12
  • 1
    $\begingroup$ @DavidG.Stork: The reason I like posting questions here is more to better understand Mathematica than to actually solve the problem I have at hand (I can usually find some workaround). In this instance, the answer seems simply to be a immovable bug in Mathematica which is good enough for me. $\endgroup$ – JeffDror Apr 26 '16 at 16:28
2
$\begingroup$

Is there any reason you can't solve for logarithms? If you do, NSolve handles the provided example pretty straightforwardly:

Thread[y -> (Exp[y] /. NSolve[-y == Log[10^(-50)], y])]    
(* {y -> 1.*10^50} *) 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.