# biological example: synchronization of n fireflies

Consider a biological example of the synchronization of n fireflies flashing. The fireflies can be modeled mathematically as periodic oscillators which tend to synchronize their flashing until they are flashing in phase with each other. A state value of ${\tau_i=1}$ corresponds to a flash, and after each flash, the firefly automatically resets its internal timer (periodic cycle) to ${\tau_i=0}$. The synchronization of the fireflies can be modeled as a hybrid system because every time one firefly flashes, the other firefly notices and jumps ahead in its internal timer ${\tau_i}$ by ${(1+\varepsilon)\tau_i}$, where ${\varepsilon}$ is a biologically determined coefficient. This happens until eventually all fireflies synchronize their internal timers and are flashing simultaneously. We assume that the fireflies are fully interconnected as in Figure 1.

Now consider a population of n fireflies, then the population can be represented with a vector state ${x = [\tau_1,\tau_2,\ldots,\tau_n]^\top\in\Re^n}$.

Furthermore, the fireflies population can be modeled as a hybrid system given by

Notice that here no external inputs are being considered. The only event that affects the flashing of a firefly ${\tau_i}$ is the flashing of the other firefly.

A solution to the hybrid system with ${T=7,\ J=100,\ r=1,\ \varepsilon=0.2}$ is depicted in Figure 2. Both the projection onto ${t}$ and ${j}$ are shown.

These simulations reflect the expected behavior of the hybrid system. The fireflies initially flash out of phase with one another and then synchronize to flash in the same phase.

Source code for Simulation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: run.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Fireflies example - Lite simulator version
%% initial conditions
clear all
close all
clc

% initial conditions
N = 20; % Poblation size
x0 = rand(N,1);
% simulation horizon
T = 7;
J = 100;
% simulation horizon
TSPAN = [0 T];
JSPAN = [0 J];

% rule for jumps
% rule = 1 -> priority for jumps
% rule = 2 -> priority for flows
% rule = 3 -> no priority, random selection when simultaneous conditions
rule = 1;

options = odeset('RelTol',1e-6,'MaxStep',.1);

% constants
global n epsilon;
n = N; %# of state components
epsilon = 0.2; % Bilogical parameter

%% simulate
[t j x] = HyEQsolver(@f,@g,@C,@D,x0,TSPAN,JSPAN,rule,options);

%%
% plot solution
figure(1)
clf
subplot(2,1,1),plotHarc(t,j,x);
grid on
title(['Evolution of firefly oscilation for a ',num2str(n), ' population size (flow)'])
xlabel('time')
ylabel('x')

subplot(2,1,2),plotjumps(t,j,x)
grid on
ylabel('x')
xlabel('jumps')
title(['Evolution of firefly oscilation for a ',num2str(n), ' population size (jump)'])
%% plot hybrid arc
plotHybridArc(t,j,x);
xlabel('j')
ylabel('t')
zlabel('x')


Flow map


function out = f(z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: f.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Flow map
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
global  n;

% state
x =z(1:n);

% flow map
%xdot=f(x,u);
xdot = ones(n,1);

out = xdot;


Jump map

function out = g(z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: g.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Jump map
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
global  n epsilon;

% state
x = z(1:n);

xplus = x;
for ix = 1:n
if (1+epsilon)*x(ix)<1
xplus(ix) = (1+epsilon)*x(ix);
else
xplus(ix) = 0;
end
end

out = xplus;


Flow set


function [v] = C(z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: C.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Flow set
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
global  n;

% state
x =z(1:n);

if (sum(x<1)==n)  % flow condition
v = 1;  % report flow
else
v = 0;   % do not report flow
end



Jump set



function [v] = D(z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab M-file       Project: HyEQ Toolbox  @ Hybrid Dynamics and Control
% Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software
%
% Filename: D.m
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Description: Jump set
%
% Version: 1.0
% Required files: -
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% constants
global  n;

% state
x =z(1:n);

if (sum(x>=1)>0)  % jump condition
v = 1;  % report jump
else
v = 0;   % do not report jump
end