我有以下问题:
创建一个程序,其中粒子将针对以下两种情况执行N=1000步骤的随机漫步: i)在1D系统中ii)在2D系统中。程序必须计算平均值(S),其中S是粒子访问了至少一个time.You的网格位置的数量,将进行10000次运行并找到10个点(每100步一个,从0到1,000),这将是相对于时间t的平均值(S)的10000 runs.Do图。
我写了这段代码:
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()更新
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)更新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()谢谢!
发布于 2011-11-19 02:14:01
1)是的,10000步指的是期望的精度-您必须获得10000次随机漫步的时间t = 100, 200 ... 1000的平均访问单元数。要获得数据,您必须为每次随机游走累积访问单元的数量(共10000个)。为此,您必须在p循环之外初始化问题(即初始化his_pos和means)。在p循环中,你应该初始化你的随机游走--得到你的网格,初始位置和你要写入结果的列表。因此,问题init将如下所示
import scipy as sc
import matplotlib.pyplot as plt
plegma=1000
his_pos=[] # list which holds the position of the particle in the grid
means=[] #list which holds the means
for p in range(10000):
#-------Initialize problem--------
in_pos = int(sc.random.randint(0,len(grid),1)) #initial position of particle
grid=sc.ones(plegma) # grid full of available positions(ones)
his_pos.append([])初始化之后,我们需要执行随机遍历-- i循环。目前,您只进行了10步的随机漫步(len(range(0,1000,100)) == 10),但是漫步应该包含1000步。因此,i循环应该是
for i in range(1000):在遍历过程中,您必须以某种方式标记访问过的单元。最简单的方法是将grid[in_pos]更改为0。然后,每第100步,你需要计算访问的单元格的数量。实现这一点的方法如下
if i % 100 == 0:
# count the number of 0s in grid and append it to his_pos[p]最后,在1000次随机遍历的末尾,您的his_pos将是列表的(10000*10)列表,其中应该采用列方式的方法。
更新:
为了在运行过程中不丢失信息,我们应该将存储p-th运行结果的列表附加到包含所有结果的列表中。后者是his_pos。为此,我们可以将空列表附加到his_pos并在p-th运行期间使用结果填充它,或者在p-th运行之前初始化空列表并在p-th运行之后将其附加到his_pos。然后,算法将如下所示:
#-------Initialize problem--------
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 run--------
in_pos = int(sc.random.randint(0,len(grid),1)) #initial position of particle
grid=sc.ones(plegma) # grid full of available positions(ones)
num_cells = [] # list which holds number of visited cells during run
for i in range(1000):
# make i-th step, get particle position
# mark particle position on grid as "visited"
if (i+1) % 100 == 0: # on each 100th step (steps count from 0, thus i+1)
# count the number of "visited positions" in grid
# append it to num_cells
# append num_cells to his_pos
# find column-wise means for his_pos发布于 2011-11-17 06:02:06
我不太清楚这个问题所要求的是什么(您的方程式中考虑了时间t?,point指的是什么?),但是相对于您试图执行的操作,我知道您必须将每次迭代产生的每个或您的10均值mean_his数组包含在最终的10000x10 means数组中。
每个mean_his阵列由具有100个步骤的1D阵列制备而成。我将通过一个十步数组来举例说明,这些步骤必须每两步取平均(而不是每100步取1000步):
>>> his_pos = [1,2,3,4,5,6,7,8,9,10] #the ten positions
>>> history = np.array(his_pos)
>>> history
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> ar2 = np.reshape(history, (-1,2)) # group in two in order to calculate mean
>>> ar2
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10]])
>>> mean_his = ar2.mean(1)
>>> mean_his
array([ 1.5, 3.5, 5.5, 7.5, 9.5])
>>> 然后,将mean_his附加到means 10000次,并以类似的方式计算平均值(请注意,为了避免每次重复初始化,必须在outter循环外部初始化mean )。
https://stackoverflow.com/questions/8124032
复制相似问题