资源描述:
《2016年大连理工大学优化方法上机大作业.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、word格式2016年大连理工大学优化方法上机大作业学院:专业:班级:学号:姓名:上机大作业1:1.最速下降法:..word格式..word格式functionf=fun(x)f=(1-x(1))^2+100*(x(2)-x(1)^2)^2;endfunctiong=grad(x)g=zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2));g(2)=200*(x(2)-x(1)^2);endfunctionx_star=steepest(x0,eps)gk=gra
2、d(x0);res=norm(gk);k=0;whileres>eps&&k<=1000dk=-gk;..word格式ak=1;f0=fun(x0);f1=fun(x0+ak*dk);slope=dot(gk,dk);whilef1>f0+0.1*ak*slopeak=ak/4;xk=x0+ak*dk;f1=fun(xk);endk=k+1;x0=xk;gk=grad(xk);res=norm(gk);fprintf('--The%d-thiter,theresidualis%f',k,res);end
3、x_star=xk;end>>clear>>x0=[0,0]';>>eps=1e-4;>>x=steepest(x0,eps)..word格式2.牛顿法:functionf=fun(x)f=(1-x(1))^2+100*(x(2)-x(1)^2)^2;endfunctiong=grad2(x)g=zeros(2,2);g(1,1)=2+400*(3*x(1)^2-x(2));g(1,2)=-400*x(1);g(2,1)=-400*x(1);g(2,2)=200;endfunctiong=grad(x)g=
4、zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2));..word格式g(2)=200*(x(2)-x(1)^2);endfunctionx_star=newton(x0,eps)gk=grad(x0);bk=[grad2(x0)]^(-1);res=norm(gk);k=0;whileres>eps&&k<=1000dk=-bk*gk;xk=x0+dk;k=k+1;x0=xk;gk=grad(xk);bk=[grad2(xk)]^(-1);res=norm(g
5、k);fprintf('--The%d-thiter,theresidualis%f',k,res);endx_star=xk;end>>clear>>x0=[0,0]';>>eps=1e-4;>>x1=newton(x0,eps)--The1-thiter,theresidualis447.213595--The2-thiter,theresidualis0.000000x1=1.00001.0000..word格式3.BFGS法:..word格式functionf=fun(x)f=(1-x(1))^
6、2+100*(x(2)-x(1)^2)^2;endfunctiong=grad(x)g=zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)^2-x(2));g(2)=200*(x(2)-x(1)^2);endfunctionx_star=bfgs(x0,eps)g0=grad(x0);gk=g0;res=norm(gk);Hk=eye(2);k=0;whileres>eps&&k<=1000dk=-Hk*gk;..word格式ak=1;f0=fun(x0);f1=fun(x0
7、+ak*dk);slope=dot(gk,dk);whilef1>f0+0.1*ak*slopeak=ak/4;xk=x0+ak*dk;f1=fun(xk);endk=k+1;fa0=xk-x0;x0=xk;go=gk;gk=grad(xk);y0=gk-g0;Hk=((eye(2)-fa0*(y0)')/((fa0)'*(y0)))*((eye(2)-(y0)*(fa0)')/((fa0)'*(y0)))+(fa0*(fa0)')/((fa0)'*(y0));res=norm(gk);fprintf('-
8、-The%d-thiter,theresidualis%f',k,res);endx_star=xk;End>>clear>>x0=[0,0]';>>eps=1e-4;>>x=bfgs(x0,eps)..word格式4.共轭梯度法:..word格式functionf=fun(x)f=(1-x(1))^2+100*(x(2)-x(1)^2)^2;endfunctiong=grad(x)g=zeros(2,1);