3

I'm having some issues with my ode solver, I am trying to solve an SEIR problem and I keep getting the same errors dispite the code that i have based my code on being very similar. My code is:

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

I keep getting the errors:

File "C:/Users/Angus/PycharmProjects/firstAttempt/bugfinder.py", line 44, in

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

File "C:\Python36\lib\site-packages\scipy\integrate\odepack.py", line 215, in odeint ixpr, mxstep, mxhnil, mxordn, mxords) odepack.error: Extra arguments must be in a tuple

Any help would be appreciated!

angusr
  • 63
  • 1
  • 1
  • 5

2 Answers2

17

For a single argument do not forget to add the comma in the tuple e.g.

    ret = odeint(deriv, y0, t, args=(singleArg,))

See here https://www.tutorialspoint.com/python/python_tuples.htm for single valued tuples.

Unfortunately I do not have enough reputation for a comment.

dgruending
  • 973
  • 9
  • 15
2

Try replacing:

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

with this:

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

From the scipy documentation:

args : tuple, optional

Extra arguments to pass to function.

Also, google differences b/w list and tuple.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
akhilsp
  • 1,063
  • 2
  • 13
  • 26
  • Thanks @Jean-FrançoisFabre for making the answer clear – akhilsp Aug 17 '17 at 12:24
  • 1
    my pleasure. You found the fix, but I wanted to show you how to make a better answer in terms of formatting and references. This answer is useful for people _having_ to use python to use the special package, and not knowing a lot about python. – Jean-François Fabre Aug 17 '17 at 12:26
  • Thanks for this @Jean-FrançoisFabre there was also a small issue with the equations between the a and I1 there was no * which was stopping the code from running – angusr Aug 18 '17 at 00:20