import math from scipy.optimize import fmin,fminbound # one dimension golden search deff( x ): L = math.exp(-x)+x**2 return L defgolden(f ,*args): ifnot('a'in dir()): a=[] b=[] a.append(args[0]) b.append(args[1]) L=1e-16# tolrence for convergence n=80# max steps for iteration #a,b is the region containing opt point lambda1=a[0]+0.382*(b[0]-a[0]) miu1=a[0]+0.618*(b[0]-a[0]) #lambda1 miu1 is the test point of golden search method for k in range(0,n): if abs(b[k]-a[k])<=L: solve=(a[k]+b[k])/2 break f_lambda1=f(lambda1) f_miu1=f(miu1) if f_lambda1>f_miu1: a.append(lambda1) b.append(b[k]) lambda2=miu1 miu2=a[k+1]+0.618*(b[k+1]-a[k+1]) else: a.append(a[k]) b.append(miu1) miu2=lambda1 lambda2=a[k+1]+0.382*(b[k+1]-a[k+1]) lambda1=lambda2 miu1=miu2 print('optimum point is :',solve) return solve print(golden(f,0,1))