title: Finding root/zeros of a function
body: <p>I am trying to find a root of a function by using the bisection method stating that :</p>
  
  <pre><code>if f(a)*f(b) &lt; 0 then a root exists, 
  then you repeat with f(a)*f(c)&lt;0 where c = (a+b)/2       
  </code></pre>
  
  <p>but im not sure how to fix the code so it works properly.
  This is my code but its not working properly</p>
  
  <pre><code>from scipy import *
  from numpy import *
  
  
  def rootmethod(f, a, b, tol):
  
  
      x = a
      fa = sign(eval(f))
  
      x = b
      fb = sign(eval(f))
  
      c = a + b
      iterations = 0
  
      if fa == 0:
          return a
      if fb == 0:
          return b
  
      calls = 0         
      fx = 1
  
      while fx != 0:
          iterations = iterations + 1
          c *= 0.5
          x = a + c
          fc = sign(eval(f))
          calls = calls + 1
  
          if fc*fa &gt;= 0:
              x = a
              fx = sign(eval(f))
          if fc == 0 or abs(sign(fc)) &lt; eps:
              fx = sign(eval(f))
              return x, iterations, calls
  
  
  
  
  
  print rootmethod("(x-1)**3 - 1", 1, 3, 10*e-15)
  </code></pre>
  
  <p>New edit.. but still doesnt work</p>
  
  <pre><code>   if fa*fb &lt; 0:
  
          while fx != 0:
              iterations = iterations + 1
              c = (a + b)/2.0
              x =  c
              fc = sign(eval(f))
              calls = calls + 1
  
              if fc*fa &gt;= 0:
                  x = c
                  fx = sign(eval(f))
              if fc == 0 or abs(sign(fc)) &lt; tol:
                  fx = sign(eval(f))
                  return x, iterations, calls
  </code></pre>
  
  <p>Edit: Changed c=(a+b)*2 to c=(a+b)/2 in the description of the method.</p>