1
$\begingroup$
NSolve[Power[x + 1, x + 1] - Power[x, x + 2] == 0, x, Reals]

returns:

{{x -> 3.14104}}

Great.

However,

NSolve[Power[x + 1.5, x + 1.5] - Power[x, x + 3] == 0, x, Reals]

runs forever. :(

Now,

NSolve[Power[x + 2, x + 2] - Power[x, x + 4] == 0, x, Reals]

returns

{{x -> 3.47175}}

Again great.

I expected "2.5" case would run forever, just like "1.5" case. But:

NSolve[Power[x + 2.5, x + 2.5] - Power[x, x + 5] == 0, x, Reals]

Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result. >>

and outputs the answer:

{{x -> 3.61691}}


Since the solution is in real numbers, from numerical math point of view, all cases above are equal. Why such behavior of NSolve[] then?

$\endgroup$
  • $\begingroup$ Your cases are not all equal. Plot, for example, your two first cases: Plot[{Power[x + 1, x + 1] - Power[x, x + 2], Power[x + 1.5, x + 1.5] - Power[x, x + 3]}, {x, 0, 3.5}] and you will see that the curves do not have the same shape. $\endgroup$ – 2012rcampion May 15 '15 at 21:23
  • $\begingroup$ @2012rcampion the curves are not literally equal, but they do not differ "topologically" if I may say $\endgroup$ – VividD May 15 '15 at 21:26
  • $\begingroup$ From numerical math point of view, they are almost the same, just one is steeper, and that should not be a problem for numerical algorithms for finding roots at all @2012rcampion $\endgroup$ – VividD May 15 '15 at 21:29
2
$\begingroup$

In the NSolve rather than just limiting x to Reals, limit it to a finite interval of interest, say 2 < x < 6

f[x_, a_] = Power[x + a, x + a] - Power[x, x + 2 a];

data = Table[
   {a, x /. NSolve[{f[x, a] == 0, 2 < x < 6}, x][[1]]},
   {a, 1/10, 10, 1/10}];

ListLinePlot[data, AxesLabel -> {"a", "root"}]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks, two great ideas in one answer (restricting root interval, and plotting root behavior dependant on parameter a)! $\endgroup$ – VividD May 26 '15 at 18:38
4
$\begingroup$

tl;dr Don't expect NSolve to give sensible results for equations with no analytic solution.

If you are interested in a "numerical math point of view", use FindRoot or a related function to solve numerically. NSolve tries to find a transformation to an equation it knows exact solutions for, then converts those solutions to machine-precision numbers when returning them.

For example, take the equation x - Sin[x] == 1:

NSolve[x - Sin[x] == 1, x]
(* NSolve::nsmet: This system cannot be solved with the methods available to NSolve. >> *)

FindRoot[x - Sin[x] == 1, {x, 1}]
(* {x -> 1.93456} *)

Your equations look different to NSolve. Note that it first converts approximate to exact numbers (hence the message "solving a corresponding exact system"), so that your troublesome equation is being treated as:

SetPrecision[Power[x + 1.5, x + 1.5] - Power[x, x + 3] == 0, Infinity]

$$ \left(x+\frac{3}{2}\right)^{x+\frac{3}{2}}=x^{x+3} $$

I don't know why NSolve/Solve get hung up on this particular equation, it probably makes a bad guess early on and misses the transformation it used on the other ones.


Just to demonstrate that Mathematica is handling your equations differently, we can look at the output of Solve instead of NSolve:

Solve[Power[x + 1, x + 1] - Power[x, x + 2] == 0, x, Reals]

$$ \text{Root}\left[\left\{(\text{$\#$1}+1) \left(\frac{\text{$\#$1}+1}{\text{$\#$1}}\right)^{\text{$\#$1}}-\text{$\#$1}^2\&,3.141\ldots\right\}\right] $$

Solve[SetPrecision[Power[x + 2.5, x + 2.5] - Power[x, x + 5] == 0, 
  Infinity], x, Reals]

$$ \text{Root}\left[\left\{\frac{25 \sqrt{2 \text{$\#$1}+5} e^{\text{$\#$1} \log \left(\text{$\#$1}+\frac{5}{2}\right)-\text{$\#$1} \log (\text{$\#$1})}}{\text{$\#$1}^5}+\frac{20 \sqrt{2 \text{$\#$1}+5} e^{\text{$\#$1} \log \left(\text{$\#$1}+\frac{5}{2}\right)-\text{$\#$1} \log (\text{$\#$1})}}{\text{$\#$1}^4}+\frac{4 \sqrt{2 \text{$\#$1}+5} e^{\text{$\#$1} \log \left(\text{$\#$1}+\frac{5}{2}\right)-\text{$\#$1} \log (\text{$\#$1})}}{\text{$\#$1}^3}-4 \sqrt{2}\&,3.616\ldots\right\}\right] $$

Very different results...

$\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.