大连理工优化方法大作业MATLAB编程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

function[x,dk,k]=fjqx(x,s)flag=0;a=0;b=0;k=0;d=1;while(flag==0)[p,q]=getpq(x,d,s);if(p0)b=d;d=(d+a)/2;endif(p=0)&&(q=0)dk=d;x=x+d*s;flag=1;endk=k+1;if(p=0)&&(q0)a=d;d=min{2*d,(d+b)/2};endend%定义求函数值的函数fun,当输入为x0=(x1,x2)时,输出为ffunctionf=fun(x)f=(x(2)-x(1)^2)^2+(1-x(1))^2;functiongf=gfun(x)gf=[-4*x(1)*(x(2)-x(1)^2)+2*(x(1)-1),2*(x(2)-x(1)^2)];function[p,q]=getpq(x,d,s)p=fun(x)-fun(x+d*s)+0.20*d*gfun(x)*s';q=gfun(x+d*s)*s'-0.60*gfun(x)*s';结果:x=[0,1];s=[-1,1];[x,dk,k]=fjqx(x,s)x=-0.00001.0000dk=1.1102e-016k=54functionf=fun(X)%所求问题目标函数f=X(1)^2-2*X(1)*X(2)+2*X(2)^2+X(3)^2+X(4)^2-X(2)*X(3)+2*X(1)+3*X(2)-X(3);endfunctiong=gfun(X)%所求问题目标函数梯度g=[2*X(1)-2*X(2)+2,-2*X(1)+4*X(2)-X(3)+3,2*X(3)-X(2)-1,2*X(4)];endfunction[x,val,k]=frcg(fun,gfun,x0)%功能:用FR共轭梯度法求无约束问题最小值%输入:x0是初始点,fun和gfun分别是目标函数和梯度%输出:x、val分别是最优点和最优值,k是迭代次数maxk=5000;%最大迭代次数rho=0.5;sigma=0.4;k=0;eps=10e-6;n=length(x0);while(kmaxk)g=feval(gfun,x0);%计算梯度itern=k-(n+1)*floor(k/(n+1));itern=itern+1;%计算搜索方向if(itern==1)d=-g;elsebeta=(g*g')/(g0*g0');d=-g+beta*d0;gd=g'*d;if(gd=0.0)d=-g;endendif(norm(g)eps)break;endm=0;mk=0;while(m20)if(feval(fun,x0+rho^m*d)feval(fun,x0)+sigma*rho^m*g'*d)mk=m;break;endm=m+1;endx0=x0+rho^mk*d;val=feval(fun,x0);g0=g;d0=d;k=k+1;endx=x0;val=feval(fun,x0);end结果:x0=[0,0,0,0];[x,val,k]=frcg('fun','gfun',x0)x=-4.0000-3.0000-1.00000val=-8.0000k=21或者function[x,f,k]=second(x)k=0;dk=dfun(x);g0=gfun(x);s=-g0;x=x+dk*s;g1=gfun(x);while(norm(g1)=0.02)if(k==3)k=0;g0=gfun(x);s=-g0;x=x+dk*s;g1=gfun(x);elseif(k3)u=((norm(g1))^2)/(norm(g0)^2);s=-g1+u*s;k=k+1;g0=g1;dk=dfun(x);x=x+dk*s;g1=gfun(x);endendf=fun(x);endfunctionf=fun(x)f=x(1)^2-2*x(1)*x(2)+2*x(2)^2+x(3)^2+x(4)^2-x(2)*x(3)+2*x(1)+3*x(2)-x(3);functiongf=gfun(x)gf=[2*x(1)-2*x(2)+2,-2*x(1)+4*x(2)-x(3)+3,2*x(3)-x(2)-1,2*x(4)];function[p,q]=con(x,d)ss=-gfun(x);p=fun(x)-fun(x+d*ss)+0.2*d*gfun(x)*(ss)';q=gfun(x+d*ss)*(ss)'-0.6*gfun(x)*(ss)';functiondk=dfun(x)flag=0;a=0;d=1;while(flag==0)[p,q]=con(x,d);if(p0)b=d;d=(d+a)/2;endif(p=0)&&(q=0)dk=d;flag=1;endif(p=0)&&(q0)a=d;d=min{2*d,(d+b)/2};endEnd结果:x=[0,0,0,0];[x,f,k]=second(x)x=-4.0147-3.0132-1.00900f=-7.9999k=1function[f,x,k]=third_1(x)k=0;g=gfun(x);while(norm(g)=0.001)s=-g;dk=dfun(x,s);x=x+dk*s;k=k+1;g=gfun(x);f=fun(x);endfunctionf=fun(x)f=x(1)+2*x(2)^2+exp(x(1)^2+x(2)^2);functiongf=gfun(x)gf=[1+2*x(1)*exp(x(1)^2+x(2)^2),4*x(2)+2*x(2)*(x(1)^2+x(2)^2)];function[j_1,j_2]=con(x,d,s)j_1=fun(x)-fun(x+d*s)+0.1*d*gfun(x)*(s)';j_2=gfun(x+d*s)*(s)'-0.5*gfun(x)*(s)';functiondk=dfun(x,s)%获取步长flag=0;a=0;d=1;while(flag==0)[p,q]=con(x,d,s);if(p0)b=d;d=(d+a)/2;endif(p=0)&&(q=0)dk=d;flag=1;endif(p=0)&&(q0)a=d;d=min{2*d,(d+b)/2};endend结果:x=[0,1];[f,x,k]=third_1(x)f=0.7729x=-0.41960.0001k=8(1)程序:function[f,x,k]=third_2(x)k=0;H=inv(ggfun(x));g=gfun(x);while(norm(g)=0.001)s=(-H*g')';dk=dfun(x,s);x=x+dk*s;k=k+1;g=gfun(x);f=fun(x);endfunctionf=fun(x)f=x(1)+2*x(2)^2+exp(x(1)^2+x(2)^2);functiongf=gfun(x)gf=[1+2*x(1)*exp(x(1)^2+x(2)^2),4*x(2)+2*x(2)*(x(1)^2+x(2)^2)];functionggf=ggfun(x)ggf=[(4*x(1)^2+2)*exp(x(1)^2+x(2)^2),4*x(1)*x(2)*exp(x(1)^2+x(2)^2);4*x(1)*x(2)*exp(x(1)^2+x(2)^2),4+(4*x(2)^2+2)*exp(x(1)^2+x(2)^2)];function[j_1,j_2]=con(x,d,s)j_1=fun(x)-fun(x+d*s)+0.1*d*gfun(x)*(s)';j_2=gfun(x+d*s)*(s)'-0.5*gfun(x)*(s)';functiondk=dfun(x,s)%步长获取flag=0;a=0;d=1;b=10000;while(flag==0)[p,q]=con(x,d,s);if(p0)b=d;d=(d+a)/2;endif(p=0)&&(q=0)dk=d;flag=1;endif(p=0)&&(q0)a=d;if2*d=(d+b)/2d=(d+b)/2;elsed=2*d;endendEnd结果:x=[0,1];[f,x,k]=third_2(x)f=0.7729x=-0.41930.0001k=8(2)程序:function[f,x,k]=third_3(x)k=0;X=cell(2);g=cell(2);X{1}=x;H=eye(2);g{1}=gfun(X{1});s=(-H*g{1}')';dk=dfun(X{1},s);X{2}=X{1}+dk*s;g{2}=gfun(X{2});while(norm(g{2})=0.001)dx=X{2}-X{1};dg=g{2}-g{1};v=dx/(dx*dg')-(H*dg')'/(dg*H*dg');h1=H*dg'*dg*H/(dg*H*dg');h2=dx'*dx/(dx*dx');h3=dg*H*dg'*v'*v;H=H-h1+h2+h3;k=k+1;X{1}=X{2};g{1}=gfun(X{1});s=(-H*g{1}')';dk=dfun(X{1},s);X{2}=X{1}+dk*s;g{2}=gfun(X{2});norm(g{2});f=fun(x);x=X{2};endfunctionf=fun(x)f=x(1)+2*x(2)^2+exp(x(1)^2+x(2)^2);functiongf=gfun(x)gf=[1+2*x(1)*exp(x(1)^2+x(2)^2),4*x(2)+2*x(2)*(x(1)^2+x(2)^2)];functionggf=ggfun(x)ggf=[(4*x(1)^2+2)*exp(x(1)^2+x(2)^2),4*x(1)*x(2)*exp(x(1)^2+x(2)^2);4*x(1)*x(2)*exp(x(1)^2+x(2)^2),4+(4*x(2)^2+2)*exp(x(1)^2+x(2)^2);function[p,q]=con(x,d,s)p=fun(x)-fun(x+d*s)+0.1*d*gfun(x)*(s)';q=gfun(x+d*s)*(s)'-0.5*gfun(x)*(s)';functiondk=dfun(x,s)flag=0;a=0;d=1;b=10000;while(flag==0)[p,q]=con(x,d,s);if(p0)b=d;d=(d+a)/2;endif(p=0)&&(q=0)dk=d;flag=1;endif(p=0)&&(q0)a=d;if2*d=(d+b)/2d=(d+b)/2;elsed=2*d;endendend结果:x=[0,1];[f,x,k]=third_3(x)f=0.7729x=-0.41950.0000k=6functioncallqpactH=[20;02];c=[-2-5]';Ae=[];be=[];Ai=[1-2;-1-2;-12;10;01];bi=[-2-6-200]';x0=[00]';[x,lambda,exitflag,output]=qpact(H,c,Ae,be,Ai,bi,x0)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,:)*xbi(i)+epsilon),index(i)=0;endendwhile(k=kmax)Aee=[];if(ne0),Aee=Ae;endfor(j=1:ni)if(index(j)0),

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功