1、slope = dot(gk,dk);while f1 f0 + 0.1*ak*slopeak = ak/2;xk = x0 + ak*dk;f1 = fun(xk);endk = k+1;x0 = xk;gk = grad(xk);fprintf(-The %d-th iter, the residual is %fn,k,res);x_star = xk;function f = fun(x)f = (1-x(1)2 + 100*(x(2)-x(1)2)2;function g = grad(x)g = zeros(2,1);g(1)=2*(x(1)-1)+400*x(1)*(x(1)2-
2、x(2);g(2) = 200*(x(2)-x(1)2);运行结果:x0=0,0;eps=1e-4 eps =1.0000e-004xk=steepest(x0,eps)-The 1-th iter, the residual is 3.271712-The 2-th iter, the residual is 2.504194-The 3-th iter, the residual is 2.073282-The 998-th iter, the residual is 0.449447-The 999-th iter, the residual is 0.449447-The 1000-t
3、h iter, the residual is 0.449447-The 1001-th iter, the residual is 0.449447xk =0.63690.40382.牛顿法function x_star = newton(x0,eps)bk = grad2(x0)(-1);dk=-bk*gk;xk=x0+dk;bk = grad2(xk)(-1);function g = 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;eps=1e-
4、4;xk=newton(x0,eps)-The 1-th iter, the residual is 447.213595-The 2-th iter, the residual is 0.0000001.00003. BFGS方法function x_star = bfgs(x0,eps)g0 = grad(x0);gk=g0;Hk=eye(2);dk = -Hk*gk;fa0=xk-x0;g0=gk;y0=gk-g0;Hk=(eye(2)-fa0*(y0)/(fa0)*(y0)*(eye(2)-(y0)*(fa0)*(y0)+(fa0*(fa0)*(y0);function f=fun(x
5、)f=(1-x(1)2 + 100*(x(2)-x(1)2)2; xk=bfgs(x0,eps)-The 2-th iter, the residual is 2.381565-The 3-th iter, the residual is 3.448742-The 998-th iter, the residual is 0.004690-The 999-th iter, the residual is 0.008407-The 1000-th iter, the residual is 0.005314-The 1001-th iter, the residual is 0.0108800.
6、99550.99114.共轭梯度法f unction x_star =conj(x0,eps)d0=dk;k=k+1;x0=xk;gk=grad(xk);f=(norm(gk)/norm(g0)2;res=norm(gk);dk=-gk+f*d0;f=(1-x(1)2+100*(x(2)-x(1)2)2;function g=grad(x)g=zeros(2,1);g(1)=400*x(1)3-400*x(1)*x(2)+2*x(1)-2;g(2)=-200*x(1)2+200*x(2);xk=Conj(x0,eps)-The 2-th iter, the residual is 1.3805
7、42-The 3-th iter, the residual is 4.527780-The 4-th iter, the residual is 0.850596-The 73-th iter, the residual is 0.001532-The 74-th iter, the residual is 0.000402-The 75-th iter, the residual is 0.000134-The 76-th iter, the residual is 0.0000570.9999上机大作业:编写程序利用增广拉格朗日方法求解下述问题min 4x 1- x2 - 122 2s.
8、t. 25 - x1 - x2 = 010x1x1 + 10x 2- x2 - 34 0x ,x 0初始点取 x0 = 0, 精度取 = 1e - 4.主程序:function x,mu,lamda,output=main(fun,hf,gf,dfun,dhf,dgf,x0)maxk=2000;theta=0.8; eta=2.0;k=0; ink=0;ep=1e-4;sigma=0.4;x=x0; he=feval(hf,x); gi=feval(gf,x);n=length(x); l=length(he); m=length(gi);mu=0.1*ones(l,1); lamda=0.1
9、*ones(m,1);betak=10; betaold=10;while (betakep &maxk)ik,x,val=bfgs( lagrang ,dlagrang ,x0,fun,hf,gf,dfun,dhf,dgf,mu,lamda,sigma);ink=ink+ik;he=feval(hf,x);betak=sqrt(norm(he,2)2+norm(min(gi,lamda/sigma),2)2);if betakepmu=mu-sigma*he;lamda=max(0.0,lamda-sigma*gi);if (k=2 & betaktheta*betaold)sigma=et
10、a*sigma;betaold=betak;x0=x;f=feval(fun,x);output.fval=f;output.iter=k;output.inner_iter=ink;output.beta=betak;增广拉格朗日函数function lag=lagrang(x,fun,hf,gf,dfun,dhf,dgf,mu,lamda,sigma)l=length(he);lag=f; s1=0.0;for (i=1:l)lag=lag-he(i)*mu(i);s1=s1+he(i)2;lag=lag+0.5*sigma*s1;s2=0.0;m)s3=max(0.0,lamda(i)-
11、sigma*gi(i);s2=s2+s32-lamda(i)2;lag=lag+s2/(2.0*sigma);增广拉格朗日梯度函数function dlag=dlagrang(x,fun,hf,gf,dfun,dhf,dgf,mu,lamda,sigma)dlag=feval(dfun,x);dhe=feval(dhf,x); dgi=feval(dgf,x);dlag=dlag+(sigma*he(i)-mu(i)*dhe(:,i);dlag=dlag+(sigma*gi(i)-lamda(i)*dgi(:线搜索程序 基于 BFGS方法function k,x,val=bfgs(fun,gf
12、un,x0,varargin)Max=1000;ep=1.e-4;beta=0.55; sigma1=0.4;n=length(x0); Bk=eye(n);while (kMax)gk=feval(gfun,x0,varargin:);if (norm(gk)ep), break ; enddk=-Bkgk;m=0; mk=0;while (m20)newf=feval(fun,x0+betam*dk,varargin:oldf=feval(fun,x0,varargin:if (newf0)Bk=Bk-(Bk*sk*sk*Bk)/(sk*Bk*sk)+(yk*yk)/(yk*sk);val
13、=feval(fun,x0,varargin:目标函数文件function f=f1(x)f=4*x(1)-x(2)2-12;等式约束文件function he=h1(x)he=25-x(1)2-x(2)2;不等式约束function gi=g1(x)gi=zeros(3,1);gi(1)=10*x(1)-x(1)2+10*x(2)-x(2)2-34;gi(2)=x(1);gi(3)=x(2);目标函数梯度文件function g=df1(x)g=4;-2*x(1);等式函数梯度文件function dhe=dh1(x)dhe=-2*x(1);-2*x(2);不等式函数梯度文件function
14、 dgi=dg1(x)dgi=10-2*x(1),1,0; 10-2*x(2), 0,1;输入数据X0=0;0x,mu,lamda,output=main(f1,h1g1df1dh1dg1,x0)输出数据x =1.00134.8987mu =0.0158lamda =0.5542output =fval: -31.9924iter: 5inner_iter: 33beta: 8.4937e-005上机大作业:1.解:将目标函数改写为向量形式:x*a*x-b*x程序代码:n=2;a=0.5,0;0,1;b=2 4;c=1 1;cvx_beginvariable x(n)minimize( x*a
15、*x-b*x)subject toc * x =0cvx_end运算结果 :Calling SDPT3 4.0: 7 variables, 3 equality constraintsFor improved efficiency, SDPT3 is solving the dual problem.-num. of constraints = 3dim. of socp var = 4, num. of socp blk = 1dim. of linear var = 3*SDPT3: Infeasible path-following algorithmsversion predcorr
16、gam expon scale_dataNT1 0.000it pstep dstep pinfeas dinfeas gapprim-objdual-objcputime-0|0.000|0.000|8.0e-001|6.5e+000|3.1e+002| 1.000000e+001 0.000000e+000| 0:0:00| chol 1 11|1.000|0.987|4.3e-007|1.5e-001|1.6e+001| 9.043148e+000 -2.714056e-001| 0:01| chol 1 12|1.000|1.000|2.6e-007|7.6e-003|1.4e+000
17、| 1.234938e+000 -5.011630e-002| 0:3|1.000|1.000|2.4e-007|7.6e-004|3.0e-001| 4.166959e-001 1.181563e-001| 0:4|0.892|0.877|6.4e-008|1.6e-004|5.2e-002| 2.773022e-001 2.265122e-001| 0:5|1.000|1.000|1.0e-008|7.6e-006|1.5e-002| 2.579468e-001 2.427203e-001| 0:6|0.905|0.904|3.1e-009|1.4e-006|2.3e-003| 2.511
18、936e-001 2.488619e-001| 0:7|1.000|1.000|6.1e-009|7.7e-008|6.6e-004| 2.503336e-001 2.496718e-001| 0:8|0.903|0.903|1.8e-009|1.5e-008|1.0e-004| 2.500507e-001 2.499497e-001| 0:9|1.000|1.000|4.9e-010|3.5e-010|2.9e-005| 2.500143e-001 2.499857e-001| 0:10|0.904|0.904|5.7e-011|1.3e-010|4.4e-006| 2.500022e-001 2.499978e-001| 0:01| chol 2 211|1.000|1.000|5.2e-013|1.1e-0
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1