Subido por Sky Solución

# PosFalsaRicardo.py

Anuncio
```# -*- coding: utf-8 -*-
&quot;&quot;&quot;
Created on Fri Mar 6 11:06:27 2020
@author: Mejia
&quot;&quot;&quot;
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 &gt; 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 &gt; 0): # choose new bounding interval dx = x - a; a = x; fa = fx else: dx = b - x; b = x; fb = fx if ((math.fabs(dx) &lt;= eps*math.fabs(x)) or (math.fabs(fx) &lt;= eps)): print(&quot;Numero de iteraciones:&quot; + str(it))
return (x,0) print(&quot;FalsPos: max. no. of iterations exceeded !&quot;); return (x,2)```