# -*- coding: utf-8 -*- # # bisseccao.py # # # Autor: Pedro Garcia Freitas # License: Creative Commons # License: Creative Commons """ x = 0 iterCount = 0 errno = 1 fl = F(xl) # used in O1 imax = int(ceil(fabs(log(fabs(xr-xl)/errto)/log(2)))) # used in O2 while errno > errto and iterCount < imax: xold = x x = (xl+xr)/2 iterCount += 1 fx = F(x) # O1 if x != 0: errno = fabs((x - xold)/x) * 100 test = fl*fx # O1 reduce the funcion call:test = F(xl)*F(x) if test < 0: xr = x elif test > 0: xl = x else: # test == 0 is when the F(x) is the root errno = 0 return x if __name__ == "__main__": fun = lambda x: pow(x,4) - 26.0*pow(x,3) + 131.0*pow(x,2) \ - 226.0*x + 120.0 print Bissecc_O2(fun, 1.2, 2.99, 0.0001)