確認2B
/icons/hr.icon
次の微分方程式をオイラー法を用いて解くプログラムを作成せよ。
$ {\boldsymbol x}'(t) = A {\boldsymbol x}(t)
ただし、
$ A = \left[ \begin{array}{cc} 0 & 1 \\ -1 & -2 \end{array} \right], {\boldsymbol x}(0) = \left[\begin{array}{c} 0\\ 1\end{array}\right]
提出物はプログラムファイルです。
先頭にコメントで課題名(「情報通信プロジェクト演習 確認2B」)、所属、学籍番号、氏名を記述してください。
授業後半で解説予定
以下解説結果~~~~~~~~~~~~~~~~~~~~~
code:check2b.py
# コメントで課題名などを記載する
import matplotlib.pyplot as plt # 可視化
import numpy as np
def xd(x):
return A@x
x = np.array(0], [1)
A = np.array(0, 1], [-1, -2)
ts, tf, dt = 0, 10, 0.1 # 初期時刻、終端時刻、刻み時間
x_hist = [] # 軌道を保持するためのリスト
times = np.arange(ts, tf, dt)
for i in times:
x = x + xd(x)*dt
x_hist.append(x.flatten()) # (2, 1) の2d-arrayから(2, )の1d-arrayを生成
x_hist = np.stack(x_hist) # (2, )の1d-arrayをもつリストから(2, maxloop)の2d-arrayを生成する
# print(x_hist)
plt.plot(times, x_hist:,0) # 可視化
plt.show() # 可視化
"""
[
array(0.1],[0.8),
array(0.18],[0.63),
array(0.243],[0.486)
]
"""
/icons/hr.icon