odepack.error:额外的参数必须放在元组中

3

我在使用ODE求解器时遇到了一些问题,我正在尝试解决一个SEIR问题,但是尽管我的代码与我基于的代码非常相似,但我仍然一直得到相同的错误。我的代码如下:

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

# Total population, N.
N1 = 55600
# Initial number of infected and recovered individuals, I0 and R0.
I10, R10, E10 = 1, 0, 0
# Everyone else, S0, is susceptible to infection initially.
S10 = N1 - I10 - R10 - E10
# parameters
B = 0.05
a = 0.000001
d = 0.0167
g = 0.0167
z = 0.0167
M = 100000

# A grid of time points (in months)
t = np.linspace(0, 160, 160)

# The SIR model differential equations.
def deriv(y, t, N1, B, a, d, g, z, M):
S1, E1, I1, R1 = y

dS1dt = B*N1 + d*(R1) - S1/N1 * (M*a(I1))
dE1dt = S1/N1 * M*a(I1) - g * E1
dI1dt = g * E1 - z * I1
dR1dt = z * I1 - d * R1

return dS1dt, dE1dt, dI1dt, dR1dt

# Initial conditions vector
y0 = S10, E10, I10, R10
# Integrate the SIR equations over the time grid, t.
ret = odeint(deriv, y0, t, args=[N1, B, a, d, g, z, M])
S1, E1, I1, R1 = ret.T

我一直收到以下错误:
文件 "C:/Users/Angus/PycharmProjects/firstAttempt/bugfinder.py",第44行,在

处:
  ret = odeint(deriv, y0, t, args=[N1, B, a, d, g, z, M],)

文件“C:\Python36 \lib \site-packages \scipy \integrate \odepack.py”,第215行,odeint函数出错:

ixpr、mxstep、mxhnil、mxordn和mxords这些额外的参数必须放在一个元组中。

任何帮助都将不胜感激!


2
尝试将args=[N1, B, a, d, g, z, M]替换为args=(N1, B, a, d, g, z, M) - akhilsp
来自文档:`args: tuple,可选要传递给函数的额外参数。` - Jean-François Fabre
2个回答

17

非常感谢您!!! - Ramsha Khalid
谢谢@dgruending!这解决了我的问题。 - pukumarathe

2

尝试替换以下内容:

  ret = odeint(deriv, y0, t, args=[N1, B, a, d, g, z, M],)

使用以下方法:

  ret = odeint(deriv, y0, t, args=(N1, B, a, d, g, z, M))

从scipy文档中可以得知:

args:元组,可选

传递给函数的额外参数。

另外,可以查看list和tuple之间的区别


感谢@Jean-FrançoisFabre让答案更清晰。 - akhilsp
1
非常高兴为您服务。您已经找到了解决方法,但我想向您展示如何在格式和参考方面提供更好的答案。这个答案对于不熟悉Python但必须使用特殊软件包的人来说非常有用。 - Jean-François Fabre
感谢@Jean-FrançoisFabre,还有一个小问题是a和I1之间的方程式中没有*,这导致代码无法运行。 - angusr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接