% Simulation of preferential attachment model (Chapter 2, Box C) % Written by David Sumpter %Number of positions (e.g. total number of papers, or sites for animals) m=2000; %Attachment constant c=1; %Exponent, always set to one in example in book. ek=1 %Set up initial positions x=zeros(m,1); x(1:m)=1; %Number of time steps maxt=10000; %Main loop for t=1:maxt %Take a random position pos=floor(rand*m)+1; %Stores how many were taken from that position and sets population at that position add(t)=x(pos); x(pos)=0; %Now add the removed individuals one by one to other positions. for rep=1:add(t) r=rand; i=1; p=x(i)+c; sx=sum((x+c).^ek); %Loop through until we find the position it should go in. while ((r>p/sx) & (i