Skip to main content
 首页 » 编程设计

python,scipy- homework -- 如何在网格中保存粒子位置的历史记录

2025年05月04日9lexus

我有以下问题:

创建一个程序,其中粒子将针对以下两种情况执行 N=1000 步的随机游走:i) 在一维系统中 ii) 在二维系统中。 该程序必须计算平均值 (S),其中 S 是粒子至少访问过一次的网格位置的数量。您将进行 10000 次运行并找到 10 个点(每 100 步一个点,从 0 到 1000),这将是 10000 次运行的均值。绘制均值 (S) 与时间 t 的关系图。

我做了这段代码:

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() 

谢谢!

请您参考如下方法:

1) 是的,10000 步指的是所需的精度 - 对于 10000 次随机游走,您必须在 t = 100, 200 ... 1000 时获得访问单元格的平均数。要获取数据,您必须为每次随机游走(10000 次)累积访问单元格的数量。为此,您必须在p 的 之外初始化您的问题(即初始化his_pos 和means)环形。在 p 循环中,您应该初始化您的随机游走 - 获取您的网格、初始位置和您将写入结果的列表。所以,问题初始化看起来像

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 次运行结果的列表附加到包含所有结果的列表中。后者是 his_pos。为此,我们可以将空列表附加到 his_pos 并在第 p 次运行期间用结果填充它,或者在 之前初始化空列表 p-th run 并将其附加到 his_pos after p-th run。该算法将如下所示:

#-------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