【问题标题】:How to plot the motion of a projectile under the effect of gravity, buoyancy and air resistance?如何绘制弹丸在重力、浮力和空气阻力作用下的运动?
【发布时间】:2019-01-02 02:06:15
【问题描述】:

我正在尝试绘制在重力、浮力和阻力作用下的质量的抛射运动图。基本上,我想在绘图上显示浮力和阻力对飞行距离、飞行时间和速度变化的影响。

import matplotlib.pyplot as plt
import numpy as np

V_initial = 30 # m/s
theta = np.pi/6 # 30
g = 3.711
m =1
C = 0.47
r = 0.5
S = np.pi*pow(r, 2)
ro_mars = 0.0175
t_flight = 2*(V_initial*np.sin(theta)/g)
t = np.linspace(0, t_flight, 200)

# Drag force
Ft = 0.5*C*S*ro_mars*pow(V_initial, 2)

# Buoyant Force
Fb = ro_mars*g*(4/3*np.pi*pow(r, 3))

x_loc = []
y_loc = []

for time in t:
    x = V_initial*time*np.cos(theta)
        y = V_initial*time*np.sin(theta) - (1/2)*g*pow(time, 2)
    x_loc.append(x)
    y_loc.append(y)

x_vel = []
y_vel = []
for time in t:
    vx = V_initial*np.cos(theta)
    vy = V_initial*np.sin(theta) - g*time
    x_vel.append(vx)
    y_vel.append(vy)


v_ch = [pow(i**2+ii**2, 0.5) for i in x_vel for ii in y_vel]

tau = []
for velocity in v_ch:
    Ft = 0.5*C*S*ro_mars*pow(velocity, 2)
        tau.append(Ft)

buoy = []
for velocity in v_ch:
    Fb = ro_mars*g*(4/3*np.pi*pow(r, 3))
    buoy.append(Fb)

在这一点之后,我无法弄清楚如何在这种力下绘制弹丸运动。换句话说,我试图比较质量在三种情况下的抛射运动

  1. 仅受重力影响的质量
  2. 重力和空气阻力作用下的质量
  3. 重力、空气阻力和浮力作用下的质量

【问题讨论】:

  • 你提供了很多无证代码。您没有在问题中指定要针对哪个变量绘制哪个变量。您没有告诉我们所需的情节如何(1d,2d,3d)。您没有告诉我们您面临的具体问题是什么。您希望读者如何帮助您?
  • 据我了解,您的问题是双重的:求解(数值)一个微分方程,然后绘制它。请详细说明您要在两端完成的工作以及您尝试过的工作。您可能还想查看scipy.integrate.odeint 示例(如果您更好地指定问题,我们可以帮助您)。
  • 我编辑了我的问题并试图更清楚地解释。

标签: python numpy matplotlib physics


【解决方案1】:

您必须根据给定时间的力总和来计算每个位置。为此,最好从随时计算净力开始,然后用它来计算加速度、速度和位置。对于下面的计算,假设浮力和重力是恒定的(实际上这不是真的,但在这种情况下它们的变化性的影响可以忽略不计),还假设初始位置是(0,0),尽管这可以是简单地更改为任何初始位置。

F_x = tau_x 
F_y = tau_y + bouyancy + gravity

其中tau_xtau_y 分别是xy 方向上的阻力。速度,v_xv_y,然后由

给出
v_x = v_x + (F_x / (2 * m)) * dt
v_y = v_y + (F_y / (2 * m)) * dt

所以xy位置,r_xr_y,在任何时候t都是由

r_x = r_x + v_x * dt
r_y = r_y + v_y * dt

在这两种情况下,对于某些dt,必须从0t 进行评估,其中dt * n = t 如果n 是求和的步数。

r_x = r_x + V_i * np.cos(theta) * dt + (F_x / (2 * m)) * dt**2
r_y = r_y + V_i * np.sin(theta) * dt + (F_y / (2 * m)) * dt**2

整个计算实际上可以分两行完成,

r_x = r_x + V_i * np.cos(theta) * dt + (tau_x / (2 * m)) * dt**2
r_y = r_y + V_i * np.sin(theta) * dt + ((tau_y + bouyancy + gravity) / (2 * m)) * dt**2

除了v_xv_y 需要在每个时间步更新。要遍历此并计算一段时间内的xy 位置,您可以简单地按照下面的(编辑的)示例进行操作。

以下代码包含用于防止负 y 位置的更正,因为g 的给定值适用于表面或火星,我认为这是合适的 - 当您达到零 y 并尝试继续前进时,您可能会结束我们物理学家称之为快速的计划外拆卸。

编辑

针对已编辑的问题,对以下示例进行了修改,以绘制所请求的所有三种情况 - 重力、重力加阻力以及重力加阻力和浮力。绘图设置代码也已添加

完整示例(已编辑)

import numpy as np
import matplotlib.pyplot as plt

