Created
December 7, 2011 06:36
-
-
Save acbalingit/1441731 to your computer and use it in GitHub Desktop.
FFFFFFFUUUUUUUUUUUU
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
import numpy as np | |
import time | |
import matplotlib.pyplot as plt | |
#function | |
def f(x): | |
y = x**2 - 4*x*np.sin(x) + (2*np.sin(x))**2 | |
return y | |
#routine | |
def hybisect(a, c): | |
print "routine start"; old = 0 | |
while True: | |
b = (a + c)/2. | |
if f(b) == 0: return b | |
if f(a)*f(b) < 0: c = b | |
else: a = b | |
ans = (a*f(c) - c*f(a))/(f(c) - f(a)) | |
if old == ans: break | |
old = ans | |
return ans | |
print hybisect(-2,1) | |
xra = np.linspace(-5,5,100) | |
yra = f(xra) | |
plt.plot(xra,yra) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment