-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBisection_Method.py
More file actions
36 lines (28 loc) · 853 Bytes
/
Bisection_Method.py
File metadata and controls
36 lines (28 loc) · 853 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
def str_to_func(expr):
return eval("lambda x: " + expr)
def bisection(f, a, b, tol):
if f(a) * f(b) >= 0:
print("\nBisection method fails :(")
return None
c = a
while (b-a)/2 > tol:
c = (a+b)/2
if f(c) == 0.0:
break
elif f(c)*f(a) < 0:
b = c
else:
a = c
print('\nBisection Method works :) ')
return c
print('Enter the mathematical expression carefully : ')
expression = input()
f = str_to_func(expression)
print('Enter the initial interval in which you believe solution exist : ')
interval = list(map(int, input().split(" ")))
print('Enter the tolerence or maximum error in the solution : ')
tolerence = float(input())
# print(f(0))
# print(f(1))
output = bisection(f, interval[0], interval[1], tolerence)
print(f'Output is : {output}')