Created
February 27, 2015 21:59
-
-
Save tgarc/875eb57aed887fd16e33 to your computer and use it in GitHub Desktop.
Golden section search for finding the minimum of a function
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
# 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