2018-10-19 12:48:28 +00:00
|
|
|
import math
|
2022-07-11 08:19:52 +00:00
|
|
|
from collections.abc import Callable
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2020-08-01 06:02:31 +00:00
|
|
|
def intersection(function: Callable[[float], float], x0: float, x1: float) -> float:
|
2020-06-16 08:09:19 +00:00
|
|
|
"""
|
|
|
|
function is the f we want to find its root
|
|
|
|
x0 and x1 are two random starting points
|
2020-08-01 06:02:31 +00:00
|
|
|
>>> intersection(lambda x: x ** 3 - 1, -5, 5)
|
|
|
|
0.9999999999954654
|
|
|
|
>>> intersection(lambda x: x ** 3 - 1, 5, 5)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2020-08-01 06:02:31 +00:00
|
|
|
ZeroDivisionError: float division by zero, could not find root
|
|
|
|
>>> intersection(lambda x: x ** 3 - 1, 100, 200)
|
|
|
|
1.0000000000003888
|
|
|
|
>>> intersection(lambda x: x ** 2 - 4 * x + 3, 0, 2)
|
|
|
|
0.9999999998088019
|
|
|
|
>>> intersection(lambda x: x ** 2 - 4 * x + 3, 2, 4)
|
|
|
|
2.9999999998088023
|
|
|
|
>>> intersection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)
|
|
|
|
3.0000000001786042
|
|
|
|
>>> intersection(math.sin, -math.pi, math.pi)
|
|
|
|
0.0
|
|
|
|
>>> intersection(math.cos, -math.pi, math.pi)
|
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2020-08-01 06:02:31 +00:00
|
|
|
ZeroDivisionError: float division by zero, could not find root
|
2020-06-16 08:09:19 +00:00
|
|
|
"""
|
2020-08-01 06:02:31 +00:00
|
|
|
x_n: float = x0
|
|
|
|
x_n1: float = x1
|
2018-10-19 12:48:28 +00:00
|
|
|
while True:
|
2020-08-01 06:02:31 +00:00
|
|
|
if x_n == x_n1 or function(x_n1) == function(x_n):
|
|
|
|
raise ZeroDivisionError("float division by zero, could not find root")
|
|
|
|
x_n2: float = x_n1 - (
|
2019-10-05 05:14:13 +00:00
|
|
|
function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n))
|
|
|
|
)
|
2022-01-30 19:29:54 +00:00
|
|
|
if abs(x_n2 - x_n1) < 10**-5:
|
2018-10-19 12:48:28 +00:00
|
|
|
return x_n2
|
2019-10-05 05:14:13 +00:00
|
|
|
x_n = x_n1
|
|
|
|
x_n1 = x_n2
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2020-08-01 06:02:31 +00:00
|
|
|
def f(x: float) -> float:
|
2019-10-05 05:14:13 +00:00
|
|
|
return math.pow(x, 3) - (2 * x) - 5
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2018-11-05 17:19:08 +00:00
|
|
|
if __name__ == "__main__":
|
2019-10-05 05:14:13 +00:00
|
|
|
print(intersection(f, 3, 3.5))
|