Exercise 2
Consider one-dimensional random walks with initial positions x = 0 (at t = 0), where a step Δx = ±xstep (for a given xstep) is realized with probabilities w+ = w- = ½ , during a time interval tstep (for a given tstep). Be x the position after Nstep steps (which corresponds to the time t = tstep × Nstep.
Write the program to generate Nsim walks, where each walk consists of Nstep steps, and determine the x-distribution (the distribution of the x-values after Nstep steps), given (in the program) in terms of the two tables xmi[i] and phi[i], as explained here.
The x-distribution should coincide with the exact solution pEX, so compute as well pEX for a given sub-interval, using the table fun[i] for the latter. Hint : pEX for a given interval I is equal to the sum of all pEX(xi,tn), considering in the sum all expressions with xi ∈ I, using here the notation of the lecture. So the structure of the code for this part should be:
fun[j]=0;
for(np=0;... // varying np (which corresponds to x+) to sum over terms
xnp=... // computing the x-value which corresponds to np
if(xnp >= x-dx/2 && xnp <= x+dx/2) { //select xnp in the current interval
... //compute p_EX for given np
fun[j] += ... //summing the relevant terms
}
} //the final result must be divided by dx
Print xmi[i], phi[i], fun[i] to standard output. So when executing the program, we should get three columns, representing xmi[i], phi[i], fun[i].
Use Nsim = 10000, Nstep = 9, xstep = 1, tstep = 1, Nsub = 3, xmin = -10.5, xmax=10.5
For a possible graphical representation of the results, see here.
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