【问题标题】:IndexError: index 2 is out of bounds for axis 0 with size 2IndexError:索引 2 超出轴 0 的范围,大小为 2
【发布时间】:2017-05-02 01:47:38
【问题描述】:

我正在尝试求解 3 个微分方程,但我不断收到以下错误:

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
T=np.arange(0, 7, 1e-4)

k1=0.92
k2=0.47
k3=0.25
m = 50
def SistemaEqDif(Y,t):
    s=Y[0]
    b=Y[1]
    l=Y[2]
    dEdt = -k1*s
    dCdt = (k1/(0.05*m))-k2*b
    dldt = k2*b-k3*l
    return [dEdt, dCdt,dldt]
C0 = 0
E0 = 1 
Y0 = [E0, C0]
Y = odeint(SistemaEqDif,Y0,T)
plt.plot(T,Y[:,0],'g')
plt.plot(T,Y[:,1],'r')
plt.plot(T,Y[:,2],'b')

这是错误:

File "/Users/arihalpern/untitled29.py", line 21, in SistemaEqDif
    l=Y[2]
IndexError: index 2 is out of bounds for axis 0 with size 2

【问题讨论】:

  • Y 的长度是 2 而不是 3。
  • 我该如何纠正这个问题?
  • 你有Y0 = [E0, C0]。如果系统是三维的,那么Y0 必须有三个值,所以类似于Y0 = [E0, C0, l0] 其中l0 是l(t) 的初始条件。
  • 非常感谢!成功了

标签: python numpy matplotlib scipy


【解决方案1】:

(答案由 Warren Weckesser 提供,在 comments to the question 中提供。)

你有Y0 = [E0, C0]。如果系统是三维的,那么Y0 必须有三个值,所以类似于Y0 = [E0, C0, l0],其中l0 是l(t) 的初始条件。

【讨论】:

    猜你喜欢
    • 2019-03-01
    • 1970-01-01
    • 2020-11-22
    • 2020-01-16
    • 1970-01-01
    • 2017-02-16
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多