资源描述:
《优化方法作业第二版.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、优化方法上机大作业院系:化工与环境生命学部姓名:李翔宇学号:指导教师:肖现涛第一题:1.最速下降法源程序如下:functionx_star=ZSXJ(x0,eps)gk=grad(x0);res=norm(gk);k=0;whileres>eps&&k<=10000dk=-gk;ak=1;f0=fun(x0);f1=fun(x0+ak*dk);slope=dot(gk,dk);whilef1>f0+0.0001*ak*slopeak=ak/2;xk=x0+ak*dk;f1=fun(xk);endk=k+1;x0=xk;
2、gk=grad(xk);res=norm(gk);fprintf('--The%d-thiter,theresidualis%f',k,res);endx_star=xk;endfunctionf=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);end运行结果:>>x0=[0,0]';>>esp=1
3、e-4;>>xk=ZSXJ(x0,eps)--The1-thiter,theresidualis13.--The2-thiter,theresidualis12.--The3-thiter,theresidualis11.……………………………………………--The9144-thiter,theresidualis0.--The9145-thiter,theresidualis0.--The9146-thiter,theresidualis0.xk=0.99990.9998MATLAB截屏:1.牛顿法源程序如下:fun
4、ctionx_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(gk);fprintf('--The%d-thiter,theresidualis%f',k,res);endx_star=xk;endfunctionf=fun(x)f=(1-x(1
5、))^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=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);end运行结果:>>x0=[0,0]';eps=1e-4;>>xk=
6、NEWTON(x0,eps)--The1-thiter,theresidualis447.--The2-thiter,theresidualis0.xk=1.00001.0000MATALB截屏;1.BFGS方法源程序如下:functionx_star=Bfgs(x0,eps)g0=grad(x0);gk=g0;res=norm(gk);Hk=eye(2);k=0;whileres>eps&&k<=1000dk=-Hk*gk;ak=1;f0=fun(x0);f1=fun(x0+ak*dk);slope=dot(gk,d
7、k);whilef1>f0+0.1*ak*slopeak=ak/2;xk=x0+ak*dk;f1=fun(xk);endk=k+1;fa0=xk-x0;x0=xk;g0=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('--The%d-thiter,theresidualis%
8、f',k,res);endx_star=xk;endfunctionf=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(