# -*- coding: utf-8 -*- # # regulafalsi_modificado.py # # # Autor: Pedro Garcia Freitas # License: Creative Commons # # from math import fabs, exp, log def RegulaFalsi_O2(F, xl, xr, errto=0.01, imax=1000): """ Return the root of a function using Regula Falsi RegulaFalsi_O2(fun, xl, xr, imax, errto) * fun: Function where find the roots * xl: left bound of interval * xr: right bound of interval * imax: max of iterations allowed * errto: tolerated error ( for aprox. ), in percents Author: Pedro Garcia License: Creative Commons """ x = 0 iterCount = 0 errno = 1 fl = F(xl) leftCount = 0 # O2 rightCount = 0 # O2 while errno > errto and iterCount < imax: xold = x iterCount = iterCount + 1 fr = F(xr) # x = (xl+xr)/2.l ponto onde modifica a Bisseccao x = xr - fr*(xl-xr)/(fl - fr) fx = F(x) if x != 0: errno = fabs((x - xold)/x) test = fl * fx if test < 0: xr = x fr = fx # O2 rightCount = 0 # O2 leftCount += 1 # O2 if leftCount >= 2: fl = fl/2 # O2 elif test > 0: xl = x fl = fx fl = F(xl) # O2 leftCount = 0 # O2 rightCount += 1 #O2 if rightCount >= 2: fr = fr/2 # O2 else: errno = 0 return x if __name__ == "__main__": f = lambda x: x*log(x/3) - x print RegulaFalsi_O2(f, 1.2, 50, 1e-14, 100)