Rev 3599 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
C R's optimize() : function fmin(ax,bx,f,tol)C = ========== ~~~~~~~~~~~~~~~~~cc an approximation x to the point where f attains a minimum onc the interval (ax,bx) is determined.cc input..cc ax left endpoint of initial intervalc bx right endpoint of initial intervalc f function subprogram which evaluates f(x) for any xc in the interval (ax,bx)c tol desired length of the interval of uncertainty of the finalc result (.ge.0.)cc output..cc fmin abcissa approximating the point where f attains ac minimumcc the method used is a combination of golden section search andc successive parabolic interpolation. convergence is never much slowerc than that for a fibonacci search. if f has a continuous secondc derivative which is positive at the minimum (which is not at ax orc bx), then convergence is superlinear, and usually of the order ofc about 1.324....c the function f is never evaluated at two points closer togetherc than eps*abs(fmin)+(tol/3), where eps is approximately the squarec root of the relative machine precision. if f is a unimodalc function and the computed values of f are always unimodal whenc separated by at least eps*abs(x)+(tol/3), then fmin approximatesc the abcissa of the global minimum of f on the interval ax,bx withc an error less than 3*eps*abs(fmin)+tol. if f is not unimodal,c then fmin may approximate a local, but perhaps non-global, minimum toc the same accuracy.c this function subprogram is a slightly modified version of thec algol 60 procedure localmin given in richard brent, algorithms forc minimization without derivatives, prentice-hall, inc. (1973).ccdouble precision function fmin(ax,bx,f,tol)implicit nonedouble precision ax,bx,f,tolcEXTERNAL d1machdouble precision dabs,dsqrt,d1machdouble precision a,b,c,d,e,eps,xm,p,q,r,tol1,t2,u,v,w,fu,fv,fw,2 fx,x,tol3cc c is the squared inverse of the golden ratioc=0.5d0*(3.0d0-dsqrt(5.0d0))cc eps is approximately the square root of the relative machinec precision.ceps=d1mach(4)tol1=eps+1.0d0eps=dsqrt(eps)ca=axb=bxv=a+c*(b-a)w=vx=vcc -Wall indicates that d may be used before being assignedcd=0.0d0e=0.0d0fx=f(x)fv=fxfw=fxtol3=tol/3.0d0cc main loop starts here -----------------------------------c20 xm=0.5d0*(a+b)tol1=eps*dabs(x)+tol3t2=2.0d0*tol1cc check stopping criterioncif (dabs(x-xm).le.(t2-0.5d0*(b-a))) go to 900p=0.0d0q=0.0d0r=0.0d0if (dabs(e).gt.tol1) thencc fit parabolacr=(x-w)*(fx-fv)q=(x-v)*(fx-fw)p=(x-v)*q-(x-w)*rq=2.0d0*(q-r)if (q.gt.0.0d0) thenp=-pelseq=-qendifr=ee=dendifif ((dabs(p).ge.dabs(0.5d0*q*r)).or.(p.le.q*(a-x))2 .or.(p.ge.q*(b-x))) thencc a golden-section stepcif (x.lt.xm) thene=b-xelsee=a-xendifd=c*eelsecc a parabolic-interpolation stepcd=p/qu=x+dcc f must not be evaluated too close to ax or bxcif (((u-a).ge.t2).and.((b-u).ge.t2)) go to 90d=tol1if (x.ge.xm) d=-dendifcc f must not be evaluated too close to xc90 if (dabs(d).ge.tol1) thenu=x+delseif (d.gt.0.0d0) thenu=x+tol1elseu=x-tol1endifendiffu=f(u)cc update a, b, v, w, and xcif (fx.le.fu) thenif (u.lt.x) thena=uelseb=uendifendifif (fu.le.fx) thenif (u.lt.x) thenb=xelsea=xendifv=wfv=fww=xfw=fxx=ufx=fuelse170 if ((fu.le.fw).or.(w.eq.x)) thenv=wfv=fww=ufw=fuelseif ((fu.le.fv).or.(v.eq.x).or.(v.eq.w)) thenv=ufv=fuendifendifendifgo to 20cc end of main loopc900 fmin=xreturnend