I have a random walk algorithm that I want to implement, but I am getting a recursion depth issue. I am making a list of points that Z
particles can be in over the domain $(-L/2,L/2)$. I am using a double For
loop that will generate the values for the points of the different particles given this domain. The code is:
Remove["Global`*"]
Z = 3;
NMAX = 3;
l = 1;
L = 100;
x[0, i_] = 0;
For[i = 0, i <= Z, i++,
For[n = 1, n <= NMAX, n++,
If[Abs[x[n - 1, i] + l Cos[2 π RandomReal[]]] >= L/2,
x[n, i] := x[n, i] = x[n - 1, i] - l Cos[2 π RandomReal[]],
x[n, i] := x[n, i] = x[n - 1, i] + l Cos[2 π RandomReal[]]]]]
Table[x[n, i], {n, 0, NMAX}, {i, 0, Z}] // TableForm
$l$ is the uniform step that the particle will take and the If
statement makes sure that the x
position is assigned such that the value is within the domain. I would like to extend this also to y
coordinates but that is something I will tackle after I can work this kink out.
I also started the list of particles (at time $n=0$) at position (0,0). The issue I am running into is that while the position for all (3) particles, starts off at 0 and then the first time step is shown but after that the out put is x[2,i]
and x[3,i]
. It works for the first time step, but not for any subsequent time steps. The error that I get is:
Recursion depth of 1024 exceeded.
I tried setting the recursion limit to 4000, since I found this somewhere else and it works for some problems, but ...
I am not sure what setting the recursion limit does.
It didn't work for me.
Any help would be appreciated!