Exercise 3
Consider random walks of some object at two dimensions, with fixed step length d = 1 and with random step orientation, starting at Q(-2.5, 2.5). There is a straight wall between two points A(-2.5, -2.5) and B(2.5, 2.5). We assume that whenever the object hits the wall, it is reflected, keeping the total step length d.
We want to use an algorithm, which works for any choice of A and B, and which can be easily generalized to treat random
walks within an arbitrary polygon. There are some subtleties to be considered, see lecture.
The proposed solution is : Create a function "makestep" to perform a (partial) single step such that in the main program a complete step of length "dstep" is done via a while loop:
dd=dstep;
while(dd>0.)makestep(a1,a2,b1,b2,...);
"a1,a2" and "b1,b2" are the coordinates of A and B.
Write the corresponding program. As a first test, plot the trajectories with Nsteps = 100 steps per walk. As a very first test, one should consider only few simulations, print out positions and angles, and check if one gets what is expected (correct reflection angles, trajectories do no cross the wall, etc). Then make a plot for Nsim = 50 simulations, always with Nsteps = 100 steps per walk. The result should look like this :
Next plot the positions of the endponts of the walks, for Nsim = 500 simulations, with Nsteps = 100 steps per walk. Discuss the results, which should look like this:
What is the probability P that the object ends up (final position) in a square of side length 1.0, with the sides parallel to x and y axes, at some position M (center of the square). Investigate four cases:
M0 (-4, 0), M1 ( 0, 4), M2 ( 4, 0), M3 ( 0, -4).
Doing 500 000 simulations, one should find:
P0 = 0.0574, P1 = 0.0576, P2 = 0.0190, P3 = 0.0188.
For the graphs, take this as basis, and modify the script.
In case of problems in understanding basic notions of the programming langage C: see
C tutorial
The solution of this exercise can be found here