function Figure4_2a

% Matlab code for Figure 4.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.1;
finaltime=15*60;  % 15 minutes
D=0.0001;         % 10^{-4} mm^2 sec^{-1}

n=finaltime/dt+1;
numberofrealisations=10;
X=zeros(numberofrealisations,n);

for i=1:numberofrealisations
    X(i,1)=0.4;    
    for j=2:n
        X(i,j)=X(i,j-1)+sqrt(2*D*dt)*randn(1,1);
        if (X(i,j)<0)
            X(i,j)=-X(i,j);
        end
        if (X(i,j)>1)
            X(i,j)=2-X(i,j);
        end
    end
end

time=[0:dt:finaltime]/60;

Xplot(:,:)=X(:,1:5:n);
timeplot(:,:)=time(:,1:5:n);

figure(1);
set(gca,'Fontsize',18);
plot(Xplot(5,:),timeplot,'Color',[0.9 0.9 0]);   
hold on;
plot(Xplot(1,:),timeplot,'Color',[0.3 0.6 0.9]);   
plot(Xplot(2,:),timeplot,'Color',[0.9 0.3 0.6]);   
plot(Xplot(3,:),timeplot,'Color',[0.6 0.9 0.3]);   
plot(Xplot(4,:),timeplot,'g');   
plot(Xplot(6,:),timeplot,'r');   
plot(Xplot(7,:),timeplot,'b');   
plot(Xplot(8,:),timeplot,'k');   
plot(Xplot(9,:),timeplot,'c');   
plot(Xplot(10,:),timeplot,'m');   
xlabel('$x$ [mm]','interpreter','latex');
ylabel('time [min]','interpreter','latex');
set(gca,'XTick',[0 0.2 0.4 0.6 0.8 1]);
set(gca,'YTick',[0 2 4 6 8 10 12 14]);
axis([0 1 0 14]);
set(gca,'Fontsize',18);
