From 8a5964e015c3bff5c1581b2ed27afe5a5726591a Mon Sep 17 00:00:00 2001 From: KC <72213787+kushchoudhary98@users.noreply.github.com> Date: Fri, 27 Oct 2023 01:26:40 +0530 Subject: [PATCH 1/3] add: bisection_method.py --- Math/bisection_method.py | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Math/bisection_method.py diff --git a/Math/bisection_method.py b/Math/bisection_method.py new file mode 100644 index 0000000..fce8ccb --- /dev/null +++ b/Math/bisection_method.py @@ -0,0 +1,42 @@ +"""The Bisection Method: +The method is based on The Intermediate Value Theorem +which states that if f(x) is a continuous function +and there are two real numbers a and b such that f(a)*f(b) < 0, +then it is guaranteed that it has at least one root between them. + +for more reference (https://www.geeksforgeeks.org/program-for-bisection-method/) +""" + +#the continuous function whose roots are to be found +def f(x: float): + return float(x*x*x - x*x + 2) + + +def bisection(a: float, b: float, error = 0.01): + if (f(a)*f(b) >= 0): + print("The interval [a, b] are invalid") + return + + while(abs(b-a) >= error): + mid = float((a+b)/2) + + if (f(mid) == 0.0): + print("The value of root is : ", mid) + return + elif (f(a)*f(mid) < 0): + b = mid + continue + else: + a = mid + continue + + if((a-b) <= error): + print("The value of root is : ", mid) + + + +print("Enter the intervals [a, b] : ") +x = float(input()) +y = float(input()) + +bisection(x, y) From c8d09c8a451e114e2db1b55835d64a692741a3c9 Mon Sep 17 00:00:00 2001 From: KC <72213787+kushchoudhary98@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:05:17 +0530 Subject: [PATCH 2/3] Update bisection_method.py --- Math/bisection_method.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Math/bisection_method.py b/Math/bisection_method.py index fce8ccb..2b8f518 100644 --- a/Math/bisection_method.py +++ b/Math/bisection_method.py @@ -1,8 +1,7 @@ """The Bisection Method: -The method is based on The Intermediate Value Theorem -which states that if f(x) is a continuous function -and there are two real numbers a and b such that f(a)*f(b) < 0, -then it is guaranteed that it has at least one root between them. +This method is used to find the roots of a continuous equation +such that the interval [a, b] is provided by the user. +And, f(a)*f(b) < 0 for more reference (https://www.geeksforgeeks.org/program-for-bisection-method/) """ From 4f3fefde4a46b0d297cc217d02e56df62f7c75ef Mon Sep 17 00:00:00 2001 From: KC <72213787+kushchoudhary98@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:10:21 +0530 Subject: [PATCH 3/3] Update bisection_method.py --- Math/bisection_method.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Math/bisection_method.py b/Math/bisection_method.py index 2b8f518..bbf3343 100644 --- a/Math/bisection_method.py +++ b/Math/bisection_method.py @@ -2,6 +2,7 @@ This method is used to find the roots of a continuous equation such that the interval [a, b] is provided by the user. And, f(a)*f(b) < 0 +It is based on the principle of Intermediate Value Theorm. for more reference (https://www.geeksforgeeks.org/program-for-bisection-method/) """