-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathbisection_method.py
More file actions
43 lines (32 loc) · 745 Bytes
/
Copy pathbisection_method.py
File metadata and controls
43 lines (32 loc) · 745 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
@author Rafiul
"""
from math import *
# driver function
def main():
# input the raw equation
equation = input('f(x): x^2+4*x-2: ')
# replace python exponent syntax with general
equation = equation.replace("^","**")
# input the interval as string
a, b = input('Interval: ').split()
# convert the interval as float
a = float(a)
b = float(b)
# initial middle point
x = (a+b)/2
# continously check for an defined functional value
while abs(b-a) > 0.00001:
# shifting the interval left
if(eval(equation) > 0) :
b = x
# shifting the interval right
else:
a = x
# next mid point
x = (a+b)/2
# print out the approximate root
print('approximate root: ',x)
# driver call
if __name__ == "__main__":
main()