function Figure8_1

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

close all;
format long

alpha1=1;
alpha2=1;
alpha3=1;
sigma2=alpha3/(2*alpha2);
tau=[0:0.01:20];

alpha4=1;
alpha5=sqrt(alpha4*(alpha4-4*alpha2));
kappa1=alpha1*exp(-alpha4*tau/2).*(alpha4*sinh(alpha5*tau/2)/alpha5+cosh(alpha5*tau/2));
polp=[1 alpha4 alpha2*alpha4];
polq=[1 alpha4 (alpha1+alpha2*alpha4) alpha1*alpha4];
[r,p,k]=residue(polp,polq);
theoryVACF1=sigma2*(r(1)*exp(p(1)*tau)+r(2)*exp(p(2)*tau)+r(3)*exp(p(3)*tau))/sum(r);

alpha4=3;
alpha5=sqrt(alpha4*(alpha4-4*alpha2));
kappa3=alpha1*exp(-alpha4*tau/2).*(alpha4*sinh(alpha5*tau/2)/alpha5+cosh(alpha5*tau/2));
polp=[1 alpha4 alpha2*alpha4];
polq=[1 alpha4 (alpha1+alpha2*alpha4) alpha1*alpha4];
[r,p,k]=residue(polp,polq);
theoryVACF3=sigma2*(r(1)*exp(p(1)*tau)+r(2)*exp(p(2)*tau)+r(3)*exp(p(3)*tau))/sum(r);

kappaInf=alpha1*exp(-alpha2*tau);
theoryVACFInf=sigma2*exp(-alpha2*tau/2).*(cos(sqrt(alpha1-alpha2*alpha2/4)*tau)+sin(sqrt(alpha1-alpha2*alpha2/4)*tau)*(alpha2/2)/sqrt(alpha1-alpha2*alpha2/4));

figure(1);
set(gca,'Fontsize',18);
plot(tau,kappa1,'k','Linewidth',2);
hold on;
plot(tau,kappa3,'g--','Linewidth',2);
plot(tau,kappaInf,'r-.','Linewidth',2);
axis([0 10 -0.25 1.05]);
xlabel('$\tau$','interpreter','latex');
ylabel('$\kappa(\tau)$','interpreter','latex');
hl=legend('$\alpha_4=1$','$\alpha_4=3$','$\alpha_4=\infty$');
set(hl,'interpreter','latex','fontsize',18);
set(gca,'Xtick',[0 2 4 6 8 10]);
set(gca,'Ytick',[-0.2 0 0.2 0.4 0.6 0.8 1]);
box on;
set(gca,'Fontsize',18);

figure(2);
set(gca,'Fontsize',18);
plot(tau,theoryVACF1,'k','Linewidth',2);
hold on;
plot(tau,theoryVACF3,'g--','Linewidth',2);
plot(tau,theoryVACFInf,'r-.','Linewidth',2);
axis([0 20 -0.18 0.55]);
xlabel('$\tau$','interpreter','latex');
ylabel('$\chi(\tau)$','interpreter','latex');
h=legend('$\alpha_4=1$','$\alpha_4=3$','$\alpha_4=\infty$');
set(h,'interpreter','latex','Fontsize',18);
box on;
set(gca,'Fontsize',18);
