【发布时间】:2018-06-04 18:08:33
【问题描述】:
所以我试图通过使用两个步长来计算错误,一个是另一个的两倍。我设置了两个温度阵列,它们使用两种不同的步长进行计算。当我注意到在我调用它以在时间 = 5 分钟时打印温度值的循环中,我为我的错误得到了疯狂的值,我写错了。如果我用它代替任何数字,它会打印相同的值。 np.where(t=5) 在这种情况下被错误地使用了吗?对于每个不同的步长,都会产生一条曲线,该曲线具有与每个时间对应的温度值。我希望能够打印时间步长的值及其相应的错误。
for j in dt_values:
t = np.arange(0,100,j) #time
te = np.zeros(len(t)) #temperature array
te[0] = te_init
te2 = np.zeros(len(t)) #second temp array
te2[0] = te_init
dt = j #timestep
dt2 = 2*dt
def f(t,te):
y = -r*(te - te_surr) # y is the derivative
return y
for i in range(1,len(t)): #eulers method for computing temperature
p1=f(t[i-1], te[i-1])
p2 =f(t[i], te[i-1]+dt*p1)
te[i] = te[i-1] + (p1 + p2)*(dt/2)
r1=f(t[i-1], te[i-1])
r2 =f(t[i], te[i-1]+dt2*p1)
te2[i] = te2[i-1] + (r1 + r2)*(dt2/2)
if np.where(t == 5):
print j
print te[i] - te2[i]
【问题讨论】:
-
确实不正确。只需使用
if (t == 5).any()
标签: python arrays python-2.7 numpy