# -*- coding: utf-8 -*- """ Created on Fri Mar 6 11:06:27 2020 @author: Mejia """ import math #============================================================================ def FalsPos(Func, a, b): #--------------------------------------------------------------------------- # Determines a real root x of function Func isolated in interval [a,b] by # the false position method # Error code: 0 - normal execution # 1 - [a,b] does not isolate one root or contains several roots # 2 - max. number of iterations exceeded #--------------------------------------------------------------------------- eps = 1e-5 # relative precision of root itmax = 30 # max. no. of iterations x = a; fa = Func(x) # is a the root? if (math.fabs(fa) == 0e0): return (x,0) x = b; fb = Func(x) # is b the root? if (math.fabs(fb) == 0e0): return (x,0) if (fa*fb > 0): return (x,1) # [a,b] does not contain a root # or contains several roots for it in range(1,itmax+1): x = (a *fb - b *fa)/(fb - fa) # new approximation fx = Func(x) if (fa*fx > 0): # choose new bounding interval dx = x - a; a = x; fa = fx else: dx = b - x; b = x; fb = fx if ((math.fabs(dx) <= eps*math.fabs(x)) or (math.fabs(fx) <= eps)): print("Numero de iteraciones:" + str(it)) return (x,0) print("FalsPos: max. no. of iterations exceeded !"); return (x,2)