opt=fun3(xk);
wolfepowell.m文件
function [k,m,opt,x]=wolfepowell(xk,sk) max=1000;
c1=0.1; c2=0.6;a=0;b=inf;dk=0.2;m=0; while(m<=max)
if (fun3(xk)-fun3(xk+sk*dk)<-c1*dk*gfun3(xk)'*sk) b=dk;dk=(dk+a)/2; elseif
(fun3(xk)-fun3(xk+sk*dk)>=-c1*dk*gfun3(xk)'*sk)&&(gfun3(xk+sk*dk)'*sk if 2*dk<(dk+b)/2 dk=2*dk; else dk=(dk+b)/2; end else mk=m; break; end m=m+1; end m=mk;k=dk;x=xk+sk*dk;opt=fun3(x); 输入参数: 牛顿法运行结果: >> xk=[2,0]';[x,opt,mk] = newton(xk) x = 1.0000 1.0000 opt = 3.2601e-10 mk = 12 最速下降法运行结果: >>xk=[2,0]';[x,opt,mk] = Steepest(xk) x = 0.9999 0.9998 opt = 8.2108e-09 mk = 354 6 / 13 BFGS法运行结果: >>xk=[2,0]';[x,opt,mk] = bfgs(xk) x = 0.9999 0.9999 opt = 4.7725e-09 mk = 16 (0.9999, 0.9998) 最速下降法(迭代次数354) 即得数值最优点为x= (1.0000, 1.0000) 牛顿法 (迭代次数12) 文档来自于网络搜索 (0.9999, 0.9999) BFGS公式 (迭代次数16) 最优解近似为0 第四题(乘子法) multphr.m文件 function [x,mu,lambda,output]=multphr(fun,hf,gf,dfun,dhf,dgf,x0)文档来自于网络搜索 maxk=500; sigma=2.0; eta=2.0; theta=0.8; k=0; ink=0; epsilon=1e-5; x=x0; he=feval(hf,x); gi=feval(gf,x); n=length(x); l=length(he); m=length(gi); mu=0.1*ones(l,1); lambda=0.1*ones(m,1); btak=10; btaold=10; while(btak>epsilon && k [x,~,ik]=bfgs('mpsi','dmpsi',x0,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma);文档来自于网络搜索 ink=ink+ik; he=feval(hf,x); gi=feval(gf,x); btak=0.0; for (i=1:l), btak=btak+he(i)^2; end for (i=1:m); temp=min(gi(i),lambda(i)/sigma); btak=btak+temp^2; end btak=sqrt(btak); if(btak>epsilon) 7 / 13 if(k>=2&&btak> theta*btaold) sigma=eta*sigma; end for (i=1:l), mu(i)=mu(i)-sigma*he(i); end for (i=1:m) lambda(i)=max(0.0,lambda(i)-sigma*gi(i)); end end k=k+1; btaold=btak; x0=x; end f=feval(fun,x); output.fval=f; output.iter=k; output.inner_iter=ink; output.bta=btak; mpsi.m文件 function psi=mpsi(x,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma)文档来自于网络搜索 f=feval(fun,x); he=feval(hf,x); gi=feval(gf,x); l=length(he); m=length(gi); psi=f; s1=0.0; for(i=1:l) psi=psi-he(i)*mu(i); s1=s1+he(i)^2; end psi=psi+0.5*sigma*s1; s2=0.0; for(i=1:m) s3=max(0.0, lambda(i) - sigma*gi(i)); s2=s2+s3^2-lambda(i)^2; end psi=psi+s2/(2.0*sigma); h1.m文件 function he=h1(x) he=-x(1)^2-x(2)^2+25.0; f1.m文件 function f=f1(x) f=4*x(1)-x(2)^2-12; g1.m文件 8 / 13 function gi=g1(x) gi=10*x(1)-x(1)^2+10*x(2)-x(2)^2-34; h1.m文件 function dhe = dh1(x) dhe = [-1*x(1), -1*x(2)]'; dg1.m文件 function dgi = dg1(x) dgi = [10-2*x(1), 10-2*x(2)]'; df1.m文件 function g=df1(x) g = [4, -2.0*x(2)]'; bfgs.m文件 function [x,val,k]=bfgs(fun,gfun,x0,varargin) maxk=500; rho=0.55; sigma1=0.4; epsilon1=1e-5; k=0; n=length(x0); Bk=eye(n); while(k gk=feval(gfun,x0,varargin{:}); if(norm(gk) newf=feval(fun,x0+rho^m*dk,varargin{:}); oldf=feval(fun,x0,varargin{:}); if(newf m=m+1; end x=x0+rho^mk*dk; sk=x-x0; yk=feval(gfun,x,varargin{:})-gk; if(yk'*sk>0) Bk=Bk-(Bk*sk*sk'*Bk)/(sk'*Bk*sk)+(yk*yk')/(yk'*sk);文档来自于网络搜索 end k=k+1; x0=x; end val=feval(fun,x0,varargin{:}); dmpsi.m文件 9 / 13 function dpsi=dmpsi(x,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma)文档来自于网络搜索 dpsi=feval(dfun,x); he=feval(hf,x); gi=feval(gf,x); dhe=feval(dhf,x); dgi=feval(dgf,x); l=length(he); m=length(gi); for(i=1:l) dpsi=dpsi+(sigma*he(i)-mu(i))*dhe(:,i); end for(i=1:m) dpsi=dpsi+(sigma*gi(i)-lambda(i))*dgi(:,i); end 运行结果: >>x0=[1,1]'; [x,mu,lambda,output]=multphr('f1','h1','g1','df1','dh1','dg1',x0)文档来自于网络搜索 x = 1.0013 4.8987 mu = 2.0312 lambda = 0.7545 output = fval: -31.9923 iter: 5 inner_iter: 58 bta: 4.3187e-07 第五题(有效集法) qpact.m文件 function [x,lamk,exitflag,output]=qpact(H,c,Ae,be,Ai,bi,x0)文档来自于网络搜索 epsilon=1.0e-9; err=1.0e-6; k=0; x=x0; n=length(x); kmax=1.0e3; ne=length(be); ni=length(bi); lamk=zeros(ne+ni,1); index=ones(ni,1); for (i=1:ni) if(Ai(i,:)*x>bi(i)+epsilon), index(i)=0; end end while (k<=kmax) Aee=[ ]; if(ne>0), Aee=Ae; end for(j=1:ni) 10 / 13