Optimization using Particle Swarm
A set of codes used for finding minimum of a problem. By defining appropriate fitness function desired goal can be accomplished say, finding optimum design parameters so that much more beautiful solution can be achieved! :)
About Function:
[El,fval] = pso(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a structurethat has the following inputs,
INPUT fields:
fitnessfcn: <Fitness function>
nvars: <Number of design variables>
lb: <Lower bound on X>
ub: <Upper bound on X>
Genrations: <Number of iterations>
Population: <Number of swarms (particles) used for optimization>
DispWB: <whether to disply waitbar or not '1' to display>
OUTPUT fields:
EL: < The values of optimized parameters>
fval: <Fitness value after optimization>
Example:
First, create a file to evaluate fitness function say, 'simple_fitness.m' as follows:
function y = simple_fitness(x)
y = 100*(x(1)^2 - x(2))^2 + (1 - x(3))^2 + abs(0.4 - x(2));
To minimize the fitness function, user need to pass a function handle to the fitness function as the first argument to the pso function, as well as other parameters of PROBLEM as stated above in pso(PROBLEM),
ObjectiveFunction = @simple_fitness;
nvars = 3; % Number of variables
LB = [0 0 0]; % Lower bound
UB = [3 5 10]; % Upper bound
Popln=200; % Number of particles
Genrtn=50; % Number of %iterations
WByn=1; % '1' = waitbar is ON
[x,fvalue]= pso(ObjectiveFunction, 'NumVar',nvars,'LowerBound',LB,'UpperBound',UB,'Population', ... Popln, 'Genrations', Genrtn, 'IncludeWB', WByn);
disp(x);
disp(fvalue);
After running code will get optimum parameter values more closer to, x(1)=0.6325; x(2)=0.4; x(3)=1;
Cite As
Swapnil Gaul (2026). Optimization using Particle Swarm (https://in.mathworks.com/matlabcentral/fileexchange/40609-optimization-using-particle-swarm), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- Mathematics and Optimization > Optimization Toolbox >
- Mathematics and Optimization > Global Optimization Toolbox > Particle Swarm >
Tags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
