IndexError: index out of bounds
IndexError: index out of bounds
我是 Python 的新手,感谢任何建设性的反馈。
我的任务是使用 ODEINT 求解 PDE,其中我的空间网格定义如下:
L = [8.0, 4.0, 8.0] # Lenght of spatial zones
dN = 1.0e2 # Number of grid points
N = [int(l*dN) for l in L] # Gives number of grid points over each spatial zone
通过使用 ODEINT 函数,我相信我必须为我的解决方案定义一个零数组和一个时间点数组。我正在努力找出如何为数组编写正确的大小。我试过:
dydt = np.zeros(shape=N) # For solution
t = np.linspace(0, 2, 2000) # for time
但这给了我错误信息:
IndexError Traceback(最后一次调用)
在 ()
70 t = np.linspace(0, 2, lN)
71
---> 72 U = odeint(数字, y0, t, args=(h, D1, D2, N))
73
74 退出 = U/Np
/Users/severinlangeberg/anaconda/lib/python3.5/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)
213 输出 = _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 如果输出 [-1] < 0:
217 warning_msg = _msgs[output[-1]] + " 运行 with full_output = 1 得到量化信息。"
in numeric(y, t, h, D1, D2, N)
39
40 for i in [N[0] + 1]: # 800 = special 2, 必须从 N[1] 开始
---> 41 dydt[i] = D2 * (-3*y[i] + 4*y[i + 1] - y[i + 2] / h**2) # pluss
42
43 for i in range (N[0] + 2, N[0] + N[1] - 2): # 从 801 到 1197,或者 2 到 397
IndexError:索引 801 超出尺寸为 800 的轴 0 的范围
Which seems to come from my loops:
# ODE setup
def numeric(y, t, h, D1, D2, N):
dydt = np.zeros((N))
# End grid points
dydt[0] = 0
dydt[-1] = 0
# Inner grid points
for i in range (1, N[0] - 2): # 1 to 797
dydt[i] = D1 * (y[i - 1] - 2*y[i]+ y[i + 1]) / h**2 # range i = j - 1
for i in [N[0] - 2]: # 798 = special 1
dydt[i] = D1 * (-3*y[i] - 4*y[i - 1] + y[i - 2]) / h**2 # minus
for i in [N[0] - 1]: # 799 = unknown, and last index in N[0]
dydt[i] = ((D1*(-3*y[i] + 4*y[i + 1] - y[i + 2])) - (D2*(-3*y[i] - 4*y[i - 1] + y[i - 2])))
for i in [N[0] + 1]: # 800 = special 2, must be from begining of N[1]
dydt[i] = D2 * (-3*y[i] + 4*y[i + 1] - y[i + 2] / h**2) # pluss
for i in range (N[0] + 2, N[0] + N[1] - 2): # from 801 to 1197, or 2 to 397
dydt[i] = D2 * (y[i - 1] - 2*y[i] + y[i + 1]) / h**2 # range
for i in [N[0] + N[1] - 1]: # at 1198, or 398 = special 3
dydt[i] = D2 * (-3*y[i] - 4*y[i - 1] + y[i - 2]) / h**2 # range
for i in [N[0] + N[1]]: # 1199, or 399 = unknown
dydt[i] = ((D1*(-3*y[i] + 4*y[i + 1] - y[i + 2])) - (D2*(-3*y[i] - 4*y[i - 1] + y[i - 2])))
for i in [N[0] + N[1] + 1]: # at 1200, or 1 = special 4
dydt[i] = D1 * (-3*y[i + 1] + 4*y[i + 2] - y[i + 3] / h**2) # pluss
for i in range (N[0] + N[1] + 2, N[0] + N[1] + N[2] - 1): # 1202 to 1999
dydt[i] = D1 * (y[i - 1] - 2*y[i]+ y[i + 1]) / h**2 # range
return dydt
乍一看,问题似乎出在 for 循环上,而不是使用 for i in [N[0]]
,您确定不是 for i in range(N[0])
吗?
for i in [N[0]]
相当多余,因为列表只包含一个元素,即。 N[0]
,因此当您尝试查找 dydt[800]
<-- 最大索引为 799
时,大小为 800 的列表将超出索引。
像这样:
In [9]: N
Out[9]: [800, 400, 800]
In [10]: for i in [N[0]]: # ie. i == N[0] == 800
....: dydt[i] = 0
....:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-10-04daa45ec403> in <module>()
1 for i in [N[0]]: # ie. i == N[0] == 800
----> 2 dydt[i] = 0
3
IndexError: index 800 is out of bounds for axis 0 with size 800
In [11]: for i in range(N[0]):
....: dydt[i] = 0
....:
L = [8.0, 4.0, 8.0]
dN = 1.0e2
N = [int(l*dN) for l in L] # create array [800, 400, 800]
dydt = np.zeros(shape=N) # create 3 dimensional matrix
# 1d has size 800 (from 0 to 799 including)
# 2d has size 400 (from 0 to 399 including)
# 3d same as 1st
在此代码中:
for i in[N[0]]:
dydt[i] = 0 # Unknown point
我总是 800 所以它实际上与 dydt[ 800 ] = 0
相同。大于 799.
看来你使用 numpy 数组的方式不对。
是的,在低级别它们表示为连续的整数或浮点数数组。但是在 python 级别 np.array 表示为数组数组。 (即 python 中作为 3d 矩阵 mtx = np.zeros(shape=[800,400,800]
的第一个元素将是 mtx[0][0][0]
))。
因此 for i in[N[0]]:
之后的所有代码都将无效。它试图访问超出矩阵边界的子数组。
更新:
例如你有 3d 矩阵 (3x3x3)
mtx=np.zero(shape=[3,3,3])
print(mtx)
#[ [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ]
# [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ]
# [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ] ]
mtx[0] = 1
print( mtx )
#[ [ [ 1. 1. 1.]
# [ 1. 1. 1.]
# [ 1. 1. 1.] ]
# [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ]
# [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ] ]
mtx[0][1] = 2
#[ [ [ 1. 1. 1.]
# [ 2. 2. 2.]
# [ 1. 1. 1.] ]
# others are zeros
mtx[0][1][2] = 3
#[ [ [ 1. 1. 1.]
# [ 2. 2. 3.]
# [ 1. 1. 1.] ]
# others are zeros
我是 Python 的新手,感谢任何建设性的反馈。 我的任务是使用 ODEINT 求解 PDE,其中我的空间网格定义如下:
L = [8.0, 4.0, 8.0] # Lenght of spatial zones
dN = 1.0e2 # Number of grid points
N = [int(l*dN) for l in L] # Gives number of grid points over each spatial zone
通过使用 ODEINT 函数,我相信我必须为我的解决方案定义一个零数组和一个时间点数组。我正在努力找出如何为数组编写正确的大小。我试过:
dydt = np.zeros(shape=N) # For solution
t = np.linspace(0, 2, 2000) # for time
但这给了我错误信息:
IndexError Traceback(最后一次调用) 在 () 70 t = np.linspace(0, 2, lN) 71 ---> 72 U = odeint(数字, y0, t, args=(h, D1, D2, N)) 73 74 退出 = U/Np
/Users/severinlangeberg/anaconda/lib/python3.5/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) 213 输出 = _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 如果输出 [-1] < 0: 217 warning_msg = _msgs[output[-1]] + " 运行 with full_output = 1 得到量化信息。"
in numeric(y, t, h, D1, D2, N) 39 40 for i in [N[0] + 1]: # 800 = special 2, 必须从 N[1] 开始 ---> 41 dydt[i] = D2 * (-3*y[i] + 4*y[i + 1] - y[i + 2] / h**2) # pluss 42 43 for i in range (N[0] + 2, N[0] + N[1] - 2): # 从 801 到 1197,或者 2 到 397
IndexError:索引 801 超出尺寸为 800 的轴 0 的范围
Which seems to come from my loops:
# ODE setup
def numeric(y, t, h, D1, D2, N):
dydt = np.zeros((N))
# End grid points
dydt[0] = 0
dydt[-1] = 0
# Inner grid points
for i in range (1, N[0] - 2): # 1 to 797
dydt[i] = D1 * (y[i - 1] - 2*y[i]+ y[i + 1]) / h**2 # range i = j - 1
for i in [N[0] - 2]: # 798 = special 1
dydt[i] = D1 * (-3*y[i] - 4*y[i - 1] + y[i - 2]) / h**2 # minus
for i in [N[0] - 1]: # 799 = unknown, and last index in N[0]
dydt[i] = ((D1*(-3*y[i] + 4*y[i + 1] - y[i + 2])) - (D2*(-3*y[i] - 4*y[i - 1] + y[i - 2])))
for i in [N[0] + 1]: # 800 = special 2, must be from begining of N[1]
dydt[i] = D2 * (-3*y[i] + 4*y[i + 1] - y[i + 2] / h**2) # pluss
for i in range (N[0] + 2, N[0] + N[1] - 2): # from 801 to 1197, or 2 to 397
dydt[i] = D2 * (y[i - 1] - 2*y[i] + y[i + 1]) / h**2 # range
for i in [N[0] + N[1] - 1]: # at 1198, or 398 = special 3
dydt[i] = D2 * (-3*y[i] - 4*y[i - 1] + y[i - 2]) / h**2 # range
for i in [N[0] + N[1]]: # 1199, or 399 = unknown
dydt[i] = ((D1*(-3*y[i] + 4*y[i + 1] - y[i + 2])) - (D2*(-3*y[i] - 4*y[i - 1] + y[i - 2])))
for i in [N[0] + N[1] + 1]: # at 1200, or 1 = special 4
dydt[i] = D1 * (-3*y[i + 1] + 4*y[i + 2] - y[i + 3] / h**2) # pluss
for i in range (N[0] + N[1] + 2, N[0] + N[1] + N[2] - 1): # 1202 to 1999
dydt[i] = D1 * (y[i - 1] - 2*y[i]+ y[i + 1]) / h**2 # range
return dydt
乍一看,问题似乎出在 for 循环上,而不是使用 for i in [N[0]]
,您确定不是 for i in range(N[0])
吗?
for i in [N[0]]
相当多余,因为列表只包含一个元素,即。 N[0]
,因此当您尝试查找 dydt[800]
<-- 最大索引为 799
时,大小为 800 的列表将超出索引。
像这样:
In [9]: N
Out[9]: [800, 400, 800]
In [10]: for i in [N[0]]: # ie. i == N[0] == 800
....: dydt[i] = 0
....:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-10-04daa45ec403> in <module>()
1 for i in [N[0]]: # ie. i == N[0] == 800
----> 2 dydt[i] = 0
3
IndexError: index 800 is out of bounds for axis 0 with size 800
In [11]: for i in range(N[0]):
....: dydt[i] = 0
....:
L = [8.0, 4.0, 8.0]
dN = 1.0e2
N = [int(l*dN) for l in L] # create array [800, 400, 800]
dydt = np.zeros(shape=N) # create 3 dimensional matrix
# 1d has size 800 (from 0 to 799 including)
# 2d has size 400 (from 0 to 399 including)
# 3d same as 1st
在此代码中:
for i in[N[0]]:
dydt[i] = 0 # Unknown point
我总是 800 所以它实际上与 dydt[ 800 ] = 0
相同。大于 799.
看来你使用 numpy 数组的方式不对。
是的,在低级别它们表示为连续的整数或浮点数数组。但是在 python 级别 np.array 表示为数组数组。 (即 python 中作为 3d 矩阵 mtx = np.zeros(shape=[800,400,800]
的第一个元素将是 mtx[0][0][0]
))。
因此 for i in[N[0]]:
之后的所有代码都将无效。它试图访问超出矩阵边界的子数组。
更新:
例如你有 3d 矩阵 (3x3x3)
mtx=np.zero(shape=[3,3,3])
print(mtx)
#[ [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ]
# [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ]
# [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ] ]
mtx[0] = 1
print( mtx )
#[ [ [ 1. 1. 1.]
# [ 1. 1. 1.]
# [ 1. 1. 1.] ]
# [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ]
# [ [ 0. 0. 0.]
# [ 0. 0. 0.]
# [ 0. 0. 0.] ] ]
mtx[0][1] = 2
#[ [ [ 1. 1. 1.]
# [ 2. 2. 2.]
# [ 1. 1. 1.] ]
# others are zeros
mtx[0][1][2] = 3
#[ [ [ 1. 1. 1.]
# [ 2. 2. 3.]
# [ 1. 1. 1.] ]
# others are zeros