## 如何在python中将参数滑块添加到微分方程图？

dldt = al - bl*p
dpdt = -cp + dl*p

from scipy.integrate import odeint
import matplotlib.pyplot as plt
from IPython.html.widgets import *
import ipywidgets as wg
from IPython.display import display
from numpy import pi

def f(s, t):

``````a = 1
b = 1
c = 1
d = 0.5
l = s[0]
p = s[1]
dldt = a*l - b*l*p
dpdt = -c*p + d*l*p
return [dldt, dpdt]
``````

t = np.arange(0,10*pi,0.01)
s0=[0.1,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("day in menstrual cycle")
plt.ylabel("concentration (ng/mL)")
plt.legend(["LH","P"])
plt.show()

Python

• 一码平川MACHEL
2019-07-17 23:26:38

您需要一个函数，它将ODE的参数和绘图的其他参数作为命名参数。在最简单的情况下，只有a，b，c，d。此功能需要生成一个图。

def plot_solution(a=1.,b=1.,c=1.,d=0.5):

``````def f(s, t):

l, p = s
dldt = a*l - b*l*p
dpdt = -c*p + d*l*p
return [dldt, dpdt]

t = np.arange(0,10*np.pi,0.01)
s0=[0.1,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("day in menstrual cycle")
plt.ylabel("concentration (ng/mL)")
plt.legend(["LH","P"])
plt.show()``````

然后按照文档中的说明调用交互式小部件功能。为命名参数生成滑块，这些参数以数字对作为输入。

w = interactive(plot_solution, a=(-2.0,2.0), b=(-2.0,2.0), c=(-2.0,2.0), d=(-2.0,2.0))
display(w)

0 0
+ 订阅