回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我昨天刚开始使用Python,使用<code>scipy.integrate.odeint</code>时出现错误。</p>
<p>我定义了一个函数</p>
<pre><code>def SIR(x, t, beta, gamma, mu, M):
</code></pre>
<p>它接受<code>numpy.array</code>对象<code>x</code>、<code>t</code>和<code>M</code>;标量浮动<code>beta</code>、<code>gamma</code>和<code>mu</code>。</p>
<p><code>M</code>的大小是<code>(60,60)</code>,但我认为这无关紧要。</p>
<p><code>x</code>和<code>t</code>都是不独立的,其中<code>x.shape</code>是<code>(180,)</code>,<code>t.shape</code>是<code>(5000,)</code>。我试着给它们一个单重维度,使它们分别具有形状<code>(180,1)</code>和<code>(5000,1)</code>,但我仍然得到相同的错误:</p>
<pre><code>In [1]: run measles_age.py
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/utils/py3compat.py in execfile(fname, *where)
173 else:
174 filename = fname
--> 175 __builtin__.execfile(filename, *where)
/Users/qcaudron/Documents/SIR/measles_age.py in <module>()
111
112
--> 113 x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
114
115 # plot(t, x);
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/scipy/integrate/odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg)
141 output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
142 full_output, rtol, atol, tcrit, h0, hmax, hmin,
--> 143 ixpr, mxstep, mxhnil, mxordn, mxords)
144 if output[-1] < 0:
145 print _msgs[output[-1]]
</code></pre>
<p>即使当<code>SIR</code>只返回<code>x</code>时,我也会得到这个错误,如果我从中除去<code>x</code>和<code>t</code>之外的所有参数:</p>
<pre><code>def SIR(x, t):
return x;
</code></pre>
<p>如您所见,导致错误的行是</p>
<pre><code>x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
</code></pre>
<p>编辑:</p>
<p>我被要求添加<code>SIR</code>方法的完整代码。因为它比较长,所以我将完整的.py脚本放到了一个pastebin中:
<a href="http://pastebin.com/RphJbCHN" rel="nofollow">http://pastebin.com/RphJbCHN</a></p>
<p>再次感谢。</p>