2022-07-11 08:19:52 +00:00
|
|
|
from collections.abc import Callable
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
|
2020-07-27 13:23:55 +00:00
|
|
|
def bisection(function: Callable[[float], float], a: float, b: float) -> float:
|
|
|
|
"""
|
|
|
|
finds where function becomes 0 in [a,b] using bolzano
|
|
|
|
>>> bisection(lambda x: x ** 3 - 1, -5, 5)
|
|
|
|
1.0000000149011612
|
|
|
|
>>> bisection(lambda x: x ** 3 - 1, 2, 1000)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2020-07-27 13:23:55 +00:00
|
|
|
ValueError: could not find root in given interval.
|
|
|
|
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 0, 2)
|
|
|
|
1.0
|
|
|
|
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 2, 4)
|
|
|
|
3.0
|
|
|
|
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2020-07-27 13:23:55 +00:00
|
|
|
ValueError: could not find root in given interval.
|
|
|
|
"""
|
|
|
|
start: float = a
|
|
|
|
end: float = b
|
2018-10-19 12:48:28 +00:00
|
|
|
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,
|
2020-07-27 13:23:55 +00:00
|
|
|
# then this algorithm can't find the root
|
|
|
|
raise ValueError("could not find root in given interval.")
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
2020-07-27 13:23:55 +00:00
|
|
|
mid: float = start + (end - start) / 2.0
|
2022-01-30 19:29:54 +00:00
|
|
|
while abs(start - mid) > 10**-7: # until precisely 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
|
2019-06-24 10:11:07 +00:00
|
|
|
mid = start + (end - start) / 2.0
|
2018-10-19 12:48:28 +00:00
|
|
|
return mid
|
|
|
|
|
|
|
|
|
2020-07-27 13:23:55 +00:00
|
|
|
def f(x: float) -> float:
|
2022-01-30 19:29:54 +00:00
|
|
|
return x**3 - 2 * x - 5
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2018-11-05 17:19:08 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print(bisection(f, 1, 1000))
|
2020-07-27 13:23:55 +00:00
|
|
|
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|