模拟重力作用下静止粒子的轨迹

问题描述

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

  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

这是我应该达到的目标:

enter image description here

这是我到目前为止所做的:

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 向量集成到循环中,但我不知道如何。

谢谢。

解决方法

这是您的代码的修改版本,我相信它可以为您提供所需的结果(您可能需要选择不同的初始速度值):

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

  • 查看评论了解更多信息

结果:resulting plot

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...