物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

Python: 常微分方程式を解く

放射性崩壊などの時間発展をPythonで計算してみよう。

寿命 life=10 として、微分方程式は dx/dt=-x/life なので、

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def system(x, t):
    life = 10
    dxdt = -x/life
    return dxdt

x0 = 1
t = np.arange(0, 40, 0.04)
y = odeint(system, x0, t)
plt.plot(t, y)
plt.xlabel('Time')
plt.ylabel('Values')
plt.grid(alpha=0.5)
plt.show()

ちゃんと time=10 で 1/e になってる。