Radioactivity exponential decay 指數衰減 dy/dt= -ky
Example Code
- import numpy as np
- from scipy.integrate import odeint
- import matplotlib.pyplot as plt
- def dU_dx(U, t):
- k=-1;
- return [U[1], -1*U[1]-100*U[0]]
- U0 = [10, 0]
- xs = np.linspace(0, 10, 5000)
- Us = odeint(dU_dx, U0, xs)
- ys = Us[:,0]
- plt.xlabel("t")
- plt.ylabel("x")
- plt.title("Damped harmonic oscillator")
- plt.plot(xs,ys);
- plt.show()
Note and Comments
- import numpy as np: 讀入numpy函式庫並命名為np
- from scipy.integrate import odeint 讀取scipy.integrate函式庫的odeint函式庫 (可使用odeint求解ODE)
- def model(I,t): 定義常微分方程式
- k = 1
- dIdt = -k * I
- return dIdt
- I0 = 1 設定I(t)在t=0的初始值
- I = odeint(model, I0, t): odeeint() 爲解常微分方程的求解程式, 其中第一個變量equation是我們求欲求解的常微分方程, I0爲初始值, t爲變數
result