【发布时间】:2015-05-26 02:32:06
【问题描述】:
我正在使用 scipy ode 解决钟摆问题。
from scipy import *
import matplotlib.pyplot as plt
from scipy.integrate import ode
def pendulumdot(t, y, gamma, omega0, Fd):
theta, v = y[0], y[1]
return array([v, -2*gamma*v-omega0**2*sin(theta)+Fd*cos(2*pi*t)])
def pendulum_sample(theta0, thetadot0, gamma, omega0, Fd, t):
Theta0 = array([theta0, thetadot0])
r = ode(pendulumdot)
r.set_integrator('dopri5')
r.set_initial_value(Theta0)
r.set_f_params( gamma, omega0, Fd)
#r.set_jac_params()
theta = zeros(len(t))
thetadot = zeros(len(t))
theta[0] = theta0
thetadot[0] = thetadot0
for n in range(1, len(t)):
r.integrate(t[n])
assert r.successful()
theta[n] = (r.y)[0]
thetadot[n] = (r.y)[1]
return theta, thetadot
def pendulum_demo():
gamma = 0.1
omega0 = 10.0
theta0 = 0.0
thetadot0 = 0.0
Fd = 50.0
t1 = linspace(0, 200, 10000)
theta1, thetadot1 = pendulum_sample(theta0, thetadot0, gamma, omega0, Fd, t1)
plt.plot(t1, theta1)
t2 = linspace(0, 150, 10000)
theta2, thetadot2 = pendulum_sample(theta0, thetadot0, gamma, omega0, Fd, t2)
plt.plot(t2, theta2)
plt.show()
pendulum_demo()
我在不同的时间范围内绘制了两个 theta 与时间的关系图,一个是 (0, 150),一个是 (0, 200)。我期望的是这两个数字在时间范围(0、150)内应该是相同的,但是,这不是我观察到的。我的脚本有什么问题吗?谢谢
【问题讨论】: