我想要一个自由落体的物体来“反弹”。在一个简单的例子中,一个物体从初始高度下降。当它击中“地面”时,我希望它“反弹”。
在该示例中,对象以初始速度为零下降并以1 g加速。有没有办法强制odeint从'x'位置而不是经过一段时间后终止其“整合”?
或者scipy是否提供了解决问题的更好方法?
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
def gravity (x, t, params):
# x = (x, x')
# Ordinary differential equations for displacement and velocity
# of object in frictionless free-fall.
g = params
return [g*t, g]
# First 2 seconds of motion
t = np.arange (0, 2.1, 0.1)
g = -9.8 # m/s^2
bc = np.array ([9.8, 0])
soln = odeint (gravity, bc, t, args = (g,))
# How do we limit odeint to free fall until contact with the ground at x = 0?
fig = plt.figure (1, figsize = (8,8))
ax1 = fig.add_subplot(211)
ax1.plot(t, soln [:,0])
ax1.set_xlabel ('time')
ax1.set_ylabel ('x')
ax1 = fig.add_subplot(212)
ax1.plot(t, soln [:,1])
ax1.set_xlabel ('time')
ax1.set_ylabel ('Vx')
plt.show ()