一段程序在matlab2011b上运行出现的问题,求助

2025-03-21 11:58:19
推荐回答(1个)
回答1:

我在别地方也看到您的提问,我来为你解答:

clc;clear;
c1=1.49445;
c2=1.49445;
maxgen=100;
sizepop=20;
popmax=5;popmin=-5;
Vmax=1;Vmin=-1;
for i=1:sizepop
pop(i,:)=5*rands(1,2);
V(i,:)=rands(1,2);
fitness(i)=fun(pop(i,:));
end
function y=fun(x)
y=-20*exp(-0.2*sqrt((x(1)^2+x(2)^2)/2))-exp((cos(2*pi*x(1))+cos(2*pi*x(2)))/2)...
+20+exp(1);
[bestfitness bestindex]=min(fitness);
zbest=pop(bestindex,:);
gbest=pop;
fitnessgbest=fitness;
fitnesszbest=bestfitness;
for i=1:maxgen
for j=1:sizepop
V(j,:)=V(j,:)+c1*rand*(gbest(j,:)-pop(j,:))+c2*rand*(zbest-pop(j,:));
V(j,find(V(j,:)>Vmax))=Vmax;
V(j,find(V(j,:)
pop(j,:)=pop(j,:)+0.5*V(j,:);
pop(j,find(pop(j,:)>popmax))=popmax;
pop(j,find(pop(j,:) fitness(j)=fun(pop(j,;));
end
for j=1:sizepop
if fitness(j) gbest(j,:)=pop(j,:);
fitnessgbest(j)=fitness(j);
end
if fitness(j) zbest=pop(j,:);
fitnesszbest=fitness(j);
end
end
yy(i)=fitnesszbest;
end

你的采纳是我前进的动力,还有不懂的地方,请继续追问。