Find_root not finding a root

94 views
Skip to first unread message

saad khalid

unread,
Mar 1, 2018, 11:45:31 PM3/1/18
to sage-support
Hey everyone:

I'm running this code:
find_root(e^(-2*x*1)-(1 - 4*x),-2,2)

It returns
2.4011774461136836e-13

which is approximately 0. However, there should be another root around x = -0.628. Why isn't it finding this root?
Is there any way I can make sure it finds all of them?


Thanks

Simon King

unread,
Mar 2, 2018, 1:17:53 AM3/2/18
to sage-s...@googlegroups.com
Hi Khalid,
I suppose you know that in Python (which is the underlying language
of SageMath), you can access the documenation of an object by putting a
question mark after it. So, do

sage: find_root?

and you can read:
Numerically find a root of "f" on the closed interval [a,b] (or
[b,a]) if possible, where "f" is a function in the one variable.
Note: this function only works in fixed (machine) precision, it is
not possible to get arbitrary precision approximations with it.

So, it says *a* root, not *all* roots. The solution returned above
obviously is in the given intervall, and so SageMath's answer complies
with the specification.

If you want other solutions, you need to provide a smaller intervall:

sage: f(x) = e^(-2*x*1)-(1-4*x)
sage: find_root(f, -1,0)
0.0
sage: f(x=0)
0

--> one gets yet another solution, the intervall is still too large. Again
smaller:

sage: find_root(f, -1,-0.5)
-0.6282156043130847
sage: f(x=_)
-4.440892098500626e-16

And that was what you were looking for.

Best regards,
Simon


saad khalid

unread,
Mar 5, 2018, 2:01:59 PM3/5/18
to sage-support
Hello, and thank you for your response. While I agree that the behaviour of the function certainly complies with the specifications listed in its description, I think everyone would agree that it would be better if it did give all of the roots in a given interval. Would you happen to know anything about the difference between the alg Mathematica uses vs the alg Sage uses that lets Mathematica find the root without making the interval so precise?

Thank you!

Vincent Delecroix

unread,
Mar 5, 2018, 6:14:05 PM3/5/18
to sage-s...@googlegroups.com
Hello,

I don't agree. There might be infinitely many roots (just take
the function x * sin(1/x) in the interval [-1,1]).

There is a slight difference between root *isolation* and root
*approximation*. Since you are not providing the Mathematica code
you are using it is hard to tell anyway.

Best
Vincent

saad khalid

unread,
Mar 14, 2018, 10:25:00 PM3/14/18
to sage-support
That is a good point, however I feel an even better solution in that case would be giving some of the roots and then giving some indication that there are an infinite number of roots. Disregarding the case where there are infinitely many roots, I don't see why it wouldn't be preferable to have the default behavior to be to give all the roots, or even just give the option to get all roots even if it is not the default. It just seems like useful functionality. What are the limitations towards implementing something like this?

The Mathematica code was simply:
Solve[ Exp[-2*a*x]-4*a*x==0,x]//N

Dima Pasechnik

unread,
Mar 15, 2018, 10:57:09 AM3/15/18
to sage-support


On Thursday, March 15, 2018 at 2:25:00 AM UTC, saad khalid wrote:
That is a good point, however I feel an even better solution in that case would be giving some of the roots and then giving some indication that there are an infinite number of roots. Disregarding the case where there are infinitely many roots, I don't see why it wouldn't be preferable to have the default behavior to be to give all the roots, or even just give the option to get all roots even if it is not the default. It just seems like useful functionality. What are the limitations towards implementing something like this?

it could be a different function, which potentially would run much longer, by repetitive splitting of the interval
(I guess that's what Mathematica is doing)  

The Mathematica code was simply:
Solve[ Exp[-2*a*x]-4*a*x==0,x]//N


What is the result of such a Mathematica command on an example with infinitely many roots given in this thread?

Robert Dodier

unread,
Mar 15, 2018, 4:31:14 PM3/15/18
to sage-s...@googlegroups.com
On 2018-03-15, Dima Pasechnik <dim...@gmail.com> wrote:

> it could be a different function, which potentially would run much longer,
> by repetitive splitting of the interval
> (I guess that's what Mathematica is doing)

I have toyed with the idea of repurposing whatever adaptive splitting
code is in the plotting functions to finding initial intervals for the
1-d numerical root finder. There is a similar need in the plotting code
to try to assess the wiggles of the function in order to judge if the
plot is smooth enough. In any event, reusing the plotting code's
splitting algorithm would at least mean that a multi-root finder would
find the same roots as a human inspecting a plot and calling the root
finder accordingly. (I'm guessing that look at plot + call root finder
is the most common heuristic; I don't really have any evidence for
that.)

Maxima, which I'm familiar with, has its own plotting code, dunno about
Sage, in any event just reusing the splitting algorithm isn't any big
deal.

best,
Robert Dodier

saad khalid

unread,
Mar 15, 2018, 6:23:44 PM3/15/18
to sage-support
I have to apologize, I gave a slightly incorrect Mathematica code earlier, the actual code was:

Solve[ Exp[-2*a*x]-1+4*a*x==0,x]//N

The earlier code gave the wrong answer. Anyways:


it could be a different function, which potentially would run much longer, by repetitive splitting of the interval
(I guess that's what Mathematica is doing) 

There appear to be 2 main solving functions in Mathematica, "Solve" and "Reduce," the differences are explained in this well written answer:
https://mathematica.stackexchange.com/questions/17127/what-is-the-difference-between-reduce-and-solve


What is the result of such a Mathematica command on an example with infinitely many roots given in this thread?


So, the Solve function doesn't find any it seems but it tells you to use the Reduce function, which I think does give all the solutions.
Auto Generated Inline Image 1

Dima Pasechnik

unread,
Mar 15, 2018, 6:56:54 PM3/15/18
to sage-support
OK, that was too easy, as there was an exact symbolic solution.
How about solving sin(1/x)==x on [-1,1] instead?
 

saad khalid

unread,
Mar 15, 2018, 11:24:20 PM3/15/18
to sage-support


OK, that was too easy, as there was an exact symbolic solution.
Is this a solution Sage could have given symbolically as well?

Here are the results for sin(1/x)==x on [-1,1] :

It apparently computed many solutions and originally asked me whether I wanted to show all the found solutions or just some of them, and I pressed show all. It also tells you that it "couldn't prove that these are all of the solutions." There are many more solutions given however these are all I could fit on the screen.
Auto Generated Inline Image 1

Dima Pasechnik

unread,
Mar 16, 2018, 5:31:33 AM3/16/18
to sage-support
This makes sense. It most probably has a pre-set minimal interval length, so it stops splitting at certain moment.

saad khalid

unread,
Mar 19, 2018, 4:45:53 PM3/19/18
to sage-support
So what is likely the difference between how Sage solves this problem and how Mathematica solves this problem that makes Mathematica show more solutions?

Marcin Kostur

unread,
Mar 23, 2018, 4:14:50 AM3/23/18
to sage-support
Dear Saad,

It happens that I want to find more than one root numerically, then I use brute force as in code below. It is not possible to 
have any general heuristics for arbitrary function about where its roots are, so if one knows more about the function in some special case
 then the interval generation can be adapted to reflect this knowledge.

zeros = []
xmin = -1
xmax = 1
f = sin(1/x)-x
intv = srange(xmin,xmax,(xmax-xmin)/150)
for x1,x2 in zip(intv,intv[1:]):
    try:
        rt = find_root(f,x1,x2 )
        zeros.append(rt)
    except:
        pass

That is all.

mk
Reply all
Reply to author
Forward
0 new messages