Ask Your Question

Archie's profile - activity

2014-06-29 11:09:55 +0200 received badge  Popular Question (source)
2014-06-29 11:09:55 +0200 received badge  Notable Question (source)
2014-06-29 11:09:55 +0200 received badge  Famous Question (source)
2013-07-03 10:38:03 +0200 answered a question find_root return values
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
2013-07-03 10:36:25 +0200 answered a question find_root return values

I just wanted to write a piece of code to find ALL roots in given interval by dividing it into small subintervals. I found two difficulties: 1. pay attention if your function makes sense on boarders of the interval you plan to scan (solution -- just pay attention to it); 2. handling the situation when there is no root (solved by the comment of tmonteil, thanks!). The code is primitive and surely not optimal, but it does the job. Find it attached. Good luck! Archie

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

2013-07-03 09:22:36 +0200 commented answer find_root return values

Great answer! Exactly what I wanted, thank you very much! I will post my naive piece of code for finding all roots in given interval. Just in case anyone likes it. Best...

2013-07-03 09:21:15 +0200 received badge  Supporter (source)
2013-07-03 08:59:34 +0200 asked a question 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