i have the following problem:
Create a program where a particle will execute a random walk for N=1000 steps for these two cases: i) in a 1D system ii) in a 2D system. The program must calculate the mean(S) where S is the number of grid positions where the particle visited at least one time.You will make 10000 runs and find 10 points (one for every 100 steps ,from 0 to 1000) , which will be the means of 10000 runs.Do the plot of mean(S) in relation to time t.
I did this code:
import scipy as sc
import matplotlib.pyplot as plt
import random
plegma=1000
grid=sc.ones(plegma) # grid full of available positions(ones)
for p in range(10000):
#-------------------Initialize problem-------------------------------------------------
his_pos=[] # list which holds the position of the particle in the grid
in_pos = int(sc.random.randint(0,len(grid),1)) #initial position of particle
means=[] #list which holds the means
#--------------------------------------------------------------------------------------
for i in range(0,1000,100):
step=2*sc.random.random_integers(0,1)-1 #the step of the particle can be -1 or 1
# Check position for edges and fix if required
# Move by step
in_pos += step
#Correct according to periodic boundaries
in_pos = in_pos % len(grid)
#Keep track of random walk
his_pos.append(in_pos)
history=sc.array(his_pos)
mean_his=sc.mean(history)
means.append(mean_his)
plt.plot(means,'bo')
plt.show()
UPDATED -------------------------------------
import scipy as sc
import matplotlib.pyplot as plt
import random
plegma=1000
his_pos=[] # list which holds the number of visited cells in the grid
means=[] #list which holds the means
for p in range(10000):
#-------------------Initialize problem-------------------------------------------------
grid=sc.ones(plegma) # grid full of available positions(ones)
in_pos = int(sc.random.randint(0,len(grid),1)) #initial position of particle
num_cells=[] # list which holds number of visited cells during run
#--------------------------------------------------------------------------------------
for i in range(1000):
step=2*sc.random.random_integers(0,1)-1 #the step of the particle can be -1 or 1
# Check position for edges and fix if required
# Move by step
in_pos += step
#Correct according to periodic boundaries
in_pos = in_pos % len(grid)
grid[in_pos]=0 # mark particle position on grid as "visited"
if (i+1) % 100 == 0:
number=1000-sc.sum(grid) # count the number of "visited positions" in grid
num_cells.append(number) # append it to num_cells
his_pos.append(num_cells) # append num_cells to his_pos
history=sc.array(his_pos)
mean_his=history.mean(1)
means.append(mean_his)
UPDATE 2 ----------------------------- .....
if (i+1) % 10 == 0:
number=1000-sc.sum(grid) # count the number of "visited positions" in grid
num_cells.append(number) # append it to num_cells
his_pos.append(num_cells) # append num_cells to his_pos
history=sc.array(his_pos)
mean_his=history.mean(0)
plt.plot(mean_his,'bo')
plt.show()
Thanks!