常微分方程式の定性的な挙動は2次元までなら手で何とかなるとは言ってもやはり可視化,特にアニメーションにしたいですよね.
次のサンプルで解いてるのは減衰振動です.funcを適当にいじれば非線形でもOKです.
僕はまだ生後260ヶ月の赤ちゃんなので,実行例のgifは添付できてない上につい先日までスライス機能を知らず積分をfor文の中で実行しており計算時間がエライことになってました.
DiffAni.ipynb
%matplotlib nbagg
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
from numpy import sin, cos, pi
from matplotlib.animation import ArtistAnimation
# Draw phase space of 2-D differential eq
def func(v, t, g, k):
return [ v[1], -g*v[1]-k*v[0]] # 2-D damping oscillator
# parameters
g = 0.1
k = 3
# initial state
v0=[10,0]
# time interval
dt = 0.1
T = np.arange(0.0, 100, dt)
# integrate
v = odeint(func, v0, T, args=(g,k))
# make animation
fig = plt.figure()
ims = []
for i in range(len(T)):
line = plt.plot(v[:i,0], v[:i,1],'r')
ims.append(line)
plt.xlim(-17 ,17)
plt.ylim(-17 ,17)
plt.grid()
ani = ArtistAnimation(fig, ims, interval=17, blit=True, repeat=True)
plt.show()
jitでfor文を高速化してみたかったのですが,試しに実装してみたら寧ろ遅くなったんですよね.このことも後々記事にするかもしれないです.