2018-10-19 12:48:28 +00:00
|
|
|
import math
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
|
|
|
def intersection(
|
|
|
|
function, x0, x1
|
|
|
|
): # function is the f we want to find its root and x0 and x1 are two random starting points
|
2018-10-19 12:48:28 +00:00
|
|
|
x_n = x0
|
|
|
|
x_n1 = x1
|
|
|
|
while True:
|
2019-10-05 05:14:13 +00:00
|
|
|
x_n2 = x_n1 - (
|
|
|
|
function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n))
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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))
|