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

=================

1

Huh. If you set WorkingPrecision to anything other than the default value, it works.

– J. M.♦

Apr 25 at 17:57

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.

– Michael Seifert

Apr 25 at 20:20

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_, {}}]

– george2079

Apr 25 at 20:25

Simpler is to use Solve, which works fine.

– David G. Stork

Apr 25 at 20:48

=================

2 Answers

2

=================

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=a1/x = a and solves, yielding a=1/x=10(−(−50))=1050a = 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.

Thanks for your response. Unfortunately, I am unable to use Solve for my actual problem (which is more intricate then the above equation).

– JeffDror

Apr 26 at 2:50

@Jeff, did you try increasing the setting of WorkingPrecision instead?

– J. M.♦

Apr 26 at 3:04

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.

– JeffDror

Apr 26 at 3:07

1

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.

– David G. Stork

Apr 26 at 16:12

1

@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.

– JeffDror

Apr 26 at 16:28

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} *)