Implement the Bisection Method - Implement the Bisection Method

only test with returning are passing (like 14, 16, 18, but not 8-11)

So something is wrong with what you are printing.

And you also have something wrong when the number to take the square root of is less than 1.

what does it mean “the number between … and …”? substraction? (solved)

Please share your updated code

Is it solved now? You no longer need assistance?

def square_root_bisection(tofind, toler=0.01, times=100):

    if tofind < 0:
        raise ValueError("Square root of negative number is not defined in real numbers")
    if tofind == 0 or tofind == 1:
        print(f"The square root of {tofind} is {tofind}")
        return tofind**1

    interv = [0, tofind] #[low, high]    
    itertimes = times #separating iterations
    
    while itertimes > 0:
        
        mid = (interv[0]+interv[1])/2
        
        if mid**2 == tofind or interv[1]-interv[0] <= toler:
            print(f"The square root of {tofind} is approximately {(interv[0]+interv[1])/2}")
            return mid
        
        elif mid ** 2 < tofind:
            interv[0] = mid

        else:
            interv[1] = mid
        itertimes -= 1
        
    print(f"Failed to converge within {times} iterations")
    return None

print(square_root_bisection(81, 1e-5, 100))

i’ve solved the problem with printing, but still have problem with tests 8-11…

For example, if the tolerance is 0.01, the bisection method will keep halving the interval until the difference between the upper and lower bounds is less than or equal to 0.01.

This code should implement this instruction

the upper and lower bounds

This refers to high and low.

When does your loop stop?

1 Like

What is the square root of 0.25?

1 Like

I’VE SOLVED IT! thank you so much guys for spending so much time with me :smiling_face_with_tear:

2 Likes

Nice work!

1 Like