I am having some difficulties implementing a Monte Carlo simulation in order to compare the results to analytical calculations. The following code simulates a case with one random walker and a stationary trap. The results from the code below match the analytical calculations. The average walk-length for a 5x5 lattice turns out to be about 31.66, which is correct.
stepTypes = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
pos1 = RandomInteger[{0, 4}, 2];
While[pos1 != {2, 2}, (*{2,2} is the stationary trap*)
pos1 = Mod[pos1 + RandomChoice[stepTypes], 5];
index++
]
Print["Walk length is:", index];
index = 0;
But when analyzing the same case, only replacing the trap with another random walker. The results dont match at all, they tend to be higher than the analytical and the difference increases hyperbolically as the lattice size grows.
stepTypes = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
pos1 = RandomInteger[{0, 4}, 2];
pos2 = RandomInteger[{0, 4}, 2];
While[pos1 != pos2,
pos1 = Mod[pos1 + RandomChoice[stepTypes], 5];
pos2 = Mod[pos2 + RandomChoice[stepTypes], 5];
index++;
];
Print["Walk length is:", index];
index = 0;
By simulating the first case I was able to tell that my boundary conditions and the random number generation work correct. Not sure if the problem is in the way I synchronize the two walkers' movement? While loop tests the condition after both walkers have moved and increments the walk-length counter once both have moved as well. Any ideas of why the second case does not work? Perhaps it has to do with a subsequent random number generation?
Note: The original code runs this simulation N number of times and computes the average.
Analytics:
N Walker/Trap Two Walkers
---------------------------------
3 8.99 8.00
4 18.33 22.23
5 31.66 26.10
6 49.24 52.67
7 71.82 56.61
{{5, 31.09}, {7, 78.28}, {9, 128.01}, {11, 227.43}, {13, 254.77}, {15, 288.32}}
with the means being taken over 100 iterations. How does that compare to answers you expect? There was no exponential growth of run time as the lattice size increased. $\endgroup$ – m_goldberg Apr 18 '16 at 2:29