python,scipy-家庭作业——如何保持网格中粒子位置的历史记录
我有以下问题: 创建一个程序,其中粒子将在这两种情况下执行N=1000步的随机行走:i)在1D系统中;ii)在2D系统中。python,scipy-家庭作业——如何保持网格中粒子位置的历史记录,python,scipy,Python,Scipy,我有以下问题: 创建一个程序,其中粒子将在这两种情况下执行N=1000步的随机行走:i)在1D系统中;ii)在2D系统中。 程序必须计算平均值(S),其中S是粒子至少访问过一次的网格位置数。您将运行10000次,并找到10个点(每100步一个,从0到1000),这将是10000次运行的平均值。绘制与时间t相关的平均值(S) 我做了以下代码: import scipy as sc import matplotlib.pyplot as plt import random plegma=1000
程序必须计算平均值(S),其中S是粒子至少访问过一次的网格位置数。您将运行10000次,并找到10个点(每100步一个,从0到1000),这将是10000次运行的平均值。绘制与时间t相关的平均值(S) 我做了以下代码:
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-----------------------------
谢谢 我不确定问题要求的是什么(方程式中考虑了时间
t
,指的是什么?),但与您试图执行的操作相关,我理解您必须在最终10000x10means
数组中包含每次迭代产生的每个或您的10 meansmeans\u his
数组
每个平均值\u his
数组是从一个具有100个步骤的1D数组中准备的。我将以每两个步骤平均一次(而不是每100个步骤平均1000个)的十个步骤为例来说明这一点:
然后将mean\u his
附加到means
10000次,并以类似方式计算平均值(注意,为了避免每次重复都初始化means,必须在outter循环之外初始化means)。我不确定是否理解问题的含义(如果等式中考虑了时间t
,那么点
指的是什么?),但是相对于您尝试执行的操作,我理解您必须在最终10000x10平均值数组中包含每次迭代产生的每一个或10个平均值平均值数组
每个平均值\u his
数组是从一个具有100步的1D数组中准备的。我将用一个具有10步的数组来举例说明这一点,该数组必须每两步平均一次(而不是每100步平均1000步):
然后将mean\u his
附加到means
10000次,并以类似方式计算平均值(注意,为了避免每次重复都初始化平均值,必须在outter循环之外初始化平均值)。1)是的,10000个步骤表示所需的精度-必须获得时间t=100时访问的平均单元数, 200 ... 1000
用于10000次随机游动。要获得数据,您必须为每次随机行走累积访问的单元格数(10000个)。为此,您必须在p
循环之外初始化您的问题(即初始化his_pos
和意味着
)。在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(01000100))==10
),但行走应包含1000步。因此,i
循环应该是
for i in range(1000):
在行走过程中,你必须以某种方式标记已访问的单元。最简单的方法是将网格[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)列表,从中应取列式平均值
更新:
为了在整个运行过程中不丢失信息,我们应该将存储第次运行结果的列表附加到包含所有结果的列表中。后者是他的位置。为了实现这一点,我们可以将空列表附加到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
1) 是的,10000个步骤表示所需的精度-您必须获得时间t=100200时访问的单元格的平均数量。。。1000
用于10000次随机游动。要获得数据,您必须为每次随机行走累积访问的单元格数(10000个)。为此,您必须在p
循环之外初始化您的问题(即初始化his_pos
和意味着
)。在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(01000100))==10
),但行走应包含1000步。因此,i
循环应该是
for i in range(1000):
在行走过程中,你必须以某种方式标记已访问的单元。最简单的方法是将网格[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)列表,从中应取列式平均值
更新:
为了在整个运行过程中不丢失信息,我们应该将存储第次运行结果的列表附加到包含所有结果的列表中。后者是他的位置。为了实现这一点,我们可以将空列表附加到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
如果这是一个家庭作业问题(它看起来确实像一个),那么请将其标记为这样。