Python 索引错误:索引 2 超出轴 0 的范围,大小为 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43728867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:20:42  来源:igfitidea点击:

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

pythonnumpymatplotlibscipy

提问by yonexftw

Im trying to solve 3 differential equations,but I keep getting the following error:

我试图解决 3 个微分方程,但我不断收到以下错误:

This is my code:

这是我的代码:

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')

And this is the error:

这是错误:

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

回答by

(Answer courtsey of Warren Weckesser, provided in the comments to the question.)

(答案由 Warren Weckesser 提供,在问题评论中提供。)

You have Y0 = [E0, C0]. If the system is three-dimensional, then Y0must have three values, so something like Y0 = [E0, C0, l0]where l0is the initial condition for l(t).

你有Y0 = [E0, C0]。如果系统是三维的,则Y0必须具有三个值,因此类似于Y0 = [E0, C0, l0]wherel0的初始条件是l(t)