maxiter = 200; % 迭代次数
sizepop = 10; % 种群数量
% 频率范围
popmin1 = -1; popmax1 = 1; % x1 频率
popmin2 = -1; popmax2 = 1; % x2 频率
Qmin = 0.1; % 最小频率
Qmax = 0.5; % 最大频率
impluse = 0.4; % 脉冲率
Vmin = -1; % 最小速度
Vmax = 1; % 最大速度
%% 初始化种群
for i=1:sizepop
x1 = popmin1 + (popmax1-popmin1)*rand;
x2 = popmin2 + (popmax2-popmin2)*rand;
pop(i,1) = x1;
pop(i,2) = x2;
fitness(i) = fun([x1,x2]);
V(i,1)=0;
V(i,2)=0;
end
% 记录一组最优值
[bestfitness,bestindex]=min(fitness);
zbest=pop(bestindex,:); % 全局最佳
gbest=pop; % 个体最佳
fitnessgbest=fitness; % 个体最佳适应度值
fitnesszbest=bestfitness; % 全局最佳适应度值
%% 迭代寻优
for i=1:maxiter
for j=1:sizepop
Q = Qmin + (Qmax-Qmin)*rand;
V(j,:) = V(j,:) + (pop(j,:)-zbest)*Q;
% V--x1
if V(j,1)>Vmax
V(j,1)=Vmax;