% Matlab code for the simulation of linear averaging algorithm.
xinitial = [+1 -1 +1 -1 +1]';
time_steps = 50;

% Complete graph
Acomplete = 1/5*ones(5,5);
xcompletefinal = Acomplete^(time_steps) * xinitial
% result: xcompletefinal is 0.2 [1; 1; 1; 1; 1]

% Ring topology
Aring = [1/3 1/3 0 0 1/3;
1/3 1/3 1/3 0 0;
0 1/3 1/3 1/3 0 ;
0 0 1/3 1/3 1/3;
1/3 0 0 1/3 1/3];
xringfinal = Aring^(time_steps) * xinitial
% result: xringfinal is 0.2 [1; 1; 1; 1; 1]

% Star topology. center = node 1
Astar = [1/5 1/5 1/5 1/5 1/5;
1/2 1/2 0 0 0;
1/2 0 1/2 0 0;
1/2 0 0 1/2 0
1/2 0 0 0 1/2];
xstarfinal = Astar^(time_steps) * xinitial
% result: xstarfinal is 0.3846 [1; 1; 1; 1; 1]