大连理工大学优化方法上机大作业优化方法上机大作业学院:姓名:学号:指导老师:肖现涛大连理工大学优化方法上机大作业1第一题源程序如下:functionzy_x=di1ti(x)%di1ti是用来求解优化作业第一题的函数。x0=x;yimuxulong=0.000001;g0=g(x0);s0=-g0;A=2*ones(100,100);k=0;whilek100lanmed=-(g0)'*s0/(s0'*A*s0);x=x0+lanmed*s0;g=g(x);k=k+1;ifnorm(g)yimuxulongzy_x=x;fprintf('After%diterations,obtaintheoptimalsolution.\n\nTheoptimalsolutionis\n%f.\n\nTheoptimalxisans.',k,f(x))大连理工大学优化方法上机大作业2break;endmiu=norm(g)^2/norm(g0)^2;s=-g+miu*s0;g0=g;s0=s;x0=x;endfunctionf=f(x)f=(x'*ones(100,1))^2-x'*ones(100,1);functiong=g(x)g=(2*x'*ones(100,1))*ones(100,1)-ones(100,1);代入x0,运行结果如下:x=zeros(100,1);di1ti(x)After1iterations,obtaintheoptimalsolution.Theoptimalsolutionis-0.250000.Theoptimalxisans.ans=0.005*ones(100,1).大连理工大学优化方法上机大作业3第二题1.最速下降法。源程序如下:functionzy_x=di2titidu(x)%该函数用来解大作业第二题。x0=x;yimuxulong=1e-5;k=0;g0=g(x0);s0=-g0;whilek=0ifnorm(g0)yimuxulongbreak;elselanmed=10;c=0.1;i=0;whilei=0&i100x=x0+lanmed*s0;iff(x)(f(x0)+c*lanmed*g0'*s0)lanmed=lanmed/2;i=i+1;elsebreak;大连理工大学优化方法上机大作业4endendx=x0+lanmed*s0;x0=x;g0=g(x);s0=-g0;k=k+1;endendzy_x=x;zyj=f(x);fprintf('after%diterations,obtaintheoptimalsolution.\n\nTheoptimalsolutionis%f.\n\nTheoptimalxisans.\n',k,zyj);functionf=f(x)x1=[1000]*x;x2=[0100]*x;x3=[0010]*x;x4=[0001]*x;f=(x1-1)^2+(x3-1)^2+100*(x2-x1^2)^2+100*(x4-x3^2)^2;functiong=g(x)x1=[1000]*x;x2=[0100]*x;x3=[0010]*x;x4=[0001]*x;g=[2*(x1-1)-400*x1*(x2-x1^2);200*(x2-x1^2);2*(x3-1)-400*x3*(x4-x3^2);200*(x4-x3^2)];大连理工大学优化方法上机大作业5x=[-1.21-1.21]';di2titidu(x)after5945iterations,obtaintheoptimalsolution.Theoptimalsolutionis0.000000.Theoptimalxisans.ans=1.00001.00001.00001.00002.牛顿法源程序如下:functionzy_x=di2tinewton(x)%该函数用来解大作业第二题。x0=x;yimuxulong=1e-5;k=0;g0=g(x0);h0=h(x0);s0=-inv(h0)*g0;whilek=0&k1000ifnorm(g0)yimuxulongbreak;elsex=x0+s0;x0=x;g0=g(x);h0=h(x);大连理工大学优化方法上机大作业6s0=-inv(h0)*g0;k=k+1;endendzy_x=x;zyj=f(x);fprintf('after%diterations,obtaintheoptimalsolution.\n\nTheoptimalsolutionis%f.\n\nTheoptimalxisans.\n',k,zyj);functionf=f(x)x1=[1000]*x;x2=[0100]*x;x3=[0010]*x;x4=[0001]*x;f=(x1-1)^2+(x3-1)^2+100*(x2-x1^2)^2+100*(x4-x3^2)^2;functiong=g(x)x1=[1000]*x;x2=[0100]*x;x3=[0010]*x;x4=[0001]*x;g=[2*(x1-1)-400*x1*(x2-x1^2);200*(x2-x1^2);2*(x3-1)-400*x3*(x4-x3^2);200*(x4-x3^2)];functionh=h(x)x1=[1000]*x;x2=[0100]*x;大连理工大学优化方法上机大作业7x3=[0010]*x;x4=[0001]*x;h=[2+1200*x1^2-400*x2-400*x100;-400*x120000;002+1200*x3^2-400*x4-400*x3;00-400*x3200];代入初始值,运行结果如下:x=[-1.21-1.21]';di2tinewton(x)after6iterations,obtaintheoptimalsolution.Theoptimalsolutionis0.000000.Theoptimalxisans.ans=1.00001.00001.00001.0000可以看出,用Newton法经过6次迭代就能求出最优解。3.BFGS法源程序如下:functionzy_x=di2tiBFGS(x)%该函数用来解大作业第二题。x0=x;yimuxulong=1e-5;k=0;g0=g(x0);H0=eye(4);s0=-H0*g0;whilek=0&k100ifnorm(g0)yimuxulongbreak;大连理工大学优化方法上机大作业8elselanmed=10;c=0.1;i=0;whilei=0&i100x=x0+lanmed*s0;iff(x)(f(x0)+c*lanmed*g0'*s0)lanmed=lanmed/2;i=i+1;elsebreak;endendx=x0+lanmed*s0;dete_x=x-x0;dete_g=g(x)-g0;miu=1+dete_g'*H0*dete_g/(dete_x'*dete_g);H=H0+(miu*dete_x*dete_x'-H0*dete_g*dete_x'-dete_x*dete_g'*H0)/(dete_x'*dete_g);s=-H*g(x);x0=x;s0=s;H0=H;g0=g(x);k=k+1;endendzy_x=x;zyj=f(x);fprintf('after%diterations,obtaintheoptimalsolution.\n\nTheoptimalsolutionis%f.\n\nTheoptimalxisans.\n',k,zyj);大连理工大学优化方法上机大作业9functionf=f(x)x1=[1000]*x;x2=[0100]*x;x3=[0010]*x;x4=[0001]*x;f=(x1-1)^2+(x3-1)^2+100*(x2-x1^2)^2+100*(x4-x3^2)^2;functiong=g(x)x1=[1000]*x;x2=[0100]*x;x3=[0010]*x;x4=[0001]*x;g=[2*(x1-1)-400*x1*(x2-x1^2);200*(x2-x1^2);2*(x3-1)-400*x3*(x4-x3^2);200*(x4-x3^2)];代入初始值,计算结果如下:x=[-1.21-1.21]';di2tiBFGS(x)after53iterations,obtaintheoptimalsolution.Theoptimalsolutionis0.000000.Theoptimalxisans.ans=1.00001.00001.0000大连理工大学优化方法上机大作业101.0000第三题1.惩罚函数法源程序如下:functionzy_x=di3ti(x)%该函数用来解大作业第三题。x0=x;M=100;c=4;m=1;whilem0g0=g(x0,M);yimuxulong=1e-5;k=0;s0=-inv(H(x0,M))*g0;whilek=0ifnorm(g0)yimuxulongbreak;elsex=x0+s0;%牛顿法;x0=x;g0=g(x,M);s0=-inv(H(x0,M))*g0;k=k+1;endend大连理工大学优化方法上机大作业11ifmax([abs(h(x)),g1(x),g2(x),g3(x)])0.5break;elseM=M*c;m=m+1;endendzy_x=x;zyj=f(x);fprintf('after%diterations,obtaintheoptimalsolution.\n\nTheoptimalsolutionis%f.\n\nTheoptimalxisans.\n',m,zyj);functionF=F(x,M)x1=[10]*x;x2=[01]*x;F=4*x1-x2^2-12+M*(h^2+g1^2+g2^2+g3^2);functiong=g(x,M)x1=[10]*x;x2=[01]*x;g=[4+M*(-4*(25-x1^2-x2^2)*x1+2*(10*x1-x1^2+10*x2-x2^2-34)*(10-2*x1)+2*x1);-2*x2+M*(-4*(25-x1^2-x2^2)*x2+2*(10*x1-x1^2+10*x2-x2^2-34)*(10-2*x2)+2*x2)];functionH=H(x,M)x1=[10]*x;x2=[01]*x;H=[M*(24*x1^2-120*x1+8*x2^2-40*x2+238),M*(16*x1*x2-40*x1-40*x2+200);M*(16*x1*x2-40*x1-40*x2+200),-2+M*(24*x2^2-120*x2+8*x1^2-40*x1+238)];functionf=f(x)大连理工大学优化方法上机大作业12x1=[10]*x;x2=[01]*x;f=4*x1-x2^2-12;functionh=h(x)x1=[10]*x;x2=[01]*x;h=25-x1^2-x2^2;functiong1=g1(x)x1=[10]*x;x2=[01]*x;g=10*x1-x1^2+10*x2-x2^2-34;ifg0g1=g;elseg1=0;endfunctiong2=g2(x)x1=[10]*x;x2=[01]*x;ifx1=0g2=0;elseg2=x1;endfunctiong3=g3(x)x1=[10]*x;x2=[01]*x;大连理工大学优化方法上机大作业13ifx2=0g3=0;elseg3=x2;end代入任意初始值,运算结果如下。x=rand(2,1);di3ticf(x)after1iterations,obtaintheoptimalsolution.Theoptimalsolutionis-31.490552.Theoptimalxisans.ans=1.00