Created
October 5, 2014 08:01
-
-
Save majiang/d37c6d200a6a3517b091 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 bs(f, x=0.0, y=1.0, eps=0.001): | |
if f(y) < 0: | |
return bs(f, y, 3*y-x, eps) | |
if 0 < f(x): | |
return bs(f, 3*x-y, x, eps) | |
m = (x + y) / 2 | |
if y - x < eps: | |
return m | |
if f(m) < 0: | |
return bs(f, m, y, eps) | |
if 0 < f(m): | |
return bs(f, x, m, eps) | |
return m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment