function Figure4_4c

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

close all;
rand('state',6);

dt=0.02;
finaltime=15*60;  % 15 minutes
D=0.0001;         % 10^{-4} mm^2 sec^{-1}
speed=0.01;
lambda=speed*speed/(2*D);

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

for i=1:numberofrealisations
    X(i,1)=0.4;
    if (rand(1,1)<0.5)
       velocity=speed;
    else
       velocity=-speed;
    end
    for j=2:n
        if (rand(1,1)<lambda*dt)
            velocity=-velocity;
        end
        X(i,j)=X(i,j-1)+velocity*dt;
        if (X(i,j)<0)
            X(i,j)=-X(i,j);
            velocity=-velocity;
        end
        if (X(i,j)>1)
            X(i,j)=2-X(i,j);
            velocity=-velocity;
        end
    end
end

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

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

figure(1);
set(gca,'Fontsize',18);
plot(Xplot(5,:),timeplot,'Color','r');   
hold on;
plot(Xplot(1,:),timeplot,'Color',[0.9 0.9 0]);   
plot(Xplot(2,:),timeplot,'Color',[0.6 0.9 0.3]);   
plot(Xplot(3,:),timeplot,'Color','k');   
plot(Xplot(4,:),timeplot,'b');   
plot(Xplot(6,:),timeplot,'Color','m');   
plot(Xplot(7,:),timeplot,'Color',[0.3 0.6 0.9]);   
plot(Xplot(8,:),timeplot,'Color',[0.9 0.3 0.6]);   
plot(Xplot(9,:),timeplot,'g');   
plot(Xplot(10,:),timeplot,'Color','c');   
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);
