使用Odeint使用微分方程来绘制图形

时间:2015-12-03 16:35:02

标签: python-3.x differential-equations odeint

我对任何编码都非常陌生并且遇到很多python问题(python 3)。 我试图在与艾滋病相关的科学论文中重新绘制数字。他们使用了我必须重新创建的模型的计算机模拟。我不断改变方面并不断收到不同的错误信息 - 当前的错误信息 TypeError: odeint() missing 1 required positional argument: 't'

这是我正在使用的代码,基于之前的任务。

#Import necessary programmes
import numpy as np
from scipy.integrate import odeint as od 
import matplotlib.pyplot as plt
#define equation for uninfected vs free virus
def free_virus(x,y,v,vm):
    x=x=l-(d*x)-(b*x*v)-(bm*x*vm) 
    v=(k*y)-(u*v)
    return np.array(x,v)
l=10
d=0.01
a=0.5
u=3
b=0.01
bm=0.005
e=0.0001
k=km=10
init= [0,0]
t= np.arange(0,40,5)
figure_1=od(free_virus,t,args=(0,40))
plt.plot(figure_1,t)

x和v是科学论文中给出的等式,我需要相互图形化odeint code

这些变量在论文中给出 如果这看起来非常基本,我很抱歉,非常感谢任何帮助。这已被编辑以添加到我的代码中。

1 个答案:

答案 0 :(得分:0)

您没有正确使用它。你的ode函数必须是f(x,t,params)形式,其中x可以是一个向量(在你的情况下为2维:x和v作为变量取决于时间)。

尝试按以下方式重写代码:

#Import necessary programmes
import numpy as np
from scipy.integrate import odeint as od 
import matplotlib.pyplot as plt
#define equation for uninfected vs free virus
def free_virus(X,t,params):
    x = your eq dependent on X, t and params
    v = your second eq
    return np.array(x,v)
paramsDict = {'l':10, 'd':0.01 .... and so on}
init= [0,0]
t= np.arange(0,40,5)
figure_1=od(free_virus,t,args=paramsDict)
plt.plot(figure_1,t)

对于方程参数,最好使用python字典。