Python/arithmetic_analysis/intersection.py
Christian Clauss 9316e7c014
Set the Python file maximum line length to 88 characters (#2122)
* flake8 --max-line-length=88

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
2020-06-16 10:09:19 +02:00

27 lines
533 B
Python

import math
def intersection(function, x0, x1):
"""
function is the f we want to find its root
x0 and x1 are two random starting points
"""
x_n = x0
x_n1 = x1
while True:
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:
return x_n2
x_n = x_n1
x_n1 = x_n2
def f(x):
return math.pow(x, 3) - (2 * x) - 5
if __name__ == "__main__":
print(intersection(f, 3, 3.5))