错误:索引2超出轴1的大小1的范围

问题描述:

我使用Python(odeint)解决激光速率方程(常微分方程第一阶)的以下小程序存在问题。错误:索引2超出轴1的大小1的范围

当我运行该程序,错误总是显示出来:

index 2 is out of bounds for axis 0 with size 1 

的公式是正确的;我不知道这个错误是什么意思。

任何人都可以解释这是什么意思,以及如何解决它?

def Rate(y,t): 
    D = y[0] 
    P = y[1] 
    w = 1.3 
    F = 0.0001 
    R = 90 
    dD_dt= w - D*P -D 
    dP_dt= R*D*P - R*P +F*D 
    dydt = [dD_dt,dP_dt] 
    return dydt 
    y0=0 
    t=np.arange(0,100,0.01) 
    sol=odeint(Rate,y0,t) 
    plt.plot(t,sol[:,0],'b',label='normalized inversion(t)') 
    plt.plot(t,sol[:,1],'g',label='normalized photon density(t)') 
    plt.legend() 
    plt.xlabel('t') 
    plt.grid() 
    plt.show() 

和错误是:

IndexError        Traceback (most recent call last) 
    <ipython-input-20-50f3d07b9161> in <module>() 
    ----> 1 sol=odeint(Rate,y0,t) 

    C:\Users\asem\Anaconda2\lib\site-packages\scipy\integrate\odepack.pyc 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) 
    213  output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu, 
    214        full_output, rtol, atol, tcrit, h0, hmax, 
    hmin, 
    --> 215        ixpr, mxstep, mxhnil, mxordn, mxords) 
    216  if output[-1] < 0: 
    217   warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to 
    get quantitative information." 

    <ipython-input-18-2f25cf16efde> in Rate(y, t) 
     1 def Rate(y,t): 
     2  D =y[0] 
     ----> 3  P =y[2] 
     4  w = 1.3 
     5  F = 0.0001 

     IndexError: index 2 is out of bounds for axis 0 with size 1 

尝试线2和3之间print y看看你真的有。

这意味着y只有一个元素。当列表只有一个时,你不能得到元素2(第三个元素)。

请注意,您的代码和错误消息不匹配(y [1]与y [2])。