簡體   English   中英

Python:嘗試多次運行一個函數並為每次運行保存值

[英]Python: Trying to run a function multiple times and save values for each run

我正在嘗試從運行的數值模擬中獲取一些數據。 我有一個函數可以解析ODE並將所需的值保存在向量rv 我想運行我的函數1000次並獲取每個時間步長的rv平均值,但是我不知道該怎么做。

我的第一個想法是調用該函數1000次並將數據保存在數組中。

for i in range(1000):
    datavector = np.array(0)
    circle(N,dt,x0,y0,phi0,r,T,nu,v,Omega,R)
    datavector = np.append(datavector, rv)

但是當我這樣做時,我收到以下錯誤消息: NameError: name 'rv' is not defined

因為我不太會解釋,所以我將附加我的代碼。 很抱歉這么亂

import math
import numpy as np
import matplotlib.pyplot as plt

def circle(N,dt,x0,y0,phi0,r,T,nu,v,Omega,R):


kB = 1.38*10**-23 # Boltzmann constant [J/K]plt #math constants
DT = kB*T/(6*math.pi*nu*r)  # Translational diffusion coefficent [m^2/s]
DR = kB*T/(8*math.pi*nu*r**3) # Rotational diffusion coefficent [rad^2/s]
n = 0 #iteration constant


x=x0 #vectors and initial values
y=y0
phi=phi0
phiv= np.array(phi) #vector containing phi-values
xv= np.array(x) #vector containing x-values
yv= np.array(y) #vector containing y-values
rv=np.array(np.sqrt(x**2+y**2))
xss=np.array(x) #vector containing start and (soon) end value
yss=np.array(y) # same as above

phiK=math.sqrt(2*DR*dt) #constants used in the iteration
xyK=math.sqrt(2*DT*dt)
Odt=Omega*dt

while n < N: #from 0 -> N-1
    phi = phi + Odt + phiK*np.random.normal(0,1) #eq (9)
    x = x + v*math.cos(phi)*dt + xyK*np.random.normal(0,1) #eq (10)
    y = y + v*math.sin(phi)*dt + xyK*np.random.normal(0,1) #eq (11)
    if (x**2+y**2) > R**2: #if the particle is outside the boundary
        if abs(x) > R: #need to make sure arccos(x/R) is meaningful
            xn = np.sign(x)*R
            theta = np.sign(y)*np.arccos((xn/R))#angle to particle
        else:
            theta = np.sign(y)*np.arccos((x/R))#angle to particle  
        rp = np.array([x,y]) #r for the particle
        rr = np.array([np.cos(theta)*R,np.sin(theta)*R]) #r for the boundary closest to the particle
        #d = rp - rr #smallest distance from particle to boundary
        q = (2*np.linalg.norm(rr)/np.linalg.norm(rp)) - 1
        x = q*np.array(rp[0]) #x- and y-value forced inside the boundary
        y = q*np.array(rp[1])

    phiv = np.append(phiv, phi) #adding all phi-values to a vector
    xv = np.append(xv, x) # adding all x-values to a vector
    yv = np.append(yv, y) # adding all y-values to a vector'
    rv = np.append(rv,(np.sqrt(x**2+y**2)))
    n=n+1 #iteration 


return(rv)
#print(rv)

for i in range(2): #run the program a number of times
    datavector = np.array(0)
    circle(1E5,1E-3,0*np.random.uniform(-1E-5,1E-5),0*np.random.uniform(-1E-5,1E-5),np.random.uniform(-2*np.pi,2*np.pi),1E-6,300,1E-3,5*1E-6,0,2E-5)
    datavector = np.append(datavector, rv)
np.savetxt('testet.txt', datavector)

name 'rv' is not defined表示在對其執行操作之前尚未定義變量。

根據您提供的信息,永遠不會定義rv 最重要的是,您將在每次迭代時重新初始化結果向量datavector ,而僅應在主循環之前對其進行初始化。

我假設rvcircle的返回值。

因此,您的代碼更正應如下所示:

datavector = []
for i in range(1000):
    rv = circle(N,dt,x0,y0,phi0,r,T,nu,v,Omega,R)
    datavector.append(rv)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM