python - How to find a root for a mathematical function using Intermediate value theorem? -


accroding intermediate value theorem given function f(x), i'm supposed write function, gets mathematical function, 2 numbers , b , , error range, , gives output number x functions' value close 0 epsilon. examples:

>>> find_root(lambda x : x - 1 , -10, 10) 1.0009765625  >>> find_root(lambda x : x**2 , -10, 10) >>> #returned none 

this code wrote far, think i'm on right way, can't figure out loop over, don't correct answer code. should fix in it?

def find_root(f, a, b, eps=0.001):     if (f(a)*f(b))<0:         in range(a,b):             if f(i)<eps:                 return (i)     else:         return (none) 

use dichotomy :

def find_root(f, a, b, eps=0.0001):   if f(a)==0 : return   if f(b)==0 : return b   if f(a)*f(b)>0 : return none   c=(a+b)/2   while(abs(f(c))>eps) :     if f(a)*f(c)<0 : b=c     else : a=c     c=(a+b)/2   return c 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -