<>这个程序是一本美国的数值方法教材上提供的,但是我没有成功地运行出来!</P>
<>希望有高手指点迷津,本人在此先谢谢了!</P>
<>程序清单如下:</P>
<P>function [p0,y0,err,P]=grads(F,G,P0,maxl,delta,epsilon,show)<BR>%input -F is the object function input as string 'F'<BR>% -G=(-1/norm)(grad F)*grad F;the search direction <BR>% input as a string 'G'<BR>% -P0 is the initial strating point <BR>% -maxl is the maximum number of interctions<BR>% -delta is the tolerance for the hmin in the single <BR>% parameter minuization in the search direction <BR>% -epsilon is the tolerance for the error in y0<BR>% -show;if show==1 the iterations are display <BR>%Output-P0 is the point of minium <BR>% -y0 is the function value F(P0)<BR>% -err is error bound for y0<BR>% -P is a vector containing the iterations<BR>if nargin==5,show=0;end <BR>[m,n]=size(P0);<BR>maxj=10;big=1e8;h=1;<BR>P=zeros(maxj,n+1);<BR>len=norm(P0);<BR>y0=feval(F,P0);<BR>if(len>1e4),h=len/le4;end<BR>err=1;cnt=0;cond=0;<BR>P(cnt+1,=[P0 y0];<BR>while(cnt<maxl&cond~=5&(h>delta|err<epsilon))<BR> %Compute search direction <BR> S=feval(G,P0);<BR> %Start single parameter quadratic minimization<BR> P1=P0+h*S;<BR> P2=P0+2*h*S;<BR> y1=feval(F,P1);<BR> y2=feval(F,P2);<BR> cond=0;j=0;<BR> while(j<maxj&cond==0)<BR> len=norm(P0);<BR> if(y0<y1)<BR> P2=P1;<BR> y2=y1;<BR> h=h/2;<BR> P1=P0+h*S;<BR> y1=feval(F,P1);<BR> else<BR> if(y2<y1)<BR> P1=P2;<BR> y1=y2;<BR> h=2*h;<BR> P2=P0+2*h*S;<BR> y2=feval(F,P2);<BR> else<BR> cond=-1;<BR> end<BR> end<BR> j=j+1;<BR> if(h<delta),cond=1;end<BR> if(abs(h)>big|len>big),cond=5;end<BR> end<BR> <BR> if(cond==5)<BR> Pmin=P1;<BR> ymin=y1;<BR> else<BR> d=4*y1-2*y0-2*y2;<BR> if(d<0)<BR> hmin=h*(4*y1-2*y0-2*y2)/d;<BR> else<BR> cond=4;<BR> hmin=h/3;<BR> end<BR> %constrcuct the next point<BR> Pmin=P0+hmin*S;<BR> ymin=feval(F,Pmin);<BR> %Determine magitude of next h<BR> h0=abs(hmin);<BR> h1=abs(hmin-h);<BR> h2=abs(hmin-2*h);<BR> if(h0<h),h=h0;end<BR> if(h1<h),h=h1;end<BR> if(h2<h),h=h2;end<BR> if(h==0),h=hmin;end<BR> if(h<delta),cond=1;end<BR> %Terination test for minization <BR> e0=abs(y0-ymin);<BR> e1=abs(y1-ymin);<BR> e2=abs(y2-ymin);<BR> if(e0~=0&e0<err),err=e0;end<BR> if(e1~=0&e1<err),err=e1;end<BR> if(e2~=0&e2<err),err=e2;end<BR> if(e0==0&e1==0&e2==0),err=0;end<BR> if(err<epsilon),cond=2;end<BR> if(cond==2&h<delta),cond=3;end<BR> end<BR> cnt=cnt+1;<BR> P(cnt+1,=[Pmin ymin];<BR> P0=Pmin;<BR> y0=ymin;<BR>end<BR>if(show==1)<BR> disp(P);<BR>end </P> |