I want to use the random walk simulation A + A=0 of the chemical reaction, here is my code I use 0 Spaces, 1 represents the particle grid; 1 + 1=0
clear; CLC
IterMax=20.
Nx=10;
Ny=10;
M_0=10; % the number of particles initial
A=zeros (Nx, Ny);
N=Nx * Ny;
% linear index S
Ind=sub2ind (size (A), 1: Nx, 1: Ny);
The index=randperm (N, M_0); % choose 10 element in A randomly
A (index)=1
S=find (A==1);
% of each particle seat
M=zeros (1, iterMax);
M (1)=M_0;
R=zeros (1, iterMax);
R (1)=0.25;
I=fix (r. * M) + 1;
For n=1: iterMax
For j=2: iterMax
R (j)=rand (1);
End
If (0 & lt; R (n)) & amp; & (r (n) & lt;=0.25)
DS=- Ny;
If (S (I) + dS) & lt;=N & amp; & (S (I) + dS) & gt; 0
If A ((S (I) + dS)==0)
A (S) (I)=0;
A (S (I) + dS)=1;
S (I)=S (I) + dS;
M=M (n) (n + 1);
The else
A (S) (I)=0;
A (S (I) + dS)=0;
K=find (S==S (I) + dS);
S (k)=[];
S (I)=[];
M=M (n) (n + 1) - 2;
If M (n + 1) & lt; 0
Break
End
End
The else
S (I)=S (I);
M (n + 1)=M (n)
End
Elseif (0.25 & lt; R (n)) & amp; & (r (n) & lt;=0.5)
DS=Ny;
,,,
Error:
Why in addition to the M (1)=10, other M 0 loop is what went wrong?