If running under false condition

Asked

Viewed 70 times

0

Hello, I started today with python. I’m making an algorithm that returns the root of a polynomial (2x -4) by the bisection method:

The methods I will use in the main function (biseccon).

Since [a. b] is my interval, temRaiz() serves to verify a possible signal change between f(a) and f(b), if any, there is a root in this interval.

def temRaiz(intervalo) :
    if ((2*intervalo[0] - 4)*(2*intervalo[1] - 4) < 0):
        return True
    else:
        return False

The methods below are to halve the interval by moving only one point.


def cortaIntervaloA(intervalo, ptmedio) :
    intervalo[0] = ptmedio
    return intervalo

def cortaIntervaloB(intervalo, ptmedio) :
    intervalo[1] = ptmedio
    return intervalo

Here I get the initial interval values and right after the main function.

intervalo = []
intervalo.append(float(input('Digite o primeiro valor do intervalo:')))
intervalo.append(float(input('Digite o segundo valor do interalo:')))

def bisseccao(intervalo) :
    while abs(intervalo[1]-intervalo[0]) > 0:
        acumuladorMedia = (intervalo[1]+intervalo[0])/2
        print(intervalo)
        if temRaiz(cortaIntervaloB(intervalo,acumuladorMedia)):
            cortaIntervaloB(intervalo,acumuladorMedia)
        else:
            print(intervalo)
            cortaIntervaloA(intervalo,acumuladorMedia)
        print(intervalo)

    return acumuladorMedia

print(bisseccao(intervalo))

The problem seems to be in the if structure, the instruction in it is executed even when the condition returns False and soon after Else is also executed. (I put the print functions to help identify where the error was)

Example (input: 1.6)

[1.0, 6.0]
[1.0, 3.5]
[1.0, 3.5]
[1.0, 2.25]
[1.0, 2.25]
[1.0, 1.625] # ** Quando a condição retorna False o ponto B ainda é deslocado
[1.625, 1.625] # ** else executado em seguida
1.625

  • Along those lines if temRaiz(cortaIntervaloB(intervalo,acumuladorMedia)): you are already changing the interval and change again when you run the cortaIntervaloB in the next line, no?

  • So, at first I thought so, but looking at the output of the example, on line 2 (the last print(interval) of the first iteration) to see that it only ran once, if that was the case, it would have run twice.

  • Is it not a question of printing before calling the function? Else, should not call print AFTER calling function'trim'?

  • What happens is, precisely, because you’re calling the cortaIntervaloB no if. Tries to call the temRaiz, as follows: temRaiz([intervalo[0],acumuladorMedia]) and whether he solves his problem. Remembering that the ideal is that you put an accuracy in the subtraction of while, otherwise it can be in infinite loop with very small numbers...

  • Thank you Felipe! I didn’t know it was possible to call a function within a condition, I thought it stored the value returned temporarily only for verification, without changing any scope variable, I will include the precision in the algorithm.

No answers

Browser other questions tagged

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