Python/arithmetic_analysis/bisection.py

39 lines
1011 B
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
import math
2019-10-05 05:14:13 +00:00
def bisection(
function, a, b
): # finds where the function becomes 0 in [a,b] using bolzano
2018-10-19 12:48:28 +00:00
start = a
end = b
if function(a) == 0: # one of the a or b is a root for the function
return a
elif function(b) == 0:
return b
2019-10-05 05:14:13 +00:00
elif (
function(a) * function(b) > 0
): # if none of these are root and they are both positive or negative,
2018-10-19 12:48:28 +00:00
# then his algorithm can't find the root
print("couldn't find root in [a,b]")
return
else:
mid = start + (end - start) / 2.0
2019-10-05 05:14:13 +00:00
while abs(start - mid) > 10 ** -7: # until we achieve precise equals to 10^-7
2018-10-19 12:48:28 +00:00
if function(mid) == 0:
return mid
elif function(mid) * function(start) < 0:
end = mid
else:
start = mid
mid = start + (end - start) / 2.0
2018-10-19 12:48:28 +00:00
return mid
def f(x):
2019-10-05 05:14:13 +00:00
return math.pow(x, 3) - 2 * x - 5
2018-10-19 12:48:28 +00:00
if __name__ == "__main__":
print(bisection(f, 1, 1000))