ValueError:要解压的值太多 - Python

时间:2017-11-27 02:23:05

标签: python python-3.x ode

我试图在Python中绘制一个简单(SIR)流行病模型的相图。但我一直得到一个ValueError:解压缩(预期2)错误的值太多了。有办法解决这个问题吗?

错误似乎与第87行, plt.quiver(S,I,R,dS,dI,dR)有关,到目前为止,谷歌搜索量没有任何帮助。

我正在使用Python 3.6.2,这是我的代码:

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

# the parameters for interactions
beta, gamma = 0.001, 1.0/10

# the initial populations
s0 = 500
i0 = 10
r0 = 0

def ds(s, i, r):
     return -beta * s * i

def di(s, i, r):
     return beta * s * i - gamma * i

def dr(s, i, r):
    return gamma * i

def derivs(state, t):
     s, i, r = state
     deltas = ds(s, i, r)
     deltai = di(s, i, r)
     deltar = dr(s, i, r)
     return deltas, deltai, deltar



 # create a time array from 0..100 sampled at 0.1 second steps
t = np.arange(0.0, 100, 0.1)


y0 = [s0, i0, r0]  # the initial state vector

 # integrate your ODE using scipy.integrate.
y = integrate.odeint(derivs, y0, t)

 # the return value from the integration is a Nx3 array.  Extract it
 # into three 1D arrays caled s,i,r using numpy slice indexing
s = y[:,0]
i = y[:,1]
r = y[:,2]
plt.figure()
plt.plot(t, s, label='S')
plt.plot(t, i, label='I')
plt.plot(t, r, label='R')
plt.xlabel('time (days)')
plt.ylabel('Population')
plt.title('Population trajectories')
plt.grid()
plt.legend()


 # phase-plane plot
plt.figure()
plt.plot(s, i, color='red')
plt.xlabel('S')
plt.ylabel('I')
plt.title('phase plane')

plt.show()

 # Create arrays for S,I,R to represent the entire phase plane --
smax = 1.1 * s.max()
imax = 1.1 * i.max()
rmax = 1.1 * r.max()
S, I, R = np.meshgrid(np.arange(-1, smax), np.arange(-1, imax), np.arange(-1, rmax))
dS = ds(S, I, R)
dI = di(S, I, R)
dR = dr(S, I, R)
plt.quiver(S, I, R, dS, dI, dR)

 # Now find the nul-clines, for dS dI and dR respectively.  These are the
 # points where dS = dI = dR = 0 in the (S, I, R) phase plane.
S, I, R = np.meshgrid(np.arange(-1, smax, 0.1), np.arange(-1, imax, 0.1), np.arange(-1, rmax, 0.1))
dS = ds(S, I, R)
dI = di(S, I, R)
dR = dr(S, I, R)
plt.contour(S, I, R, dS, levels=[0], linewidths=3, colors='blue')
plt.contour(S, I, R, dI, levels=[0], linewidths=3, colors='green')
plt.ylabel('I')
plt.title('trajectory, direction field and null clines')


plt.show()

编辑:完全追溯

Traceback (most recent call last):
  File "S_vs_I copy.py", line 73, in <module>
    plt.quiver(S, I, R, dS, dI, dR)
  File "/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3387, in quiver
    ret = ax.quiver(*args, **kw)
  File "/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1898, in inner
    return func(ax, *args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4594, in quiver
    q = mquiver.Quiver(self, *args, **kw)
  File "/anaconda3/lib/python3.6/site-packages/matplotlib/quiver.py", line 442, in __init__
    X, Y, U, V, C = _parse_args(*args)
  File "/anaconda3/lib/python3.6/site-packages/matplotlib/quiver.py", line 396, in _parse_args
    nr, nc = U.shape
ValueError: too many values to unpack (expected 2)

0 个答案:

没有答案