From ac60aa2e498e20ba2adb9340250e484043ae8dfe Mon Sep 17 00:00:00 2001 From: emil Date: Sat, 7 Dec 2024 03:02:56 +0300 Subject: [PATCH] first commit --- main.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..ce767f5 --- /dev/null +++ b/main.py @@ -0,0 +1,22 @@ +import math + +def f_bisection(x): + return x**3 - 6*x**2 + 11*x - 6 + +def bisection_method(a, b, eps): + fa = f_bisection(a) + fb = f_bisection(b) + if fa * fb > 0: + raise ValueError("f(a)*f(b) > 0. No guarantee that a root exists in the interval [a,b].") + + while True: + c = (a + b) / 2.0 + fc = f_bisection(c) + if abs(fc) < eps: + return c + if fa * fc < 0: + b = c + fb = fc + else: + a = c + fa = fc