我想打印出方程组每t+dt的解的执行时间,其中dt是.01秒
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import time
from datetime import timedelta
# function that returns dz/dt
def model(z,t):
start_time = time.clock()
dxdt = z[1]
dydt = -1.2*(z[0]+z[1]) + 0.2314*z[0] + 0.6918*z[1] - 0.6245*abs(z[0])*z[1] + 0.0095*abs(z[1])*z[1] + 0.0214*z[0]*z[0]*z[0]
dzdt = [dxdt,dydt]
#Print
elapsed_time_secs = time.clock()-start_time
msg = "Execution took: %s secs (Wall clock time)" %
timedelta(seconds=round(elapsed_time_secs,5))
print(msg)
return dzdt
# initial condition
z0 = [0,0]
# time points
t = np.linspace(0,10,num=1000)
# solve ODE
z = odeint(model,z0,t)
由于我的时间间隔是.01(10/1000),从技术上讲,我应该有1000行输出语句,但由于某些原因,打印的行要少得多。这取决于设置的初始条件。对于[0,0],大约打印10行。对于[1,2],大约打印了400行。我不明白为什么会这样。你知道吗
你在做一些奇怪的事情,你在测量ODE函数的执行时间。由于您的一些算术运算需要大约100个时钟周期,最多1000个时钟周期,并且您的处理器以每秒10^9个时钟周期工作,因此您的调用将需要大约1e-6秒,这在给定的输出格式中是看不到的。有时您会看到一些解释器开销和JIT编译时间。你知道吗
odeint
与内部自适应步长一起工作,请求的输出从内部步长内插。内部步长可以小于输入时间数组的时间步长,或者对于常量解等无聊解,内部步长可以非常大。ODE函数将只在内部步骤调用,而不是在所有请求的输出时间调用。如果将t
和z
添加到打印的值中,您将看到。你知道吗相关问题 更多 >
编程相关推荐