Python中微分方程的绘图系统

时间:2017-03-18 00:16:20

标签: python matplotlib plot

我刚刚开始使用python进行科学绘图来绘制微分方程的数值解。我知道如何使用N' = a * N - (C/(1+C)) * b * N C' = (C/(1+C)) * N - C + 1 a = 4 b = 7 N(0) = 100 C(0) = 5 求解和绘制单微分方程,但不了解微分方程系统。我如何绘制以下耦合系统?

{{1}}

1 个答案:

答案 0 :(得分:3)

只需将所有变量定义为空间矢量,然后应用积分:

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

def f(s,t):
    a = 4
    b = 7
    n = s[0]
    c = s[1]
    dndt = a * n - (c/(c+1)) * b * n
    dcdt = (c/(c+1)) * n - c + 1
    return [dndt, dcdt]

t = np.linspace(0,20)
s0=[20,5]

s = odeint(f,s0,t)

plt.plot(t,s[:,0],'r--', linewidth=2.0)
plt.plot(t,s[:,1],'b-', linewidth=2.0)
plt.xlabel("t")
plt.ylabel("S[N,C]")
plt.legend(["N","C"])
plt.show()

enter image description here