Golden Section Method

2159 days 전, jhlee2chn 작성

1. Golden Section Method

# golden section method def golds(phi,a,b,epsilon,delta): t=(sqrt(5.0)-1)/2 h=b-a phia=phi(a) phib=phi(b) p=a+(1-t)*h q=a+t*h phip=phi(p) phiq=phi(q) i=0 print a, p, q, b while (abs(phib-phia) > delta) or (h > epsilon): if phip <= phiq: b=q phib=phiq q=p phiq=phip h=b-a p=a+(1-t)*h phip=phi(p) else: a=p phia=phip p=q phip=phiq h=b-a q=a+t*h phiq=phi(q) i=i+1 print a, p, q, b if phip <= phiq: s=p phis=phip else: s=q phis=phiq ds=abs(b-a) dphi=abs(phib-phia) return i,s,phis,ds,dphi # test phi(x)=3*x^2-2*tan(x) i, s, phis, ds, dphi = golds(phi, 0.0, 1.0, 1e-4, 1e-5) print "iteration ", i print "optimal solution ", s print "optimal value ", phis print "error of x ", ds print "error of phi(x) ", dphi
 0.000000000000000 0.381966011250105 0.618033988749895 1.00000000000000 0.000000000000000 0.236067977499790 0.381966011250105 0.618033988749895 0.236067977499790 0.381966011250105 0.472135954999579 0.618033988749895 0.236067977499790 0.326237921249264 0.381966011250105 0.472135954999579 0.326237921249264 0.381966011250105 0.416407864998738 0.472135954999579 0.326237921249264 0.360679774997897 0.381966011250105 0.416407864998738 0.360679774997897 0.381966011250105 0.395121628746530 0.416407864998738 0.381966011250105 0.395121628746530 0.403252247502313 0.416407864998738 0.381966011250105 0.390096630005888 0.395121628746530 0.403252247502313 0.381966011250105 0.386991009990747 0.390096630005888 0.395121628746530 0.386991009990747 0.390096630005888 0.392016008731388 0.395121628746530 0.386991009990747 0.388910388716246 0.390096630005888 0.392016008731388 0.386991009990747 0.388177251280389 0.388910388716246 0.390096630005888 0.388177251280389 0.388910388716246 0.389363492570031 0.390096630005888 0.388910388716246 0.389363492570031 0.389643526152104 0.390096630005888 0.388910388716246 0.389190422298319 0.389363492570031 0.389643526152104 0.389190422298319 0.389363492570031 0.389470455880391 0.389643526152104 0.389363492570031 0.389470455880391 0.389536562841743 0.389643526152104 0.389363492570031 0.389429599531383 0.389470455880391 0.389536562841743 0.389429599531383 0.389470455880391 0.389495706492735 0.389536562841743 0.389470455880391 0.389495706492735 0.389511312229400 0.389536562841743 iteration 20 optimal solution 0.389495706492735 optimal value -0.365810354351015 error of x 0.0000661069613518972 error of phi(x) 2.78797757102822e-9 0.000000000000000 0.381966011250105 0.618033988749895 1.00000000000000 0.000000000000000 0.236067977499790 0.381966011250105 0.618033988749895 0.236067977499790 0.381966011250105 0.472135954999579 0.618033988749895 0.236067977499790 0.326237921249264 0.381966011250105 0.472135954999579 0.326237921249264 0.381966011250105 0.416407864998738 0.472135954999579 0.326237921249264 0.360679774997897 0.381966011250105 0.416407864998738 0.360679774997897 0.381966011250105 0.395121628746530 0.416407864998738 0.381966011250105 0.395121628746530 0.403252247502313 0.416407864998738 0.381966011250105 0.390096630005888 0.395121628746530 0.403252247502313 0.381966011250105 0.386991009990747 0.390096630005888 0.395121628746530 0.386991009990747 0.390096630005888 0.392016008731388 0.395121628746530 0.386991009990747 0.388910388716246 0.390096630005888 0.392016008731388 0.386991009990747 0.388177251280389 0.388910388716246 0.390096630005888 0.388177251280389 0.388910388716246 0.389363492570031 0.390096630005888 0.388910388716246 0.389363492570031 0.389643526152104 0.390096630005888 0.388910388716246 0.389190422298319 0.389363492570031 0.389643526152104 0.389190422298319 0.389363492570031 0.389470455880391 0.389643526152104 0.389363492570031 0.389470455880391 0.389536562841743 0.389643526152104 0.389363492570031 0.389429599531383 0.389470455880391 0.389536562841743 0.389429599531383 0.389470455880391 0.389495706492735 0.389536562841743 0.389470455880391 0.389495706492735 0.389511312229400 0.389536562841743 iteration 20 optimal solution 0.389495706492735 optimal value -0.365810354351015 error of x 0.0000661069613518972 error of phi(x) 2.78797757102822e-9

2. Comparison

phi(x)=3*x^2-2*tan(x) plot(phi(x), (x, 0, 1))
find_local_minimum(phi, 0, 1, tol=1e-4)
 (-0.3658103542331941, 0.38948516763735291) (-0.3658103542331941, 0.38948516763735291)