1

我想要一个自由落体的物体“反弹”。在一个简单的例子中,一个物体从一个初始高度自由落体。当它撞到“地面”时,我希望它“弹跳”。

在这个例子中,一个物体以 0 的初始速度下落并以 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 ()
4

0 回答 0