-1

为什么同一问题的以下两个版本会给出不同的结果 y_phi,而所有参数都相同,并且随机值在两个版本中都以相同的值作为种子?我知道 Version_1 给出了正确的结果,而 Version_2 给出了错误的结果,为什么会这样?我在哪里犯了 Version_2 错误?

版本_1:

from numpy import *
import random
from scipy.integrate import odeint

def f(phi, t, alpha):
    v_phi = zeros(x*y, float)
    for n in range(x*y):
        cmean = cos(phi[n])
        smean = sin(phi[n])
        v_phi[n] = smean*cos(phi[n]+alpha)-cmean*sin(phi[n]+alpha)
    return v_phi

x = 5
y = 5
N = x*y
PI = pi
alpha = 0.2*PI
random.seed(1010)
phi = zeros(x*y, float)
for i in range(x*y):
    phi[i] = random.uniform(0.0, 2*PI)
t = arange(0.0, 100.0, 0.1)

y = odeint(f, phi, t, args=(alpha,))
y_phi = [y[len(t)-1, i] for i in range(N)]
print y_phi

版本_2:

def f(phi, t, alpha, cmean, smean):
    v_phi = zeros(x*y, float)
    for n in range(x*y):
        v_phi[n] = smean[n]*cos(phi[n]+alpha)-cmean[n]*sin(phi[n]+alpha)
    return v_phi

x = 5
y = 5
N = x*y
PI = pi
alpha = 0.2*PI
random.seed(1010)
phi = zeros(x*y, float)
for i in range(x*y):
    phi[i] = random.uniform(0.0, 2*PI)
t = arange(0.0, 100.0, 0.1)

cmean = []
smean = []
for l in range(x*y):
    cmean.append(cos(phi[l]))
    smean.append(sin(phi[l]))

y = odeint(f, phi, t, args=(alpha, cmean, smean,))
y_phi = [y[len(t)-1, i] for i in range(N)]
print y_phi
4

1 回答 1

0

在第一种情况下smean = cos(phi(t)[n]),它取决于 phi 的当前值,但在第二种情况下,它smean = cos(phi(0)[n])仅取决于初始值。(同样对于cmean)。

顺便说一句,您也可以使用http://en.wikipedia.org/wiki/Rubber_duck_debugging找到这个问题

于 2013-04-04T15:47:49.843 回答