Académique Documents
Professionnel Documents
Culture Documents
Codigo C++ Posicion Falsa
Codigo C++ Posicion Falsa
ec = raw_input("Ecuacion: ")
x0 = float(raw_input("x0: "))
x1 = float(raw_input("x1: "))
##Metodo
f0 = eval(ec, {'x' : x0})
f1 = eval(ec, {'x' : x1})
tol = 0.00001
if f0 * f1 < 0.0:
while abs((x1 - x0) / x1) > tol:
R = x1 - (f1 * (x0 - x1)) / (f0 - f1)
fR = eval(ec, {'x' : R})
if f0 * fR < 0.0:
x1 = R
f1 = fR
if fR * f1 < 0.0:
x0 = R
f0 = fR
##Salidas
if f0 * f1 < 0.0:
print "La raiz es:", R
else:
print "Valores iniciales malos"