资源描述:
《a03共轭梯度法编程》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、MK7FCCK282Z3SVMK6CZBW26Q6LZ9MKHMNXTJJQ4S83MKMDE2RESG33HSP90第二章11(2)用大M法求解minw=2x1+x2-x3-x4s.tx1-x2+2x3-x4=22x1+x2-3x3+x4=6x1+x2+x3+x4=7xi≥0,i=1,2,3,4用matlab求解如下:f=[2,1,-1,-1,200,200,200];a=[1-12-1100;21-31010;1111001];b=[267];lb=zeros(7,1);[x,fval,exitflag,output,lambda]=linprog(
2、f,[],[],a,b,lb)Optimizationterminated.运行结果如下:x=3.00000.00001.00003.00000.00000.00000.0000fval=2.0000exitflag=1output=iterations:7algorithm:'large-scale:interiorpoint'cgiterations:0message:'Optimizationterminated.'lambda=ineqlin:[0x1double]eqlin:[3x1double]upper:[7x1double]lower:[
3、7x1double]从上述运行结果可以得出:最优解为x=,最小值约为f*=2。P151第三章26用共轭梯度算法求f(x)=(x1-1)^2+5*(x2-x1^2)^2的极小点,取初始点x0=。用matlab求解如下:functionmg=MG()%%共轭梯度法求解习题三第26题%clc;clear;n=2;x=[20]';max_k=100;count_k=1;trace(1,1)=x(1);trace(2,1)=x(2);trace(3,1)=f_fun(x);k=0;g1=f_dfun(x);s=-g1;whilecount_k<=max_kifk=
4、=ng0=f_dfun(x);s=-g0;k=0;elser_min=fminbnd(@(t)f_fun(x+t*s),-100,100);x=x+r_min*s;g0=g1;g1=f_dfun(x);ifnorm(g1)<10^(-6)break;endm=(norm(g1)^2)/(norm(g0)^2);s=-g1+m*s;count_k=count_k+1;trace(1,count_k)=x(1);trace(2,count_k)=x(2);trace(3,count_k)=f_fun(x);k=k+1;endendcount_kxf=f_fu
5、n(x)functiong=f_dfun(x)g(1,1)=20*x(1)^3-20*x(1)*x(2)+2*x(1)-2;g(2,1)=10*x(2)-10*x(1)^2;functionf=f_fun(x)f=5*x(1)^4-10*x(1)^2*x(2)+x(1)^2-2*x(1)+1+5*x(2)^2;运行结果如下:k=59x0=1.00001.0000f0=0从上述运行结果可以得出:最优解为x=,最小值约为f*=0。P151第三章26用BFGS算法求f(x)=(x1-1)^2+5*(x2-x1^2)^2的极小点,取初始点x0=。用matlab求
6、解如下:symsx1x2;f=(x1-1)^2+5*(x2-x1^2)^2;v=[x1,x2];df=jacobian(f,v);df=df.';x0=[2,0]';g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});k=0;H0=[1,0;0,1];while(norm(g1)>0.001&k<300)ifk==0p=-H0*g1;elsevk=sk/(sk'*yk)-(H0*yk)/(yk'*H0*yk);w1=(yk'*H0*yk)*vk*vk';H1=H0-(H0*yk*yk'*H0)/(yk'*H0*yk)+(sk*sk
7、')/(sk'*yk)+w1;p=-H1*g1;H0=H1;endx00=x0;result=Usearch1(f,x1,x2,df,x0,p);arf=result(1);x0=x0+arf*p;g0=g1;g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});p0=p;yk=g1-g0;sk=x0-x00;k=k+1;end;kx0f0=subs(f,{x1,x2},{x0(1,1),x0(2,1)})functionresult=Usearch1(f,x1,x2,df,x0,p)mu=0.001;sgma=0.99;a=0;b
8、=inf;arf=1;pk=p;x3=x0;x4=x3+arf*pk;f1=su