確認2B
前回の課題を参考にして、オイラー法を用いて次のベクトル型常微分方程式の初期値問題を解くプログラムを作成せよ。
$ \dot{\bm{x}}(t) = A \bm{x}(t)
$ A = \left[\begin{array}{cc}0 & 1 \\ -1& -2\\\end{array}\right] , \bm{x}(0) = \left[ \begin{array}{c}0 \\ 1 \end{array} \right]
code:check1B.py
import matplotlib.pyplot as plt # 可視化
def xd(x): # <---関数名 yd を xd に変更
return a*x
x = 1 # 初期値
a = -1 # 係数
dt = 0.1 # 刻み時間
x_hist = []
loopmax = 30
i = 0
while i < loopmax:
x = x + xd(x)*dt
x_hist.append(x)
i = i + 1
print(x_hist)
plt.plot(x_hist) # 可視化
plt.show() # 可視化