斜方投射のシミュレーション
やること
- 初速度、仰角を入力
- 位置xと高さyを計算
- x,yを出力する
- x,yをプロットする
使った関数
- math.tan : タンジェントの計算
- math.radians : 角度(°)を角度(rad)に変換
- math.cos : コサインの計算
- pow : べき乗の計算
- matplotlib.pyplot.scatter : 散布図のプロット
- matplotlib.pyplot.show : 散布図の表示
環境
準備
斜方投射のx座標とy座標の計算は次の式に従います。
コード
import math #mathのインポート x=0 #x座標の初期値 y=0 #y座標の初期値 theta_initial=float(input('出射角度(°)を入力')) #出射角度(Θ0)の入力 v_initial=float(input('初速度(m/s)を入力')) #初速度(v0)の入力 tan_theta_initial=math.tan(math.radians(theta_initial)) #tan(Θ0)の計算 cos_theta_initial_squared=pow(math.cos(math.radians(theta_initial)),2) #cos^2(Θ0)の計算 while y >= 0: #yが0以上の間以下の処理を繰り返す y=tan_theta_initial*x-9.8/(2*v_initial**2*cos_theta_initial_squared)*x**2 #y座標の計算 print(x,y) x=x+1 #xに1を加える
実行結果
出射角度45°、初速度10m/sを設定した場合
出射角度(°)を入力45
初速度(m/s)を入力10
0 0.0
1 0.9019999999999999
2 1.6079999999999999
3 2.118
4 2.432
5 2.55
6 2.472
7 2.1980000000000004
8 1.7280000000000006
9 1.0620000000000003
10 0.20000000000000107
11 -0.8579999999999988
x,yが出力されます。
データをプロットする場合です。
コード
import math #mathのインポート import matplotlib.pyplot as plt #matplotlib.pyplotのインポート x=0 #x座標の初期値 y=0 #y座標の初期値 x_plot=[] #x座標を保存するリストを作成、初期化 y_plot=[] #y座標を保存するリストを作成、初期化 theta_initial=float(input('出射角度(°)を入力')) #出射角度(Θ0)の入力 v_initial=float(input('初速度(m/s)を入力')) #初速度(v0)の入力 tan_theta_initial=math.tan(math.radians(theta_initial)) #tan(Θ0)の計算 cos_theta_initial_squared=pow(math.cos(math.radians(theta_initial)),2) #cos^2(Θ0)の計算 while y >= 0: #yが0以上の間以下の処理を繰り返す y=tan_theta_initial*x-9.8/(2*v_initial**2*cos_theta_initial_squared)*x**2 #y座標の計算 #print(x,y) x_plot.append(x) y_plot.append(y) x=x+1 #xに1を加える plt.scatter(x_plot, y_plot) #x_plot vs y_plotで散布図を描画 plt.show()
実行結果
散布図がプロットされます。
出射角度(°)を入力45
初速度(m/s)を入力10