I have the following simple command to solve for h in an equation:

Solve[(1 - 2.25577*^-5*h)^5.25588 == 0.9644952131579817, h]

This works just fine, but throws the warning that inverse functions are used so some solutions may not be found. Not a problem.

However, if I use NSolve, Literally by just adding an N in front of Solve, it takes forever, and I end up aborting it because it takes too long.

Does anyone know why exactly this is happening?

I'm using Mathematica 10.0 Student Edition on a Windows 8.1 system.

  • $\begingroup$ Could you please share the version you are using, as well as the platform. $\endgroup$ – Sasha Nov 11 '14 at 3:59
  • 1
    $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Nov 11 '14 at 4:07
  • $\begingroup$ Updated my initial post. $\endgroup$ – pragmatist1 Nov 11 '14 at 4:12
  • $\begingroup$ Find root works works well for this equation. FindRoot[(1. - (2.25577*^-5) h)^5.2558 == 0.9644952131579817, {h, 300}] returns {h -> 303.869} essentially instantaneously. $\endgroup$ – m_goldberg Nov 11 '14 at 4:27
  • $\begingroup$ I think the way you wrote your equation is not clear. It should be written as (1 - 2.25577*10^(-5)*h) $\endgroup$ – Nasser Nov 11 '14 at 4:31

It is probably because it is solving a degree 131397 equation:

(1 - 2.25577*^-5*h)^5.25588 == 0.9644952131579817 // Rationalize[#, 0] &

(*  (1 - (225577 h)/10000000000)^(131397/25000) == 79369373/82291101  *)

Simpler comparison, to show equivalence with a rationalized equation:

s1 = NSolve[(1 - 2.25577*^-5*h)^5.30 == 0.9644952131579817 // Rationalize[#, 0] &, h];
s2 = NSolve[(1 - 2.25577*^-5*h)^5.30 == 0.9644952131579817, h];
s1 == s2

(*  True  *)

Update - Remarks: What I've gleaned from the documentation and this site is that NSolve is based on algorithms for solving polynomial systems. It can be used to solve systems that can be converted to polynomial systems, such as the OP's equation with rationalized coefficients and power. It can be converted to a degree 131397 polynomial equation, with a RHS involving some pretty large integers, probably another factor in the slowness. One would expect that many of the 131397 solutions to the polynomial equation would be extraneous. Note: A recent improvement has extended the capabilities of NSolve and Solve to transcendental equations over a bounded domain; e.g., NSolve[Erfc[x] == BesselJ[1, x] && 0 < Re@x < 5 && 0 < Im@x < 5, x].

NSolve to my mind is not the numeric analog to a symbolic Solve. NSolve is more specialized. Further NSolve I believe will find all roots, and it will find all real roots if we specify the domain Reals with NSolve[eqn, h, Reals]. Solve is content to return just one in the OP's case. There is a big difference in verifying that all solutions have been found and that one has been found.

In the case where one has a numeric equation with a single real root, NSolve seems the wrong tool to me. FindRoot would be my first thought. But clearly, Solve turns out to be a good choice here. Knowing that Solve would use inverse functions, one can suppress the message with

Quiet[Solve[(1 - 2.25577*^-5*h)^5.25588 == 0.9644952131579817, h], Solve::ifun]
  • $\begingroup$ fyi, I tried this on Maple, using fsolve, which is the Mathematica version of NSolve, and Maple solved it instantly. Screen shot: !Mathematica graphics I wonder if this mean Maple is using some other fast method to numerically solve this. $\endgroup$ – Nasser Nov 11 '14 at 4:36
  • $\begingroup$ @Nasser Perhaps fsolve is not equivalent to NSolve. On my simpler example, NSolve gives all 5 solutions, one real, four complex: image. Perhaps fsolve is more akin to Solve -- Solve returns a single solution fairly quickly (in both cases). $\endgroup$ – Michael E2 Nov 11 '14 at 12:59
  • $\begingroup$ fyi, Maple fsolve says " The fsolve command numerically computes the zeroes of one or more equations, expressions or procedures." so I assumed it is like NSolve. maplesoft.com/support/help/maple/view.aspx?path=fsolve $\endgroup$ – Nasser Nov 11 '14 at 20:28
  • $\begingroup$ @Nasser It seems close, though not identical, if you look under "Output." I don't know whether the differences have much significance. $\endgroup$ – Michael E2 Nov 12 '14 at 2:09

That's quite usual for NSolve. From my experience I see this as follows:

NSolve is a very special function which uses numerical methods for finding approximate roots of ONLY linear, 'usual' polynomial, simple trigonometric, etc. equations. If the equation's simple enough then NSolve will give you all roots without any guesses from your side - that is the great advantage. But if equation is a bit heavier - you can only use common numerical methods, implemented in FindRoot with starting points, etc. And I'm sad to admit - NSolve works often really worse for some reason comparing to its analogs in Matlab, Mathcad and Maple.

On the other hand Solve doesn't use approximal numerical methods, but accurately solves equations of different types with appropriate methods.

You are in situation, when the equation CAN be accurately solved, but methods of NSolve are not good.

  • $\begingroup$ It appears there's another function FindInstance, which solves the equation accurately and clean. $\endgroup$ – funnyp0ny Nov 11 '14 at 9:46

The solution, given by Solve, ist 303.865. This is quite near to a zero of the derivative of the original function (which can easily be calculated). So when finding the root numerically, using Newton´s method, this will clearly slow down the calculation or even make it impossible to find such a solution

  • $\begingroup$ Why do you think that NSolve uses Newton's method at all? This method requires starting point while NSolve doesn't. And it solves similar equation (with integer power 5) immediately - (1 - 2.25577*^-5.*h)^5 == 0.9644952131579817 $\endgroup$ – funnyp0ny Nov 11 '14 at 9:17
  • $\begingroup$ I do not think so, I just wanted to point out that this could lead to longer run-time and that we here have the case of very small value of f´ near the zero. $\endgroup$ – mgamer Nov 11 '14 at 9:37
  • $\begingroup$ The zero of the derivative is around h == 44330.8. What measure are you using to claim this is close to h == 303.865? $\endgroup$ – Michael E2 Nov 11 '14 at 15:00

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.