python - 基本的Python弹跳球问题(可能还有嵌套循环?)

标签 python for-loop while-loop nested-loops

我在这里想要实现的是创建一个球下落和反弹的轨迹,该运动的方程是 y,每次球反弹时,v_0 变成 e*v_0,或 3/4之前的。我首先计算没有弹跳的 y(t),让 y 为负数,然后轨迹的负数部分将被以弹跳开始的新轨迹替换,其 v_0 将是之前的 3/4,然后重复直到没有剩余 y 负值。 (我尝试了嵌套循环,但我还没有真正弄清楚如何,我们不必使用嵌套循环,但我真的没有看到其他方法)。最终,我想使用函数下方的 t_test 生成一个包含球的所有 y 的数组。


def traj_y_bounce(t, v_0, y_0=0, e=0.75):    
    y = y_0 + v_0 * t - (1/2)*g*(t**2)
    return y
#    for i in range(len(y)):
#        while y[i] < 0:
#            y[i] == 
#    return y



#t_test = np.linspace(0,5,10)
#print(traj_y_bounce(t_test, 4))

最佳答案

这是一个简单的模拟例子,球从高度 h 落下

def traj_y_bounce(tmax, v0, y0, e0=0.75):
    g = 9.9 # meters/s/s
    y = [y0] # heights over time

    dt = 0.01 # integrate in hundredths of a second

    v = v0
    t = 0
    while t <= tmax:
        if y[-1] <= 0 and v < 0:
            v = -e0*v   # reflect off ground
        v = v - g*dt    # integrate velocity
        y.append(y[-1] + v*dt)  # integrate position

        t = t + dt  # integrate time
    return y

y = traj_y_bounce(5, 0, 10)

%matplotlib notebook
import matplotlib.pyplot as plt
plt.plot(y, linewidth=4, label='height')
plot.show()

情节 Simulation

关于python - 基本的Python弹跳球问题(可能还有嵌套循环?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58280477/

相关文章:

python - while循环的大O

Python哨兵控制循环

Python:列表迭代中途列表索引超出范围

python - 检查 YAML 文件中的数据在 python 中是否按字母顺序排列

python - 在嵌套列表理解中只使用一个列表中的项目一次

Java for循环迭代并列出颜色

C 代码陷入无限的 do-while 循环

python - 将 Python 包安装到 docker 镜像的不同方法

python - 如何删除python中的符号链接(symbolic link)?

无法打印整个 int 数组