-2

I have the following function written in python 3

def nullstelle(f,a,b,counter=0,TOL=10 ** -4):
counter = counter + 1
if counter <= 100:
    g = f((a + b)/2)
    if f(a) * g <= 0:
        b = (a + b)/2
    else:
        a = (a + b)/2
    if abs(a-b) <= 2*TOL:
        return (a,b)
    else:
        nullstelle(f,a,b,counter,TOL)
else:
    return (a,b)

my problem is, that for the input

def f(x):
return x ** 3 -2
nullstelle(f,0,3)

it does not return anything. I really do not understand how this is possibly.

Im sorry if this looks like a trivial question to you guys, but programming is absolutely not my main field of interest and I know nearly nothing about it (yet).

Dominik
  • 160
  • 4

1 Answers1

1

I feel like this is a duplicate, but couldn't find one quickly. The problem is that you're not returning the result of your recursive call, so you get None if you have to recurse. Change that line to:

return nullstelle(f,a,b,counter,TOL)
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111