【问题标题】:Error: index 2 is out of bounds for axis 0 with size 1错误:索引 2 超出轴 0 的范围,大小为 1
【发布时间】:2017-06-01 23:49:04
【问题描述】:

我对以下使用 Python (odeint) 求解激光速率方程(一阶常微分方程)的小程序有疑问。

当我运行程序时,总是出现错误:

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

【问题讨论】:

    标签: python differential-equations rate odeint


    【解决方案1】:

    尝试在第 2 行和第 3 行之间输入 print y,看看你真正拥有什么。

    这意味着y 只有一个元素。当列表只有一个时,您无法获取元素 2(第三个元素)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2015-10-13
      • 2019-01-21
      相关资源
      最近更新 更多