2011-11-14 61 views
2

我有以下問題:蟒蛇,scipy-功課 - 如何保持粒子的位置的歷史網格

創建一個程序,其中一個粒子將執行這兩個爲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() 

謝謝!

+0

如果這是一個家庭作業的問題(它肯定看起來像一個),那麼請標誌是這樣。 – talonmies

+0

「程序必須計算出其中的S是......」您可以重新編輯您的文本以使其可以理解嗎? – joaquin

+0

@joaquin:好的,我編輯並更新了帖子。如果你能解釋我的話,我將不勝感激! – George

回答

0

1)是的,10000步指的是期望的準確性 - 您必須獲得10000次隨機行走時的t = 100, 200 ... 1000的平均訪問單元數。要獲取數據,您必須爲每次隨機遊走(10000次)累計訪問的單元格數量。要做到這一點,你必須初始化你的問題(即,初始化his_posmeans以外的p循環。在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運行結果被存儲,與所有結果的列表清單。後者是his_pos。要做到這一點,我們可以追加空單his_pos並在p個運行與結果填充它,或之前p個運行初始化空列表p個運行追加到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 
+0

:你好,謝謝你的提示。我不明白p循環中的'his_pos.append([])'以及如何在最後加上零計數。我更新了我的帖子,我編輯了代碼給你的建議。 – George

+0

我們需要獲得超過10000次運行的平均值,對嗎?所以,價值觀必須以某種方式積累。在這個例子中,我將'his_pos'作爲結果的累加器,並在其後添加一個空列表,它將累加第p次運行的結果。同樣,您可以在*'p'-th run之前初始化這個累加器*,並在*'p'-th運行之後將它追加到'his_pos' *。我將編輯答案來展示這個機會。至於零計數,首先你應該將一些單元格標記爲零(即使在修改後的代碼中你也不這樣做)。然後你可以簡單地把零計爲'1000 - sc.sum(grid)'。 –

+0

:我再次更新了我的代碼,我想我現在已經很近了..你說過爲his_pos找到列方式,但his_pos是1D。 – George

0

我不確定是否理解問題的要求(時間t在您的方程式中考慮,point是指什麼?),但相對於您嘗試執行的操作,我瞭解您必須在最終的10000x10 means數組中包含每個迭代產生的每個或10個平均值陣列。

每個mean_his陣列從100個步驟的1D陣列準備。

>>> 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_hismeans 10000倍,同樣計算平均值(注意,手段有:我會用十步一陣列已經被平均(而不是1000,每100)每兩個舉例說明這種在外部循環之外進行初始化,以便在每次重複時不被初始化)。

+0

:你好,時間是steps.I只想最後有10分,而不是一個數組10000x10。 – George