Skip to content

Instantly share code, notes, and snippets.

@tgarc
Created February 27, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgarc/875eb57aed887fd16e33 to your computer and use it in GitHub Desktop.
Save tgarc/875eb57aed887fd16e33 to your computer and use it in GitHub Desktop.
Golden section search for finding the minimum of a function
# Example usage
# f = lambda x: 3*(x**2)+4*x+ 5
# xhat = findmin(f,-3,2)
from scipy.constants import golden
def goldensearch(f,a,b,tol=1e-6):
xl = b - (b-a)/golden
xu = a + (b-a)/golden
eps = abs(b-a)
while eps > tol:
# print a,b,eps
if f(xl)<f(xu):
b = xu
xu = xl
xl = b - (b-a)/golden
else:
a = xl
xl = xu
xu = a + (b-a)/golden
eps = abs(b-a)
return (xu+xl)/2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment