はじめに
matplotlibのFuncAnimationで自由落下と反発係数を考慮したバウンドのアニメーションを表示する。
コード
解説
モジュールのインポートなど
バージョン
時間データの作成
np.arangeで15sまで0.05刻みで時間データを作成する。配列のサイズは300となる。
重力加速度は9.8 m/sとした。
自由落下時の位置変化
初期位置を100とした場合、地面に到着する時間はt_posi0となる。
地面に到着する時間のときのtにおけるインデックスを求める。
np.argmin(np.abs(t-t_posi0))で求められる。
自由落下の位置変化を表示すると以下のようになる。

バウンド時の位置変化
反発係数を0.8として、衝突前のスピードと衝突後のスピードを求める。
位置変化は以下のようになる。

np.where(b1_z>0)[0][-1]で位置が正の値の位置の最後のインデックスを求めて、位置データをスライスして表示すると下記のようになる。

2回目のバウンド

3回目のバウンド

4回目のバウンド

5回目のバウンド

6回目のバウンド

位置データの連結
np.concatenate()で全ての位置データを連結する。図示すると以下のようになる。

データの大きさは627となる。
アニメーションで表示
ax.hlines(0,-.25,.25)でy=0の位置に平行線を表示する。
ax.spines[‘right’].set_visible(False)などで左以外の軸を消す。
アニメーション関数の設定
set_ydataでplot(ball1)のydataをセットする。
アニメーションの表示
FuncAnimationでアニメーションを表示する。frame数を627、intervalを10msとし、6秒少々のアニメーションとする。 HTML(ani.to_html5_video())
により、jupyter notebook またはjupyter lab上にアニメーションを表示できる。
ani.save(‘ファイル名’, writer=”ffmpeg”,dpi=100)でアニメーションをMP4形式で保存することができる。
反発係数を0.5とした場合
反発係数を0.95とした場合
参考


コメント