diff --git a/python/nonlinear-equations/__pycache__/bisection.cpython-36.pyc b/python/nonlinear-equations/__pycache__/bisection.cpython-36.pyc new file mode 100644 index 0000000..a005aaa Binary files /dev/null and b/python/nonlinear-equations/__pycache__/bisection.cpython-36.pyc differ diff --git a/python/nonlinear-equations/bisection.py b/python/nonlinear-equations/bisection.py new file mode 100644 index 0000000..14b5346 --- /dev/null +++ b/python/nonlinear-equations/bisection.py @@ -0,0 +1,17 @@ +import numpy as np + +def bisection(functie, a, b, tol, nmax): + # we search for the approximate solution + # in a limited amount of steps + for n in range(nmax): + # we split the interval in which we search the solution and search at it's half + c = a + (b - a) / 2 + # if we find the solution or the interval in which we search is very small + # we return the solution and the number of steps in which we found it + if functie(c) == 0 or (b - a) / 2 < tol: + return (c, n) + # modify the searching interval + if np.sign(functie(c)) * np.sign(functie(a)) < 0: + b = c + else: + a = c diff --git a/python/nonlinear-equations/bisection.pyc b/python/nonlinear-equations/bisection.pyc new file mode 100644 index 0000000..fe5da75 Binary files /dev/null and b/python/nonlinear-equations/bisection.pyc differ