% G为对称正定矩阵,X是初始点,e为精度
%a是精确线搜索步长
function [m2,a,d,X,g1,f1] = conjgrad(G,b,c,X,e)
n=length(G)
if n==2
format long e %rat
syms x1 x2
f=1/2*[x1,x2]*G*[x1x2]+b'*[x1x2]+c
g=[diff(f,x1)diff(f,x2)]
g1=subs(subs(g,x1,X(1,1)),x2,X(2,1))
d=-g1
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=[g1 subs(subs(g,x1,X(1,2)),x2,X(2,2))]
m1=norm(g1(:,1))m2=norm(g1(:,2))
k=(m2/m1)^2
i=2
while m2>=e
d(:,i)=-g1(:,i)+k(i-1)*d(:,i-1)
a(i)=-(d(:,i)'*g1(:,i))/(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=[g1 subs(subs(g,x1,X(1,i+1)),x2,X(2,i+1))]
m1=m2m2=norm(g1(:,i+1))
k(i)=(m2/m1)^2
i=i+1
end
f1=subs(subs(f,x1,X(1,i)),x2,X(2,i))
elseif n==3
format long
syms x1 x2 x3
f=1/2*[x1,x2,x3]*G*[x1x2x3]+b'*[x1x2x3]+c
g=[diff(f,x1)diff(f,x2)diff(f,x3)]
g1=subs(subs(subs(g,x1,X(1,1)),x2,X(2,1)),x3,X(3,1))
d=-g1
a=-((X(:,1)'*G*d+b'*d)/(d'*G*d))
X(:,2)=X(:,1)+a*d
g1=[g1 subs(subs(subs(g,x1,X(1,2)),x2,X(2,2)),x3,X(3,2))]
k=(norm(g1(:,2))/norm(g1(:,1)))^2
m=norm(g1(:,2))
i=2
while m>=e
d(:,i)=-g1(:,i)+k*d(:,i-1)
a(i)=-((X(:,i)'*G*d(:,i)+b'*d(:,i))/(d(:,i)'*G*d(:,i)))
X(:,i+1)=X(:,i)+a(i)*d(:,i)
g1=[g1 subs(subs(subs(g,x1,X(1,i+1)),x2,X(2,i+1)),x3,X(3,i+1))]
k=(norm(g1(:,i+1))/norm(g1(:,i)))^2
m=norm(g1(:,i+1))
i=i+1
end
f1=subs(subs(subs(f,x1,X(1,i)),x2,X(2,i)),x3,X(3,i))
end
识别与控制管理程序。HS程序及应用原理,gwload_hs是识别与控制管理程序,识别及控制是指医药经营企业的采购、销售、收货、验收、储存、养护、出库复核、运输这些环节的管理,软件要能够根据设置的管控条件,自动判断是否超出范围经营、证照过期是否可以自动控制、商品过期是否自动控制等。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)