Python - Podrían ayudarme a saber qué tengo mal?

 
Vista:
sin imagen de perfil

Podrían ayudarme a saber qué tengo mal?

Publicado por Hector (1 intervención) el 25/06/2015 01:35:01
Buenas! Apenas si soy un novato del Python, y tengo un rato con este programa... Es un método de bisección, pero siempre me imprime las variables antes de entrar al While, me parece a mi... Podrían ayudarme a saber qué tengo mal?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def bisection(funcion, superior, inferior, tolerancia=0.0001):
    fu = funcion(superior)
    if fu == 0.0:
        return superior
    fl = funcion(inferior)
    if fl == 0.0:
        return inferior
    if fu * fl > 0.0:
        return None
    else:
        n = 1.0
        error = (superior - inferior) / 2 ** n
        mitad = (superior + inferior) / 2
        while error > tolerancia:
            n = n + 1.0
            fu = funcion(superior)
            fl = funcion(inferior)
            fm = funcion(mitad)
            if fm == 0.0:
                return mitad
            elif fm * fu < 0.0:
                inferior = mitad
            elif fm * fl < 0.0:
                superior = mitad
            mitad = (superior + inferior) / 2
            error = (superior - inferior) / 2 ** n
        return (mitad, n, abs(error))
 
 
def func(x):
    return x ** 3 + x - 6
 
(x, y, z) = bisection(func, 1.0, 10.0)
print (('La raiz hallada es {}'.format(x) + ' con {} iteraciones'.format(y) + ' y un error de {}'.format(z)))
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder