Hi guys, I am in dilemma as I could not find out which variable is in wrong size.
Error in PSO2 (line 69): swarm(it, 2, 1) = inertia*swarm(it, 2, 1) + correction_factor2*rand*(swarm(it, 3, 1) - swarm(it, 1, 1)) + correction_factor1*rand*(swarm(gbest, 3, 1) - swarm(it, 1, 1)); %x velocity component
% Simulates the movements of a swarm to minimize the objective function
% The swarm matrix is
%
% swarm(index, [location, velocity, best position, best value], [x, y components or the value component])
iterations = 50;inertia = 0.9;correction_factor1 = 2;correction_factor2 = 2;swarm_size = 49;% ---- initial swarm position -----
index = 1;for it = 1 : sqrt(swarm_size) for jt = 1 : sqrt(swarm_size) swarm(index, 1, 1) = it; swarm(index, 1, 2) = jt; index = index + 1; end endswarm(:, 4, 1) = 1000; % best value so far
swarm(:, 2, :) = 0; % initial velocity
%%Iterations
for iter = 1 : iterations %-- evaluating position & quality ---
for it = 1 : swarm_size swarm(it, 1, 1) = swarm(it, 1, 1) + swarm(it, 2, 1); %update x position
swarm(it, 1, 2) = swarm(it, 1, 2) + swarm(it, 2, 2); %update y position
if gt(swarm(it, 1, 1),0) KP = swarm(it, 1, 1); if gt(swarm(it, 1, 2),0) KI = swarm(it, 1, 2); end end run runbmPID.m; max([J1, J2, J3, J4, J5, J7, J8]); val = max([J1, J2, J3, J4, J5, J7, J8]); % fitness evaluation
if val < swarm(it, 4, 1) % if new position is better
swarm(it, 3, 1) = swarm(it, 1, 1); % update best x,
swarm(it, 3, 2) = swarm(it, 1, 2); % best y postions
swarm(it, 4, 1) = val; % and best value
end end temp = min(swarm(:, 4, 1)); z = swarm(:, 4, 1); gbest = find(z == temp); % best value index
KP = swarm(gbest, 3, 1); KI = swarm(gbest, 3, 2); disp(sprintf('temp[%g][%g] = %g',KP,KI,temp)); %--- updating velocity vectors
for it = 1 : swarm_size swarm(it, 2, 1) = inertia*swarm(it, 2, 1) + correction_factor2*rand*(swarm(it, 3, 1) - swarm(it, 1, 1)) + correction_factor1*rand*(swarm(gbest, 3, 1) - swarm(it, 1, 1)); %x velocity component swarm(it, 2, 2) = inertia*swarm(it, 2, 2) + correction_factor2*rand*(swarm(it, 3, 2) - swarm(it, 1, 2)) + correction_factor1*rand*(swarm(gbest, 3, 2) - swarm(it, 1, 2)); %y velocity component
end %%Plotting the swarm
clf plot(swarm(:, 1, 1), swarm(:, 1, 2), 'KP') % drawing swarm movements
axis([-2 30 -2 30]); %pause(.2)
end
Best Answer