# -*- coding: utf-8 -*- # # regulafalsi.py # # # Autor: Pedro Garcia Freitas # License: Creative Commons # # from math import fabs, exp def RegulaFalsi(F, xl, xr, errto=0.01, imax=1000): """ Return the root of a function using Regula Falsi RegulaFalsi(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 Author: Pedro Garcia License: Creative Commons """ x = 0 iterCount = 0 errno = 1 fl = F(xl) 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 elif test > 0: xl = x fl = fx else: errno = 0 return x if __name__ == "__main__": f = lambda x: exp(-x) - x print RegulaFalsi(f, 1.2, 50, 0.0001,100)