2
$\begingroup$

Consider the following function:

$$ f(x)=\beta~e^{-x^2}-\frac{\beta +e}{e~x}+1~, $$

where $\beta$ is some parameter and $e$ represents the Euler constant.

I've been trying to get Mathematica to find the roots, using

FindRoot[1 + β E^-x^2 - (E + β)/(E x), {x, 1.4}]

However there seems to be some problem when, for instance, $\beta=2.3$, since FindRoot outputs $\text{x=1.32902}$ but the function is only zero, for that value of $\beta$, when $x=0$.

What am I doing wrong?

Edit: The answers by David Keith and John Doty are absolutely correct. However it seems I have not formulated the question in the right way. My problem is not that with that code Mathematica cannot find the right answer (gets stuck in a local minimum) but the fact that it outputs an incorrect answer without showing some kind of error message. I'm using version 11.0.

$\endgroup$
6
  • $\begingroup$ Please add your code in copyable form (not as image) to your post so that we can reproduce the error. $\endgroup$ Apr 5, 2018 at 21:02
  • $\begingroup$ @HenrikSchumacher Sorry for the inconvenience. Mathematica.SE was complaining about code formatting and the only way I got it to post the question was by removing all the code. I think now it works. $\endgroup$
    – PML
    Apr 5, 2018 at 21:17
  • $\begingroup$ Sometimes, reformulating the the function helps. Multiplying 1 + β E^-x^2 - (E + β)/(E x) by x introduces an artificial root at 0 but FindRoot[E x (1 + E^-x^2 \[Beta]) == E + \[Beta], {x, 1.4}] works nicely... $\endgroup$ Apr 5, 2018 at 21:39
  • $\begingroup$ @MichaelE2 The point of the whole question was that I did(do) not receive the error message in Mathematica 11.0. In the edit paragraph is explained. I'll remove your edit $\endgroup$
    – PML
    Apr 6, 2018 at 12:14
  • $\begingroup$ @PML Oh, sorry. I don't have 11.0, but 10.4.3 and 11.2, 11.3 each give the error message. Unless you've done something to your 11.0 (turned off the error message, changed the defaults for FindRoot, etc.), it looks like a bug that came and went. Maybe someone with 11.0 will come along and verify. (I still think it might be better to explicitly mention FindRoot::lstol, which should have been emitted.) $\endgroup$
    – Michael E2
    Apr 6, 2018 at 12:23

3 Answers 3

2
$\begingroup$

Choose a better starting point. Your function has a minimum around 1.3, so FindRoot finds that and doesn't look farther.

FindRoot[1 + \[Beta] E^-x^2 - (E + \[Beta])/(E x), {x, 0.5}]
(* {x -> 1.} *)

In Mathematica 11.3, it reports a FindRoot::lstol message for an initial starting point of 1.4 before returning the x value for the minimum.

$\endgroup$
7
  • $\begingroup$ But it's not a root...For $\beta<2.3$ it has no problems going all the way to $x=1$ $\endgroup$
    – PML
    Apr 5, 2018 at 21:31
  • $\begingroup$ x -> 1. certainly is a root. 1 + \[Beta] E^-x^2 - (E + \[Beta])/(E x) /. x -> 1. yields -2.22045*10^-16, zero within rounding error. For an initial guess of 1.4, I get a FindRoot::lstol error message. FindRoot uses Newton's method, and is thus sensitive to your initial guess. $\endgroup$
    – John Doty
    Apr 5, 2018 at 21:38
  • $\begingroup$ Of course $x=1$ is a root. What I meant is that for my starting point FindRoot gets stuck in a local minimum but that minimum is not a root $\endgroup$
    – PML
    Apr 5, 2018 at 21:40
  • $\begingroup$ That's the nature of the method. It can get stuck.And it complains with an error message, as it should. $\endgroup$
    – John Doty
    Apr 5, 2018 at 21:42
  • $\begingroup$ Oh I missed your point: it complains that it can't find a root in your case. In my case - with the code that I posted - it simply returns that output. No error message. I've checked the "messages board" and there's nothing there. What version are you using. I'm using 11.0 $\endgroup$
    – PML
    Apr 5, 2018 at 21:46
5
$\begingroup$

The Newton method gets stuck at a local minimum. Try this:

In[1]:= exp = 1 + \[Beta] E^-x^2 - (E + \[Beta])/(E x);

In[2]:= sol = 
  FindRoot[exp /. \[Beta] -> 2.3, {x, .5, 2}, Method -> "Brent"];

In[3]:= exp /. \[Beta] -> 2.3 /. sol

Out[3]= 8.8773433*10^-13
$\endgroup$
4
  • $\begingroup$ Note that the default method also works here if given {x, .5, 2} as the initial guess. $\endgroup$
    – John Doty
    Apr 5, 2018 at 21:51
  • $\begingroup$ Thanks, John. I did not try that. I used Brent’s method because it will maintain a bracket and not stall in a local min. $\endgroup$ Apr 5, 2018 at 22:01
  • $\begingroup$ It appears that FindRoot defaults to Brent's method when given two starting points. Using EvaluationMonitor shows an identical trajectory. $\endgroup$
    – John Doty
    Apr 5, 2018 at 22:23
  • $\begingroup$ That makes sense. Newton’s method really can’t make use of two starting points. $\endgroup$ Apr 5, 2018 at 23:13
0
$\begingroup$

ContourPlot shows two solution-branches and bifurcation:

pic=ContourPlot[0 == 1 + \[Beta] E^-x^2 - (E + \[Beta])/(E x), {\[Beta], 0, 5}, {x,0, 3}, FrameLabel -> {\[Beta], x}, MaxRecursion -> 5]  

enter image description here

Obviously the bifurcation might be the reason why FindRoot fails to find all solutions!

A very easy way to solve the equation is to solve for \[Beta][x] as a function of x (analytically)

erg\[Beta] =Solve[0 == 1 + \[Beta] E^-x^2 - (E + \[Beta])/(E x),\[Beta]][[1]]
(*{\[Beta] -> (E^(1 + x^2) (-1 + x))/(E^x^2 - E x)}*)

\[Beta]x = Table[{\[Beta], x} /. erg\[Beta], {x, 0.5, 3, .2}]
(* {{23.2332, 0.5}, {4.92134, 0.7}, {3.0776, 0.9}, {2.50863,1.1}, {2.34368,1.3}, {2.38344, 1.5}, {2.56035, 1.7}, {2.84377,1.9}, {3.21305, 2.1}, {3.64878, 2.3}, {4.13162, 2.5}, {4.64434,2.7}, {5.17382, 2.9}} *)

Show[{pic, Graphics[{Red, PointSize[Large], Point[\[Beta]x]}]}]

enter image description here

answer The point{\[Beta] = 2.338 , x = 1.349 } is a solution, so FindRoot's answer is correct!

$\endgroup$
2
  • $\begingroup$ Of course your text is correct, but it is not an answer to my question. Correctly you say that for that value of $\beta$ there are two roots, but my point was, for smaller values of $\beta$ there is only one root, and yet FindRoot still outputted a result without throwing an error (being stuck in a minimum) $\endgroup$
    – PML
    Apr 6, 2018 at 10:26
  • $\begingroup$ If I try, as you suggest in your question, With[{\[Beta] = 2.3}, FindRoot[1 + \[Beta] E^-x^2 - (E + \[Beta])/(E x), {x, 1.4}]] FindRoot gives an error FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. I believe this is correct, because solutions x (beside x==1) only exist if \[Beta]>2.33815 $\endgroup$ Apr 6, 2018 at 10:41

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.