【发布时间】:2018-12-16 03:28:08
【问题描述】:
我不断收到此错误。我不确定出了什么问题,有人可以帮忙吗?
TypeError: unbound method deriv() must be called with NormalGraph instance as
first argument (got ndarray instance instead)
这是产生错误的代码。当程序不在一个类中时它可以工作,但是当它被放入一个类时它似乎不起作用。这是不断给出错误的类:
class NormalGraph:
def __init__(self, N, R0, I0, S0, beta, gamma, mu):
self.N=N
self.R0=R0
self.I0=I0
self.S0=S0
self.beta=beta
self.gamma=gamma
self.mu=mu
def deriv(y, t, N, beta, gamma, mu):
S, I, R = y
dSdt = mu * N -(beta * S * I) / N - mu * S
dIdt = beta * S * I / N - gamma * I - mu * I
dRdt = gamma * I- mu*R
return dSdt, dIdt, dRdt
def plotGraph(self, S0, I0, R0, beta, gamma, N, mu):
y0 = S0, I0, R0
t = np.linspace(0, 160, 160*2)
ret = odeint(NormalGraph.deriv, y0, t, args=( N, beta, gamma, mu))
S, I, R = ret.T
fig = plt.figure(facecolor='w')
ax = fig.add_subplot(111, axis_bgcolor='#dddddd', axisbelow=True)
ax.plot(t, S/1000, 'b', alpha=0.5, lw=2, label='Susceptible')
ax.plot(t, I/1000, 'r', alpha=0.5, lw=2, label='Infected')
ax.plot(t, R/1000, 'g', alpha=0.5, lw=2, label='Recovered with immunity')
ax.set_xlabel('Time /days')
ax.set_ylabel('Number (1000s)')
ax.set_ylim(0,1.2)
ax.yaxis.set_tick_params(length=0)
ax.xaxis.set_tick_params(length=0)
ax.grid(b=True, which='major', c='w', lw=2, ls='-')
legend = ax.legend()
legend.get_frame().set_alpha(0.5)
for spine in ('top', 'right', 'bottom', 'left'):
ax.spines[spine].set_visible(False)
plt.show()
N = 1000
R0=0
I0=1
S0 = N - I0 - R0
beta=0.2
gamma = 0.1
mu=6
P=0.3
p=0.3
graph=NormalGraph(N, R0, I0, S0, beta, gamma, mu)
graph.plotGraph( S0, I0, R0, beta, gamma, N, mu)
【问题讨论】:
-
您能分享一下
deriv、NormalGraph和Vaccination的定义吗?这段代码不足以理解问题。 -
很抱歉。
标签: python