-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisection.py
executable file
·44 lines (39 loc) · 1.08 KB
/
bisection.py
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
44
#!/usr/bin/env python
import math
import sys
CONST = -0.5307963268
f1 = lambda x : x - 2 * math.sin(x)
f2 = lambda x : x * x * x - 25
f3 = lambda x : x * x * x + x - 4
f = lambda x : CONST + math.asin(x) + x * math.sqrt(1-x * x)
def bis(a,b,n):
for _ in range(n):
c = (a+b)/2
try:
y1=f(a) # why? not used!
y2=f(b)
y3=f(c)
except:
print("The values must be in range bla bla")
sys.exit(1)
if y1 * y2 > 0: #have same sign
print(f'Root may be located outside the region [{a}, {b}]')
sys.exit(1);
if y1 * y2 == 0:
print(a if y1 == 0 else b)
sys.exit(1)
if y3 > 0:
b = c
else:
a = c
print(c)
if __name__ == "__main__":
if len(sys.argv) < 2:
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = int(input('number of iterations: '))
else:
a = float(sys.argv[1])
b = float(sys.argv[2])
c = int(sys.argv[3])
bis(a,b,c)