使用 Google 試算表及 VPython 計算砲彈軌跡
作者:王一哲
日期:2022/3/2
前言
前幾天在 YouTube 上看到李永樂老師的影片「如何精準命中目標?戰爭到底帶給我們什麽?」,影片中提到理想狀態與實際情境下的斜向拋射運動差異,最後有提到計算道彈的數值方法。影片中李永樂老師是用 Excel 計算的,而我在高三多元選修中則是教學生用 VPython 計算,雖然工具不同,但是原理相同。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
理想狀態
若砲彈的初速度量值為 、仰角為 ,重力加速度為 ,只考慮重力的作用,則砲彈的水平位移 與時間 的關係為
鉛直位移 與時間 的關係為
將 代入 可得軌跡方程式
由於
可以將軌跡方程式改寫成
若已知目標物所在的位置 ,則初速度仰角 可以由 為變數的一元二次方程式解出。
用影片中給定的條件 、、、 代入上式可得
實際情境
若砲彈速度 往右上方,速度與水平方向夾角為 ,則水平分量 ,鉛直分量 。砲彈受到空氣阻力 方向與速度相反,其量值為
其中 為無因次的阻力係數, 為空氣密度、 為物體截面積。因此砲彈受到向左下方的力量 ,其水平方向分量
鉛直方向分量
由牛頓第二運動定律可以計算砲彈的加速度
由於力量、加速度、速度會互相影響,很難將軌跡方程式寫出來,但是我們可以用數值方法計算軌跡。最簡單的作法是用時刻 的速度計算力量,代入一小段時間 ,計算砲彈在 內的速度變化以及時刻 時的速度;再用更新後的速度乘以 ,計算砲彈在 內的位移以及時刻 時的位置;接著用更新後的速度計算時刻 時的力量;不斷重複以上的過程就可以算出軌跡。
使用數值方法計算軌跡的流程圖
我仿照影片中的作法用 Google 試算表做了以下的檔案,連結在此,有興趣的同學可以將檔案另存複本拿回去修改看看。如果採用影片中的數值,砲彈初速度 、質量 ,目標物水平距離 、高度 ,無因次的空氣阻力係數 、空氣密度 、砲彈截面積 ,重力加速度 ,時間間隔 ,計算結果 ,與理論計算的結果 差距相當大。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
使用 Google 試算表及數值方法計算砲彈軌跡
VPython
接下來改用 VPython 計算砲彈,將初速度仰角 由 開始代入程式中計算砲彈與目標物最接近的距離,每次增加 ,直到 為止。計算的結果為,當 時,砲彈與目標物的距離最近,量值約為 3.37 m。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
模擬程式畫面截圖,為了使畫面較為清楚,每隔 2° 畫一條軌跡。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
砲彈與目標物最接近距離與初速度仰角關係圖
"""
VPython教學: 計算砲彈軌跡及砲彈與目標物最近的距離
日期: 2022/3/2
作者: 王一哲
原始資料: https://youtu.be/nalzSo4f0iQ
"""
from vpython import *
import matplotlib.pyplot as plt
"""
1. 參數設定, 設定變數及初始值
"""
v0 = 828
m = 43.5
C = 0.2
rho = 1.3
s = 0.01815
cof = 0.5*C*rho*s
g = 9.8
L = 21000
t = 0
dt = 0.01
target = vec(20000, 500, 0)
ratio = 20
deg_list, t_list, dis_list = [], [], []
"""
2. 畫面設定
"""
scene = canvas(title="Cannon Trajectory", width=800, height=400, x=0, y=0,
center=vec(0.5*L, 0.1*L, 0), background=color.black, range=0.3*L)
floor = box(pos=vec(0.5*L, -0.5, 0), size=vec(L, 1, 0.1*L), color=color.blue)
ball = sphere(pos=vec(0, 0, 0), radius=100, color=color.red)
with open("CannonTrajectoryData.csv", "w", encoding="UTF-8") as file:
file.write("theta(degree), t(s), distance(m)\n")
"""
3. 物體運動部分
"""
def motion(degree):
t = 0
theta = radians(degree)
ball = sphere(pos=vec(0, 0, 0), radius=1, color=color.red,
make_trail=True, v=vec(cos(theta), sin(theta), 0)*v0)
arrow_mg = arrow(pos=ball.pos, shaftwidth=20, axis=vec(0, 0, 0), color=color.green)
arrow_f = arrow(pos=ball.pos, shaftwidth=20, axis=vec(0, 0, 0), color=color.cyan)
dis1 = mag(target - ball.pos)
while True:
rate(1000)
f = -cof*ball.v.mag2*ball.v.norm()
ball.a = f/m + vec(0, -g, 0)
ball.v += ball.a*dt
ball.pos += ball.v*dt
dis2 = mag(target - ball.pos)
arrow_mg.pos = ball.pos
arrow_mg.axis = vec(0, -m*g, 0) * ratio
arrow_f.pos = ball.pos
arrow_f.axis = f*ratio
t += dt
if dis2 < dis1: dis1 = dis2
else:
arrow_mg.visible = False
arrow_f.visible = False
return t - dt, dis1
for degree in arange(1, 30, 0.1):
t, distance = motion(degree)
deg_list.append(degree)
t_list.append(t)
dis_list.append(distance)
print(degree, t, distance)
with open("CannonTrajectoryData.csv", "a", encoding="UTF-8") as file:
file.write(str(degree) + "," + str(t) + "," + str(distance) + "\n")
print(deg_list[dis_list.index(min(dis_list))], min(dis_list))
plt.figure(figsize=(6, 4.5), dpi=100)
plt.xlabel(r'$\theta ~\mathrm{({}^{\circ})}$', fontsize=14)
plt.ylabel(r'$distance ~\mathrm{(m)}$', fontsize=14)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.grid(color='grey', linestyle='--', linewidth=1)
plt.plot(deg_list, dis_list, linestyle='-', linewidth=4, color='blue')
plt.savefig('CanonTrajectoryPlot.svg')
plt.savefig('CanonTrajectoryPlot.png')
plt.show()
程式碼運作的流程大致上與 Google 試算表的計算流程相同。為了節省程式運作所需時間,我在自訂函式 motion 裡將前一個時刻 t 的砲彈與目標物距離儲存在變數 dis1,將現在的砲彈與目標物距離儲存在變數 dis2,若 dis2 < dis1 代表砲彈正在接近目標物,繼續運作 while 迴圈;若 條件不成立代表砲彈已經遠離目標物,執行 else 當中的程式碼,執行到第66行的 return 時回傳 前一個時刻 t - dt 及最接近的距離 dis1 並且結束 while 迴圈。如果只想要得到初速度仰角與最接近的距離,可以刪除第51行的 rate(1000),但是模擬程式的畫面會變得很亂。
結語
我在之前的 VPython 講義中有寫過類似的程式:程式 6-3.斜向抛射, 使用for 迴圈改變仰角 theta, 空氣阻力係數 b,這篇文章裡的程式就是由它改寫而成的。以後在講到斜向拋射時,可以拿這篇文章的主題作為例子,說明理想狀態與實際情境有多大的差異。