-
Notifications
You must be signed in to change notification settings - Fork 192
/
bisect.py
28 lines (27 loc) · 897 Bytes
/
bisect.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
## module bisect
''' root = bisect(f,x1,x2,switch=0,tol=1.0e-9).
Finds a root of f(x) = 0 by bisection.
The root must be bracketed in (x1,x2).
Setting switch = 1 returns root = None if
f(x) increases as a result of a bisection.
'''
from math import log,ceil
import error
def bisect(f,x1,x2,switch=0,epsilon=1.0e-9):
f1 = f(x1)
if f1 == 0.0: return x1
f2 = f(x2)
if f2 == 0.0: return x2
if f1*f2 > 0.0: error.err('Root is not bracketed')
n = ceil(log(abs(x2 - x1)/epsilon)/log(2.0))
for i in range(n):
x3 = 0.5*(x1 + x2); f3 = f(x3)
if (switch == 1) and (abs(f3) >abs(f1)) \
and (abs(f3) > abs(f2)):
return None
if f3 == 0.0: return x3
if f2*f3 < 0.0:
x1 = x3; f1 = f3
else:
x2 = x3; f2 = f3
return (x1 + x2)/2.0