def projectile(V_initial, theta, bouyancy=True, drag=True):
    g = 9.81
    m = 1
    C = 0.47
    r = 0.5
    S = np.pi*pow(r, 2)
    ro_mars = 0.0175

    time = np.linspace(0, 100, 10000)
    tof = 0.0
    dt = time[1] - time[0]
    bouy = ro_mars*g*(4/3*np.pi*pow(r, 3))
    gravity = -g * m
    V_ix = V_initial * np.cos(theta)
    V_iy = V_initial * np.sin(theta)
    v_x = V_ix
    v_y = V_iy
    r_x = 0.0
    r_y = 0.0
    r_xs = list()
    r_ys = list()
    r_xs.append(r_x)
    r_ys.append(r_y)
    # This gets a bit 'hand-wavy' but as dt -> 0 it approaches the analytical solution.
    # Just make sure you use sufficiently small dt (dt is change in time between steps)
    for t in time:
        F_x = 0.0
        F_y = 0.0
        if (bouyancy == True):
            F_y = F_y + bouy
        if (drag == True):
            F_y = F_y - 0.5*C*S*ro_mars*pow(v_y, 2)
            F_x = F_x - 0.5*C*S*ro_mars*pow(v_x, 2) * np.sign(v_y)
        F_y = F_y + gravity

        r_x = r_x + v_x * dt + (F_x / (2 * m)) * dt**2
        r_y = r_y + v_y * dt + (F_y / (2 * m)) * dt**2
        v_x = v_x + (F_x / m) * dt
        v_y = v_y + (F_y / m) * dt
        if (r_y >= 0.0):
            r_xs.append(r_x)
            r_ys.append(r_y)
        else:
            tof = t
            r_xs.append(r_x)
            r_ys.append(r_y)
            break

    return r_xs, r_ys, tof

v = 30
theta = np.pi/4

fig = plt.figure(figsize=(8,4), dpi=300)
r_xs, r_ys, tof = projectile(v, theta, True, True)
plt.plot(r_xs, r_ys, 'g:', label="Gravity, Buoyancy, and Drag")
r_xs, r_ys, tof = projectile(v, theta, False, True)
plt.plot(r_xs, r_ys, 'b:', label="Gravity and Drag")
r_xs, r_ys, tof = projectile(v, theta, False, False)
plt.plot(r_xs, r_ys, 'k:', label="Gravity")
plt.title("Trajectory", fontsize=14)
plt.xlabel("Displacement in x-direction (m)")
plt.ylabel("Displacement in y-direction (m)")
plt.ylim(bottom=0.0)
plt.legend()
plt.show()

请注意,这会保留并返回变量 tof 中的飞行时间。

【讨论】:

  • 嗨,William,函数中的重力参数的值是多少,它似乎没有定义,因此它没有给出您找到的图表。
【解决方案2】:

使用矢量符号和odeint

import numpy as np
from scipy.integrate import odeint
import scipy.constants as SPC
import matplotlib.pyplot as plt

V_initial = 30 # m/s
theta = np.pi/6 # 30
g = 3.711
m = 1 # I assume this is your mass
C = 0.47
r = 0.5
ro_mars = 0.0175

t_flight = 2*(V_initial*np.sin(theta)/g)
t = np.linspace(0, t_flight, 200)

pos0 = [0, 0]
v0 = [np.cos(theta) * V_initial, np.sin(theta)  * V_initial]

def f(vector, t, C, r, ro_mars, apply_bouyancy=True, apply_resistance=True):
    x, y, x_prime, y_prime = vector

    # volume and surface
    V = np.pi * 4/3 * r**3
    S = np.pi*pow(r, 2)

    # net weight bouyancy
    if apply_bouyancy:
        Fb = (ro_mars * V - m) * g *np.array([0,1])
    else:
        Fb = -m  * g * np.array([0,1])

    # velocity vector
    v = np.array([x_prime, y_prime])

    # drag force - corrected to be updated based on current velocity
#    Ft = -0.5*C*S*ro_mars*pow(V_initial, 2)
    if apply_resistance:
        Ft = -0.5*C*S*ro_mars* v *np.linalg.norm(v)
    else:
        Ft = np.array([0, 0])

    # resulting acceleration
    x_prime2, y_prime2 = (Fb + Ft) / m

    return x_prime, y_prime, x_prime2, y_prime2

sol = odeint(f, pos0 + v0 , t, args=(C, r, ro_mars))
plt.plot(sol[:,0], sol[:, 1], 'g', label='tray')
plt.legend(loc='best')
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()

请注意,我将您的阻力修正为使用实际(非初始)速度,我不知道这是您的错误还是故意的。

另外请查看odeint 的文档,以更好地了解如何将二阶 ODE(如您的问题中的 ODE)转换为一阶向量 ODE。

要消除空气阻力或浮力,请将apply_bouyancyapply_resistance 设置为TrueFalse,方法是将它们添加到args=(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多