9
$\begingroup$

I have something like

FindRoot[f[x], {x, a}]

Now I want FindRoot to constrain the solutions to 0 < x < 1. How can I obtain this?

$\endgroup$
2
  • $\begingroup$ The easiest (and probably not the fastest) way for you might be to Select the solutions given by FindRoot. $\endgroup$
    – Öskå
    Mar 13, 2013 at 14:44
  • $\begingroup$ Maybe something like Cases[FindRoot[...],Rule[_,_>0]..&&Rule[_,_<1].. (I'm not sure whether the pattern is the best to filter solutions). $\endgroup$
    – Sos
    Mar 13, 2013 at 14:50

4 Answers 4

18
$\begingroup$

I would call these constraints, not assumptions.

From the docs,

FindRoot[lhs==rhs,{$x$, $x_{\text{start}}$, $x_{\text{min}}$, $x_{\text{max}}$}] searches for a solution, stopping the search if x ever gets outside the range $x_{\text{min}}$ to $x_{\text{max}}$

Keep in mind that FindRoot uses iterative numerical methods such as Newton's method or Brent's method which will converge to a single solution, but will not find all solutions. What this syntax does is simply stop the iteration as soon as $x$ gets outside of the specified range. If this happens, it does not mean that there are no solutions inside that range.

Here's a concrete example where there are several roots, but the search stops as soon as the method reached the edge of the search region:

In[2]:= FindRoot[Sin[x], {x, 1, .1, 10}]

During evaluation of In[2]:= FindRoot::reged: The point {0.1} is at the edge of the
   search region {0.1,10.} in coordinate 1 and the computed search direction points 
   outside the region. >>

Out[2]= {x -> 0.1}

If you need to find all roots inside an interval, I'd recommend using Reduce which will often work (if using Mathematica 7 or later). Note that while Reduce may not be able to find solution for the general case, it will very often work if you restrict the search domain to a real interval. Even for hard problems with transcendental functions.

$\endgroup$
6
  • $\begingroup$ Although if I stick with my f[x_]:=Sin[x*10] and if I use FindRoot[f[x] == 0, {x, 0, 0, 1}] the only answer it gives is 0.. Any clue why? $\endgroup$
    – Öskå
    Mar 13, 2013 at 16:23
  • $\begingroup$ @Öskå I explained that in this answer. FindRoot always returns only a single answer (for a single initial condition). $\endgroup$
    – Szabolcs
    Mar 13, 2013 at 16:40
  • $\begingroup$ @Szabolcs Thanks. But now I remain with the problem that the function returns the value of the extreme to which the iteration leads. I wish that I'd get no value at all, if there was no root. Obviously, however, I can not a priori exclude extreme values​​, since there may be the case that these are effectively a solution. Any suggestions? $\endgroup$
    – psmith
    Mar 16, 2013 at 21:39
  • 1
    $\begingroup$ @Fabrizio I just checked and FindRoot issues a message if it reaches the end. You can Check for this message. $\endgroup$
    – Szabolcs
    Mar 16, 2013 at 22:52
  • 1
    $\begingroup$ @Szabolcs Ok. Is this possible to use a similar strategy when I using FindRoot[{f(x,y),g(x,y)},{x,x0},{y,y0}] and I want that x>y? $\endgroup$
    – psmith
    Mar 21, 2013 at 13:07
8
$\begingroup$

If you already know the interval on which you want to find one of your solution, you may use the instruction

FindRoot[f[x]==0,{x,xmin,xmax}]

Here, Mathematica will use Brent's algorithm (a combination of the bisection and secant methods) restricted to the interval [xmin,xmax]. With the example

FindRoot[Sin[x]==0, {x, .1, 10}]

where one searches for a solution in [0.1,10], the algorithm does not fail and leads to

{x -> 9.42478}

As in all finding-roots methods, Mathematica only find one solution (if it exists) on the interval, even if multiple solutions may exist.

$\endgroup$
4
  • $\begingroup$ "dichotomy search" - you meant bisection, yes? It actually uses Brent's method, but it is true that bisection is one of the key ingredients of Brent… $\endgroup$ Jun 18, 2015 at 13:20
  • $\begingroup$ @Guesswhoitis. Yes you are right (see documentation.), I updated my answer accordingly. $\endgroup$
    – jibe
    Jun 18, 2015 at 13:39
  • $\begingroup$ It's actually more like "a combination of the bisection and secant methods with inverse quadratic interpolation", but that's a mouthful to say. Nevertheless, there's a reason why "Algorithms for Minimization Without Derivatives" is a classic book… $\endgroup$ Jun 18, 2015 at 13:46
  • $\begingroup$ I believe that you need the form {x, x0, xmin, xmax} instead of {x, x0, x1} $\endgroup$
    – Eric Brown
    Jul 18, 2021 at 0:42
3
$\begingroup$

Something like that works, more advanced answers should come from others.

f[x_] := Sin[x*10]

Mathematica graphics

Sort@DeleteDuplicates[Select[FindRoot[f[x], {x, Range[0, 1, 0.1]}][[1,2]], (0 <= # < 1)&],
Abs[#2 - #1] < 10^-8 &]

Gives:

{0., 0.314159, 0.628319, 0.942478}

$\endgroup$
3
$\begingroup$

This is one of the simpler ways to do it:

Solve[f[x] == 0 && 0 < x && x < 1, x]

Specifying conditions within Solve or any other function you are using is more efficient than playing Select on the results. This way, Mathematica knows where to look for solutions and only finds those within your constraints.

$\endgroup$
4
  • $\begingroup$ But I need specifically FindRoot for solve my problem, and this doesn't work in that case. $\endgroup$
    – psmith
    Mar 13, 2013 at 15:41
  • $\begingroup$ And why do you specifically need FindRoot? $\endgroup$
    – cartonn
    Mar 13, 2013 at 15:46
  • $\begingroup$ because my problem is very hard, it also contains transcendental functions, so Solve doesn't work in this case. $\endgroup$
    – psmith
    Mar 14, 2013 at 8:21
  • $\begingroup$ @Fabrizio Reduce will often work for these kinds of problems if you also supply an interval. $\endgroup$
    – Szabolcs
    Mar 17, 2013 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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