366 Appendix B
[E_new,constr] = func1(x);
if E_new < E_old
E_old = E_new;
else
if exp(-(E_new-E_old)/E_old)> rand
E_old = E_new;
end
x = x - perturb_x;
end
px(j) = j;
py(j) = E_new;
if E_new < best_obj
best_obj = E_new;
bestx = x;
flag = 0;
end
[j bestx best_obj]
if flag > 1000
break;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File name func1.m
% Enter the function to be optimized
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function [y,constr] = func1(x)
y = 20 + x(1)*x(1)-10*cos(2*pi*x(1)) + x(2)*x(2)-
10*cos(2*pi*x(2));
constr(1) = 10;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File name pso.m
% Particle ...