《最优化程序(共6页).docx》由会员分享,可在线阅读,更多相关《最优化程序(共6页).docx(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上1.function x,val,k=grad(fun,gfun,x0)maxk=5000;rho=0.5;sigma=0.4;k=0;eps=0.01;while(kmaxk) g=feval(gfun,x0); d=-g; if(norm(d)eps),break;end m=0;mk=0; while(m20) if(feval(fun,x0+rhom*d) x,val,k=grad(fun,gfun,x0)x = 0.0042 0.0001val = 1.7672e-05k = 692function x,val,k=dampnm(fun,gfun,Hess,
2、x0)maxk=100;rho=0.5;sigma=0.4;k=0;eps=0.01;while(kmaxk) gk=feval(gfun,x0); Gk=feval(Hess,x0); dk=-Gkgk; if(norm(gk)eps),break;end m=0;mk=0; while(m20) if(feval(fun,x0+rhom*dk)feval(fun,x0)+sigma*rhom*gk*dk) mk=m;break; end m=m+1; end x0=x0+rhomk*dk; k=k+1;endx=x0;val=feval(fun,x);function f=fun(x)f=
3、60-10*x(1)-4*x(2)+x(1)2+x(2)2-x(1)*x(2);endfunction g=gfun(x)g=-10+2*x(1)-x(2),-4+2*x(2)-x(1);endfunction He=Hess(x)n=length(x);He=zeros(n,n);He=2,-1;-1,2;endx,val,k=dampnm(fun,gfun,Hess,x0)x = 8 6val = 8k = 13.function x,val,k=revisenm(fun,gfun,Hess,x0)n=length(x0);maxk=150;rho=0.55;sigma=0.4;tau=0
4、.0;k=0;epsilon=0.01;while (kmaxk) gk=feval(gfun,x0); muk=norm(gk)(1+tau); Gk=feval(Hess,x0); Ak=Gk+muk*eye(n); dk=-Akgk; if (norm(gk)epsilon),break;end m=0;mk=0; while (m20) if (feval(fun,x0+rhom*dk)feval(fun,x0)+sigma*rhom*gk*dk) mk=m;break; end m=m+1; end x0=x0+rhomk*dk; k=k+1;endx=x0;val=feval(fu
5、n,x);function f=fun(x)f=4*(x(1)+1)2+2*(x(2)-1)2+x(1)+x(2)+10;endfunction He=Hess(x)n=length(x);He=zeros(n,n);He=8,0;0,4;endfunction g=gfun(x)g=8*(x(1)+1)+1,4*(x(2)-1)+1;endx0=0;0;x,val,k=revisenm(fun,gfun,Hess,x0)x = -1.1250 0.7498val = 9.8125k = 55.function x,val,k=frcg(fun,gfun,x0)maxk=100;rho=0.6
6、;sigma=0.4;k=0;eps=0.01;n=length(x0);while(k=0.0) d=-g; end end if(norm(g)eps),break;end m=0;mk=0; while(m20) if(feval(fun,x0+rhom*d) x0=0,0; x,val,k=frcg(fun,gfun,x0)x = 0 0val = 0k = 06.function x,val,k=dfp(fun,gfun,x0)n=length(x0);maxk=1e5;rho=0.55;sigma=0.4;k=0;epsilon=0.01;Hk=inv(feval(Hess,x0)
7、;while (kmaxk) gk=feval(gfun,x0); if (norm(gk)epsilon),break;end dk=-Hk*gk; m=0;mk=0; while (m20) if (feval(fun,x0+rhom*dk)0) Hk=Hk-(Hk*yk*yk*Hk)/(yk*Hk*yk)+(sk*yk); end k=k+1; x0=x;endval=feval(fun,x0);function f=fun(x)f=4*(x(1)-5)2+(x(2)-6)2;endfunction g=gfun(x)g=8*(x(1)-5),2*(x(2)-6);endx,val,k=dfp(fun,gfun,x0)x = 4.9974 6.0432val = 0.0019k = 专心-专注-专业