【问题标题】:Simulating the trajectory of a particle from rest under gravity模拟粒子在重力下静止的轨迹
【发布时间】:2021-01-03 13:57:29
【问题描述】:

希望你能帮上忙!我需要显示在g = -9.81 m/s2 的重力和dt = 0.05 sec 的时间步长下的粒子的轨迹,其中粒子的位置和速度为:

  1. x_1 = x_0 + v_x1 * dt
  2. y_1 = y_0 + v_y1 * dt
  3. v_x1 = v_x0
  4. v_y1 = v_y0 + g * dt

这是我应该实现的: 这是我到目前为止所做的:

import numpy as np
import matplotlib.pyplot as plt

plt.figure(1, figsize=(12,12))
ax = plt.subplot(111, aspect='equal')
ax.set_ylim(0,50)
ax.set_title('Boom --- Weeee! --- Ooof')

r = np.array([0.,0.,15.,30.])
g = -9.81
dt = 0.05
y = 0
x = 0
while y > 0:
    plt.plot(x_1,y_2,':', ms=2)
    x_1 = v_x1 * dt
    y_1 = v_y1 * dt
    v_x1 = v_x0
    v_y1 = v_y0 + g * dt

这不会只生成开头提到的plt.figure 的图像,我尝试将r 向量集成到循环中,但我不知道如何。

谢谢。

【问题讨论】:

    标签: python simulation physics particles


    【解决方案1】:

    这是你的代码的修改版本,我相信它会给你你想要的结果(你可能想要选择不同的初始速度值):

    import matplotlib.pyplot as plt
    
    # Set up our plot surface
    plt.figure(1, figsize=(12,12))
    ax = plt.subplot()
    # ax = plt.subplot(111, aspect='equal')
    # ax.set_ylim(0,50)
    ax.set_title('Boom --- Weeee! --- Ooof')
    
    # Initial conditions
    g = -9.81
    dt = 0.05
    y = 0
    x = 0
    v_x = 5
    v_y = 5
    
    # Create our lists that will store our data points
    points_x = []
    points_y = []
    
    while True:
    
        # Add the current position of our projectile to our data
        points_x.append(x)
        points_y.append(y)
    
        # Move our projectile along
        x += v_x * dt
        y += v_y * dt
    
        # If our projectile falls below the X axis (y < 0), end the simulation
        if y < 0:
            break
    
        # Update our y velocity per gravity
        v_y = v_y + g * dt
    
    # Plot our data
    ax.plot(points_x, points_y)
    
    # Show the plot on the screen
    plt.show()
    

    如果我可以做更少的更改,我很抱歉。以下是我能想到的实质性内容:

    • 您没有使用您计算的 r 值,因此将其删除,同时导入不再需要的 numpy。

    • 我接听了您明确确定您的情节大小的电话。你最好让情节库为你决定情节的界限

    • 我不知道是否有其他方法可以做到这一点,但我总是将数据作为点数组提供给绘图库,而不是一次提供一个点。所以在这里,我在运行模拟时将所有 x 和 y 坐标收集到两个列表中,然后将这些数组添加到最后的绘图中以绘制数据。

    • x_0x_1 等等,让我感到困惑。我看不出有任何理由跟踪多个位置和速度值,所以我将代码缩减为只使用一组位置和速度,xyv_xv_y

    • 查看 cmets 了解更多信息

    结果:

    【讨论】:

    • 哦,我现在明白你在说什么了!很好的答案。 x_0 和 x_1 也不是真正的位置,它只是物理学家的符号,所以我猜他将符号改编为代码
    • @Callister - 如果你想要点而不是线,我会把它作为练习留给你去弄清楚。坦率地说,我不知道为什么我们在这里得到一条线而不是不同的点。我没有对绘图库做很多事情。无论我第一次尝试得到什么,通常对我来说都足够了,因为我只是想大致了解我的数据是什么样的。
    • ax.plot(points_x, points_y, 'o') 获取点而不是线。 Line 是默认的线型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多