Created
February 4, 2015 17:03
-
-
Save eddotman/4db82d7607f886fdb983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bin_search(f, y, lo, hi, tol, nmax): | |
if f(lo) < y < f(hi) or f(lo) > y > f(hi): | |
for i in range(nmax): | |
mid = (hi+lo)/2. | |
fmid = f(mid) | |
if abs(fmid - y) <= tol: break | |
if (fmid < y and f(lo) < f(hi)) or (fmid > y and f(lo)>f(hi)): | |
#going right | |
lo = mid | |
else: | |
#going left | |
hi = mid | |
return mid | |
else: | |
raise ValueError("end points don't bracket the target") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment