资源描述:
《共轭梯度法 matlab程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、%conjugategradientmethods%method:FR,PRP,HS,DY,CD,WYL,LS%精确线搜索,梯度终止准则function[m,k,d,a,X,g1,fv]=conjgradme(G,b,c,X,e,method)ifnargin<6error('输入参数必须为6');endn=length(G);ifn==2formatlonge%ratsymsx1x2f=1/2*[x1,x2]*G*[x1;x2]+b'*[x1;x2]+c;g=[diff(f,x1);diff(f,x2)];g1=subs(subs(g,x1,X(1,1)),x2,X(2,1));d=-g1
2、;a=-(d'*g1)/(d'*G*d);%a=-((X(:,1)'*G*d+b'*d)/(d'*G*d));a=g1(:,1)'*g1(:,1)/(d(:,1)'*G*d(:,1));X(:,2)=X(:,1)+a*d;g1=[g1subs(subs(g,x1,X(1,2)),x2,X(2,2))];m1=norm(g1(:,1));m=norm(g1(:,2));i=2;k=zeros(1);switchmethodcase'FR'whilem>=ek(i-1)=(m/m1)^2;d(:,i)=-g1(:,i)+k(i-1)*d(:,i-1);a(i)=-(d(:,i)'*g1(:,i)
3、)/(d(:,i)'*G*d(:,i));%a1(i)=-((X(:,i)'*G*d(:,i)+b'*d(:,i))/(d(:,i)'*G*d(:,i)));a(i)=g1(:,i)'*g1(:,i)/(d(:,i)'*G*d(:,i));X(:,i+1)=X(:,i)+a(i)*d(:,i);g1=[g1subs(subs(g,x1,X(1,i+1)),x2,X(2,i+1))];m1=m;m=norm(g1(:,i+1));i=i+1;endcase'PRP'whilem>=ek(i-1)=g1(:,i)'*(g1(:,i)-g1(:,i-1))/(norm(g1(:,i-1)))^2;
4、d(:,i)=-g1(:,i)+k(i-1)*d(:,i-1);a(i)=-(d(:,i)'*g1(:,i))/(d(:,i)'*G*d(:,i));X(:,i+1)=X(:,i)+a(i)*d(:,i);g1=[g1subs(subs(g,x1,X(1,i+1)),x2,X(2,i+1))];m=norm(g1(:,i+1));i=i+1;endcase'HS'whilem>=ek(i-1)=g1(:,i)'*(g1(:,i)-g1(:,i-1))/(d(:,i-1)'*(g1(:,i)-g1(:,i-1)));d(:,i)=-g1(:,i)+k(i-1)*d(:,i-1);a(i)=-(
5、d(:,i)'*g1(:,i))/(d(:,i)'*G*d(:,i));X(:,i+1)=X(:,i)+a(i)*d(:,i);g1=[g1subs(subs(g,x1,X(1,i+1)),x2,X(2,i+1))];m=norm(g1(:,i+1));i=i+1;endcase'DY'whilem>=ek(i-1)=g1(:,i)'*g1(:,i)/(d(:,i-1)'*(g1(:,i)-g1(:,i-1)));d(:,i)=-g1(:,i)+k(i-1)*d(:,i-1);a(i)=-(d(:,i)'*g1(:,i))/(d(:,i)'*G*d(:,i));X(:,i+1)=X(:,i)
6、+a(i)*d(:,i);g1=[g1subs(subs(g,x1,X(1,i+1)),x2,X(2,i+1))];m=norm(g1(:,i+1));i=i+1;endcase'LS'whilem>=ek(i-1)=g1(:,i)'*(g1(:,i)-g1(:,i-1))/(d(:,i-1)'*(-g1(:,i-1)));d(:,i)=-g1(:,i)+k(i-1)*d(:,i-1);a(i)=-(d(:,i)'*g1(:,i))/(d(:,i)'*G*d(:,i));%a(i)=-((X(:,i)'*G*d(:,i)+b'*d(:,i))/(d(:,i)'*G*d(:,i)));X(:,
7、i+1)=X(:,i)+a(i)*d(:,i);g1=[g1subs(subs(g,x1,X(1,i+1)),x2,X(2,i+1))];m=norm(g1(:,i+1));i=i+1;endcase'CD'whilem>=ek(i-1)=g1(:,i)'*g1(:,i)/(d(:,i-1)'*(-g1(:,i-1)));d(:,i)=-g1(:,i)+k(i-1)*d(:,i-1);a(i)=-(d(:,i