电气优化设计大作业

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

电气优化设计作业

2、用黄金分割法求目标函数107)(2+-=x x x f 的极小点,初始搜索区间[a ,b]=[1,7],迭代精度取0.2。

答:程序如下

function xmin=golden(f,a,b,e)

k=0; a1 =b-0.618*(b-a); a2 =a+0.618*(b-a);

while b-a>e

y1=subs(f,a1); y2=subs(f,a2);

if y1>y2 a=a1; a1=a2; y1=y2; a2=a+0.618*(b-a);

else b=a2; a2=a1; y2=y1; a1=b-0.618*(b-a);

end

k=k+1;

end

xmin=(a+b)/2; fmin=subs(f,xmin) fprintf('k=\n'); disp(k);

将程序保存后在命令窗口输入以下指令并输入回车:

syms x a b a3 e h; a=input('搜索区间的第一点\a='); b=input('搜索区间的第二点\b='); e=input('搜索精度\ne='); disp('需求的优化函数f=f(x),调用xmin=golden(f,a,b,e)');

按照提示将搜索区间、目标函数及迭代精度输入,键入指令xmin=golden(f,a,b,e)'),回车可得结果如下(k 为迭代次数): fmin=-2.2497,k=8,xmin=3.4834

3、用牛顿法求

10)1(2)1(4)(212221+++-++=x x x x X f 的最优解,设X (0)=[0, 0]T 。 答:程序如下:

syms x1 x2;

f=4*(x1+1)^2+2*(x2-1)^2+x1+x2+10; v=[x1,x2]; df=jacobian(f,v); df=df.'; G=jacobian(df,v);

epson=1e-12;x0=[0,0]'; g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)}); G1=subs(G,{x1,x2},{x0(1,1),x0(2,1)}); k=0;mul_count=0;sum_count=0; mul_count=mul_count+12; sum_count=sum_count+6; while(norm(g1)>epson)

p=-G1\g1; x0=x0+p; g1=subs(df,{x1,x2},{x0(1,1),x0(2,1)});

G1=subs(G ,{x1,x2},{x0(1,1),x0(2,1)}); k=k+1;

mul_count=mul_count+16;sum_count=sum_count+11;

end;

x0

结果为:x0 =( -1.1250,0.7500)

4、用拉格朗日乘子法求解以下约束最优化问题:

06)( ..60

410)(min 121212221=-=+---+=x X h t s x x x x x x X f

答:程序如下

syms x1 x2 lama

f=x1^2+x2^2-x1*x2-10*x1-4*x2+60+lama*(x1-6); dx1=diff(f,x1); dx2=diff(f,x2); dlama=diff(f,lama); x1x1=solve(dx1,x1); x2x2=solve(dx2,x2); ff=subs(dlama,{x1,x2},{x1x1,x2x2}); lamao=solve(ff); xo=subs(x1x1,lama,lamao) yo=subs(x2x2,lama,lamao) fo=subs(f,{x1,x2,lama},{xo,yo,lamao}) 结果为:x1=6,x2=5,minf(x)=11。

5、用二进制遗传算法求解以下约束整数优化问题:

10

5 6

3 15

7 ..)(min 3213

21≤≤≤≤≤≤++=x x x t s x x x X f

答:程序如下:

function pop=initpop(popsize,chromlength)

pop=round(rand(popsize,chromlength));

function pop2=decodebinary(pop)

[px,py]=size(pop);

for i=1:py

pop1(:,i)=2.^(py-i).*pop(:,i);

end

pop2=sum(pop1,2);

function pop2=decodechrom(pop,spoint,length)

pop1=pop(:,spoint:spoint+length-1); pop2=decodebinary(pop1);

function [eval]=f(sol)

numv=size(sol,3); x=sol(1:numv); eval=x1+x2+x3;

function [sol,eval]=fitness(sol,options)

numv=size(sol,3)-1; x=sol(1:numv); eval=f(x); eval=-eval;

bounds=ones(3,1)*[7<=x1<=15,3<=x2<=6,5<=x3<=10]; [p,endPop,bestSols,trace]=ga(bounds,'fitness') function fitvalue=calfitvalue(objvalue)

global Cmin; Cmin=0; [px,py]=size(objvalue);

for i=1:px

if objvalue(i)+Cmin>0

temp=Cmin+objvalue(i);

else

temp=0.0;

end

fitvalue(i)=temp;

end

fitvalue=fitvalue';

function [newpop]=selection(pop,fitvalue)

totalfit=sum(fitvalue); fitvalue=fitvalue/totalfit; fitvalue=cumsum(fitvalue); [px,py]=size(pop); ms=sort(rand(px,1)); fitin=1; newin=1;

while newin<=px

if(ms(newin))

newpop(newin)=pop(fitin); newin=newin+1;

else fitin=fitin+1;

end

end

function [newpop]=crossover(pop,pc)

[px,py]=size(pop); newpop=ones(size(pop));

for i=1:2:px-1

if(rand

newpop(i+1,:)=[pop(i+1,1:cpoint),pop(i,cpoint+1:py)];

else newpop(i,:)=pop(i); newpop(i+1,:)=pop(i+1);

end

end

相关文档
最新文档