当前位置:文档之家› 大连理工大学2016年秋季优化方法大作业

大连理工大学2016年秋季优化方法大作业


//f(x)//
function f=f(x) x1=[1 0]*x; x2=[0 1]*x; f=(1-x1)^2+100*(x2-x1^2)^2; //梯度函数// function g=g(x) x1=[1 0]*x; x2=[0 1]*x; g=[-2*(1-x1)-400*x1*(x2-x1^2); 200*(x2-x1^2)];
function h=h(x) x1=[1 0]*x; x2=[0 1]*x; h=[ 2+1200*x1^2-400*x2-400*x1; -400*x1 200]; //运行过程// >> x=[0;0] x= 0 0 >> di1tinewton(x) after 2 iterations,obtain the optimal solution. The optimal solution is 0.000000. The optimal "x" is "ans". ans = 1.0000 1.0000 3°BFGS 方法
优化方法
期末上机大作业
姓 名:李岚松 学 部:电信学部电气工程 学 号:31609020
2016 年 11 月 9 日
1°最速下降法 //最速下降法主函数// function lls=di1titidu(x) =di1titidu(x) x0=x; eps=1e-4; 4; k=0; g0=g(x0); while (k>=0) if norm(g0)<eps break; else lanmed=10;c=0.1;i=0; while i>=0&&i<100 100 x=x0+lanmed*s0; if f(x)>(f(x0)+c*lanmed*g0'*s0) lanmed=lanmed/2; i=i+1; else break; end end x=x0+lanmed*s0; x0=x; g0=g(x); s0=-g0; k=k+1; end end lls=x; sll=f(x); fprintf('after %d iterations,obtain the optimal solution.\n\ solution. \nThe optimal solution is %f.\n\n n The optimal "x" is "ans".\n',k,sll); "ans". s0= s0=-g0;
//共轭梯度法主函数// function [x,val,k]=frcg(fun,gfun,x0) rho=0.6;sigma=0.4; k=0; epsilon=1e-4; n=length(x0); while(k<maxk) g=feval(gfun,x0); itern=k-(n+1)*floor(k/(n+1)); itern=itern+1; if(itern==1) d=-g; else beta=(g'*g)/(g0'*g0); d=-g+beta*d0; gd=g'*d; if(gd>=0.0) d=-g; end end if(norm(g)<epsilon), break; end m=0; mk=0; while(m<20) if(feval(fun,x0+rho^m*d)<feval(fun,x0)+sigma*rho^m*g'*d) mk=m; break; end
//运行过程//
>> x=[0;0] x= 0 0 >> di1tiBFGS(x) after 14 iterations,obtain the optimal solution. The optimal solution is 0.000000. The optimal "x" is "ans". ans = 1.0000 1.0000 4°共轭梯度法
//运行过程// >> x=[0;0] x= 0 0 >> di1titidu(x) after 4574 iterations,obtain the optimal solution. The optimal solution is 0.000000. The optimal "x" is "ans". ans = 1.0001 1.0002 2°牛顿法 //牛顿法主函数// function lls=di2tinewton(x) x0=x; eps=1e-4; k=0; g0=g(x0); h0=h(x0);s0=-inv(h0)*g0; while (k>=0&&k<1000) if norm(g0)<eps break; else x=x0+s0; x0=x; g0=g(x); h0=h(x); s0=-inv(h0)*g0; k=k+1; end end
lls=x; sll=f(x); fprintf('after %d iterations,obtain the optimal solution.\n\nThe optimal solution is %f.\n\n The optimal "x" is "ans".\n',k,sll); //f(x)//
//梯度函数//
function g=gfun(x) x1=[1 0]*x; x2=[0 1]*x; g=[-2*(1-x1)-400*x1*(x2-x1^2); 200*(x2-x1^2)];
//运行过程// >> x0=[0 0]' x0 = 0 0 >> [x,val,k]=frcg('fun','gfun',x0) x= 1.0001 1.0002
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;
//增广拉格朗日函数//
function psi=mpsi(x,fun,hf,gf,dfun,dhf,dgf,mu,lambda,sigma) f=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);
//f(x)//
function f=f(x) x1=[1 0]*x;
x2=[0 1]*x; f=(1-x1)^2+100*(x2-x1^2)^2; //梯度函数// function g=g(x) x1=[1 0]*x; x2=[0 1]*x; g=[-2*(1-x1)-400*x1*(x2-x1^2); 200*(x2-x1^2)];
val = 7.2372e-09 k= 122
//增广拉格朗日法主函数// 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-4; 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 (btak>epsilon & k<maxk) [x,ival,ik]=bfgs('mpsi' 'mpsi','dmpsi',x0,fun,hf,gf,dfun,dhf,dgf,mu,lambda,s ,x0,fun,hf,gf,dfun,dhf,dgf,mu,lambda,s igma); 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 if(k>=2 (k>=2 & btak > theta*btaold) sigma=eta*sigma; end for i=1:l, mu(i)=mu(i)-sigma*he(i); mu(i)=mu(i) end for i=1:m lambda(i)=max(0.0,lambda(i) )=max(0.0,lambda(i)-sigma*gi(i)); end end
//增广拉格朗日函数//
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
相关主题