File Exchange

image thumbnail

Video Tutorial of Particle Swarm Optimization (PSO) in MATLAB

version (2.27 MB) by Yarpiz
In this video tutorial, implementation of PSO in MATLAB is discussed in detail.


Updated 27 May 2016

View License

In this video tutorial, implementation of Particle Swarm Optimization (PSO) in MATLAB is discussed in detail. In the first part, theoretical foundations of PSO is briefly reviewed. In the next two parts of this video tutorial, PSO is implemented line-by-line and from scratch, and every line of code is described in detail.
The video tutorial is available to watch online, via Yarpiz YouTube Channel, and to download as MP4 files. The project files of the video tutorial and lecture notes written by instructor during the course, are also available to download. The instructor of this course is Dr. S. Mostapha Kalami Heris, PhD of Control and Systems Engineering.
Video Files on YouTube

Part 1 of 3 - Theoretical Foundations:

Part 2 of 3 - Implementation in MATLAB:

Part 3 of 3 - Improvements:

The course is also freely available via Udemy:

For more information and download the video and project files and lecture notes for this tutorial, see:

After watching this video tutorial, you will be able to know what is PSO, and how it works, and how you can use it to solve your own optimization problems. Also, you will learn how to implement PSO in MATLAB programming language. If you are familiar with other programming languages, it is easy to translate the MATALB code and rewrite the PSO code in other programming languages.

Cite As

Yarpiz (2020). Video Tutorial of Particle Swarm Optimization (PSO) in MATLAB (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (16)

How can I populate my dataset into this code ?

I run the basic PSO with initiating wdamp but it was updating by BestCost, can you please help me out.

close all;

%% Problem Definition

CostFunction = @(x) Sphere(x); % Cost Function

nVar = 5; % Number of Unknown (Deceision) Variables

VarSize = [1 nVar]; % Matrix Size of Decision Variables

VarMin = -10; % Lower bound of Decesion Variable
VarMax = 10; % Upper bound of Decesion Variable

%% Parameters of PSO

MaxIt = 100; % Maximum Number of Iterations

nPop = 50; % Population Size (Swarm Size)
wdamp = 0.99; % Damping Ratio of Inertia Coefficient
w = 1; % Inertia Coefficient
c1 = 2; % Personal Acceleration Coefficient
c2 = 2; % Social Acceleration Coefficient

%% Initialization

% The Particle Template
empty_particle.Position = [];
empty_particle.Velocity = [];
empty_particle.Cost = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];

% Create Population Array
particle = repmat(empty_particle, nPop, 1);

% Initialize Global Best
GlobalBest.Cost = inf;

% Initialize Population Members
for i=1:nPop

% Generate Random Solution
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);

% Initialize Velocity
particle(i).Velocity = zeros(VarSize);

% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);

% Update the Personal Best
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;

% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;

% Array to Hold Best Cost Value on Each Iteration
BestCosts = zeros(MaxIt, 1);

%% Main Loop of PSO

for it=1:MaxIt

for i=1:nPop

% Update Velocity
particle(i).Velocity = w*particle(i).Velocity ...
+ c1*rand(VarSize).*(particle(i).Best.Position - particle(i).Position) ...
+ c2*rand(VarSize).*(GlobalBest.Position - particle(i).Position);

% Update Position
particle(i).Postion = particle(i).Position + particle(i).Velocity;

% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);

% Update Personal Best
if particle(i).Cost < particle(i).Best.Cost

particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;

% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;



% Store the Best Cost Value
BestCosts(it) = GlobalBest.Cost;

% Display Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);

% Damping Interia Coefficient
w = w * wdamp;

John Cap

anyone who knows how to implement the visualization of the locations of all the particles every iteration?

zaki taouil


Isra Haroun

excellent tutorial! simple and clear. I have a question in the velocity and position update, should'nt it be particl(i).position=particle(i-1).position+particle(i).Velocity? and same for velocity. According to the notes in video 1, this is how it should be, am I missing something? Thanks

can we use the stopping criteria with tolerance of error ?
with 'while' loop not based in its iteration ?

Ali Soliman


aku kedua

hello,i need nspso ,do you have link download ?,thank

very clear and easy to understand.

Nice lecture to understand PSO.Thanks

Thanks Yarpiz it is very use full to understand concept of PSO


Image changed.
Udemy link added.

Post image added.

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

YTEA101 PSO in MATLAB Video Tutorial/Files and Codes/