function Figure9_2

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

close all;

load data_Figure9_2_a.dat;
trajectory=data_Figure9_2_a;
q=size(trajectory);
lengthtraj=q(1);

load data_Figure9_2_b.dat;
solvent=data_Figure9_2_b;

theta=[0:0.001:2*pi];

figure(1);
set(gca,'Fontsize',18);
[x,y,z]=sphere;
Cdata=zeros(length(z),length(z),3);
Cdata(:,:,1)=ones(length(z),length(z))*1;
Cdata(:,:,2)=ones(length(z),length(z))*0.88;
Cdata(:,:,3)=ones(length(z),length(z))*0.31;
[azimuth,elevation,r]=cart2sph(x,y,z);
azimuth=180+180*azimuth/pi;
s=surf(trajectory(lengthtraj,2)+x,trajectory(lengthtraj,3)+y,trajectory(lengthtraj,4)+z,Cdata,'EdgeColor','k');
hold on;
s.AlphaData = ((abs(azimuth-290))<90)|((abs(azimuth+70))<90);
s.FaceColor = 'flat';
s.FaceAlpha = 'flat';
grid on;
plot3(solvent(:,1),solvent(:,2),solvent(:,3),'.','Markersize',2);
box on;
plot3(trajectory(:,2),trajectory(:,3),trajectory(:,4),'r','Linewidth',2);
plot3([8 8 0],[0 0 0],[0 8 8],'k');
plot3([8 8],[0 8],[8 8],'k');
axis([0 8 0 8 0 8]);
xlabel('$x_1$','interpreter','latex');
ylabel('$x_2$','interpreter','latex');
zlabel('$x_3$','interpreter','latex');
set(get(gca,'zlabel'),'rotation',0)
set(gca,'Ytick',[0 4]);
view(16,16);
box on;
set(gca,'Fontsize',18);

% ==============================

X1=[-1:0.001:1];
h0=2*(3*sqrt(pi)/(8*sqrt(2)))*(1-X1.*X1);
h1=0.5*(1-X1.*X1.*X1);
h2=0.25*(2-3*X1+X1.*X1.*X1); 

figure(2);
set(gca,'Fontsize',18);
plot(X1,h0,'Linewidth',2,'Color','r');
hold on;
plot(X1,h1,'Linewidth',2,'Color','k','LineStyle','--');
plot(X1,h2,'Linewidth',2,'Color','g','LineStyle','-.');
axis([-1 1  0 1.1]);
set(gca,'Xtick',[-1 -0.5 0 0.5 1]);
set(gca,'XtickLabel',{'','','','',''});
h1=text(-1.1,-0.07,'$-R$');
set(h1,'interpreter','latex','Fontsize',18);
h2=text(-0.65,-0.07,'$-R/2$');
set(h2,'interpreter','latex','Fontsize',18);
h3=text(-0.024,-0.07,'$0$');
set(h3,'interpreter','latex','Fontsize',18);
h4=text(0.4,-0.07,'$R/2$');
set(h4,'interpreter','latex','Fontsize',18);
h5=text(0.9,-0.07,'$R$');
set(h5,'interpreter','latex','Fontsize',18);
h6=legend('$\frac{2}{\gamma \sigma_\mu} \, h_0$','$h_1$','$h_2$, $h_3$');
set(h6,'interpreter','latex','Fontsize',18);
h7=text(0.7,-0.11,'$x_1$');
set(h7,'interpreter','latex','Fontsize',18);
ylabel('$h_k$','interpreter','latex');
set(get(gca,'ylabel'),'rotation',0)
set(gca,'Fontsize',18);
