展开全部
%共轭梯度法 FR
% 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*[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;
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=m2;m2=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*[x1;x2;x3]+b'*[x1;x2;x3]+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
% 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*[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;
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=m2;m2=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*[x1;x2;x3]+b'*[x1;x2;x3]+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
展开全部
MATLAB中用遗传算法求解约束非线性规划问题 Solution of optimization with nonliear constraints programming by genetic alogorithm in MATLAB 作者:王勇, 期刊-核心期刊 哈尔滨商业大学学报(自然科学版)JOURNAL OF HARBIN UNIVERSITY OF COMMERCE(NATURAL SCIENCES EDITION) 2006年 第04期
- 约束优化问题的遗传算法求解 Genetic algorithm solution for constrained optimization 作者:宋松柏,蔡焕杰,康艳, 期刊-核心期刊 西北农林科技大学学报(自然科学版)JOURNAL OF NORTHWEST SCI-TECH UNIVERSITY OF AGRICULTURE AND FORESTRY(NATURAL
- 约束优化问题的遗传算法求解 Genetic algorithm solution for constrained optimization 作者:宋松柏,蔡焕杰,康艳, 期刊-核心期刊 西北农林科技大学学报(自然科学版)JOURNAL OF NORTHWEST SCI-TECH UNIVERSITY OF AGRICULTURE AND FORESTRY(NATURAL
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一般没有程序,你是不是问错了饿。啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |