function Figure3_2a

% Matlab code for Figure 3.2(a) in book:
% Radek Erban and Jon Chapman, Stochastic Modelling of Reaction-Diffusion
% Processes, Cambridge University Press, 2019

close all;
randn('state',10);

dt=0.001;
finaltime=1; 

n=finaltime/dt;
numberofrealizations=10000;
hist=zeros(50,1);
x=[-4.9:0.2:4.9];

for j=1:10
    dX = sqrt(dt)*randn(n,numberofrealizations);   
    position=sum(dX);             
    for i=1:numberofrealizations
        hist(round(5*position(i)+25.5))=hist(round(5*position(i)+25.5))+1;
    end
end

hist=hist/numberofrealizations/2;

gaussian=zeros(201,1);
xg=[-5:0.05:5];
gaussian=exp(-xg.*xg/2)/(sqrt(2*pi));

figure(1);
set(gca,'Fontsize',18);
h1=bar(x,hist);
set(h1,'FaceColor',[0.88 0.88 0.88]);
hold on;
h2=plot(xg,gaussian,'r','Linewidth',3);
xlabel('$x$','interpreter','latex');
ylabel('$p(x,1)$','interpreter','latex');
h3=legend([h1 h2],'$\;$SSA','(3.33)');
set(h3,'interpreter','latex','Fontsize',18,'location','northeast');
set(gca,'XTick',[-4 -3 -2 -1 0 1 2 3 4]);
axis([-4 4 0 0.45]);
set(gca,'Fontsize',18);
