Skip to content

Instantly share code, notes, and snippets.

@acbalingit
Created December 7, 2011 06:36
Show Gist options
  • Save acbalingit/1441731 to your computer and use it in GitHub Desktop.
Save acbalingit/1441731 to your computer and use it in GitHub Desktop.
FFFFFFFUUUUUUUUUUUU
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