Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 45d2f48

Browse files
Bisection in Python
The method is applicable for numerically solving the equation f(x) = 0 for the real variable x, where f is a continuous function defined on an interval [a, b] and where f(a) and f(b) have opposite signs. In this case a and b are said to bracket a root since, by the intermediate value theorem, the continuous function f must have at least one root in the interval (a, b).
1 parent 800eb9d commit 45d2f48

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Random Python Programs/bisection.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def equation(x):
2+
return (x**3 - 4*x - 9)
3+
4+
a = float(input("Enter the inital range value: "))
5+
b = float(input("Enter the final range value: "))
6+
7+
iteration = int(input("Enter the number of Iteration: "))
8+
9+
if(equation(a)*equation(b) > 0):
10+
print("Wrong Input! Root does not exits")
11+
else:
12+
while(iteration != 0):
13+
x = (a+b)/2
14+
if(equation(x)<0):
15+
a = x
16+
elif(equation(x)>0):
17+
b = x
18+
else:
19+
print("Root is Zero")
20+
print("Root : "+str(x))
21+
iteration -= 1

0 commit comments

Comments
 (0)