# find_root return values

Hi there!

What are return values of find_root in the case when there is no root in the interval?

Thanks!

Archie

find_root return values

**
asked 2013-07-03 08:59:34 +0200 **

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hi there!

What are return values of find_root in the case when there is no root in the interval?

Thanks!

Archie

add a comment

4

When there is no root in the given interval, the function `find_root()`

does not return anything but raises a `RuntimeError`

exception:

```
sage: f(x) = 2 ; f
x |--> 2
sage: find_root(f,-10,10)
...
RuntimeError: f appears to have no zero on the interval
```

If you want to deal with that, you have to catch the error (otherwise your program will stop here):

```
try:
root = find_root(f,-10,10)
root_exist = True
except RuntimeError:
root_exist = False
```

You can read more here.

1

**
answered 2013-07-03 14:31:47 +0200 **

This post is a wiki. Anyone with karma >750 is welcome to improve it.

See also a recursive algorithm in:

http://ask.sagemath.org/question/1327/obtaining-all-numerical-roots-of-a-function-in-an

0

**
answered 2013-07-03 10:38:03 +0200 **

This post is a wiki. Anyone with karma >750 is welcome to improve it.

```
def find_all_roots(func,a,b,nsteps):
roots=[]
n=0
x0=a #initializations
step=(b-a)/(nsteps) #size of a sub-interval
#the rest is quite self-explanatory
while (n<nsteps):
try:
roots.append(find_root(func,x0,x0+step))
root_exist = True
except RuntimeError:
root_exist = False
x0+=step
n+=1
roots.sort()
return roots
```

Just in case you are interested; there is a root function which does this already with a lot higher precision and more options. def find_all_roots(func,a,b): assume( x>=a , x<=b) try: roots = [cp for cp in (func==0).roots(x, ring=RR, multiplicities=False)] except: roots = [ ] return roots find_all_roots((x+x^2-10), -10,10) returns: [-3.70156211871642, 2.70156211871642] find_all_roots((x^2+1), -10, 10) returns: [ ] (sry about the bad spacing, not sure how to format the text here yet)

0

**
answered 2013-07-03 09:17:39 +0200 **

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I think it raises an error, but surely you can check this very quickly by trying to find a root of `x^2 + 1`

.

-1

**
answered 2013-07-03 10:36:25 +0200 **

This post is a wiki. Anyone with karma >750 is welcome to improve it.

def find_all_roots(func,a,b,nsteps):

roots=[]

n=0
x0=a #initializations
step=(b-a)/(nsteps) #size of a sub-interval
#the rest is quite self-explanatory
while (n<nsteps): try:="" roots.append(find_root(func,x0,x0+step))="" root_exist="True" except="" runtimeerror:="" <br="">
root_exist = False
x0+=step
n+=1
roots.sort()
return roots

Please start posting anonymously - your entry will be published after you log in or create a new account.

Asked: ** 2013-07-03 08:59:34 +0200 **

Seen: **2,098 times**

Last updated: **Jul 03 '13**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.