Bisection Method with Python

Asked

Viewed 57 times

0

I am developing a program to calculate numerical methods. Start by confinement methods, in this case one of them is the bisection.

I’m not being able to print the iterations that the method performs... I created a generic function for the method, but when I call it inside mine if responsible for the method chosen by the user, it does not return any value...

def bisection(f, a, b, valor_tolerancia, numero_iteracoes):

    i = 1
    fa = f(a)

    while i <= numero_iteracoes:
        x = a+(a-b)/2.0
        fx = f(x)

        if ((f(x) == 0) or ((b-a)/2.0 < valor_tolerancia):
            return x
        else:
            i = i + 1
            if (f(a)*f(x) > 0):
                a = x
                fa = fx
            else:
                b = x
    raise NameError("Você excedeu o Numéro de Iterações!");


if metodo_escolhido == 1:
    print("# Método da Bisecção #")
    print("\n")

    valor_a = float(input("Valor de a: "))
    valor_b = float(input("Valor de b: "))
    valor_tolerancia = float(input("Tolerancia: "))
    numero_iteracoes = float(input("Digite o numero de iterações: "))

calculo_bissec = bisection(f,a,b,valor_tolerancia,num)
print(calculo_bissec)
  • 1

    Here: calculo_bissec = bisection(f,a,b,valor_tolerancia,num) shouldn’t it be: calculo_bissec = bisection(f, value_a, value_tolerancia, numero_iteracoes)? I didn’t see the definition of f either.

  • f would be the equation in which the user would type, but do not know how to perform such operation...because most of the related content, the programmer already puts the function and not user.

  • Maybe the use of Val can help you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.