我有一个函数,通过迭代for循环,使用值填充100x100 2D数组。我需要将值存储在[i] [i],[i] [i-1]和[i] [i + 1]位置。这是我到目前为止的内容:
Delta = 1/100
N = 100
Matrix = np.zeros(N,N), int)
for i in range(N):
Matrix[i][i] = ((2*D)/pow(Delta,2) + SigmaA)
Matrix[i][i-1] = -(D/pow(Delta,2))
Matrix[i][i+1] = -(D/pow(Delta,2))
答案 0 :(得分:0)
i+1
和i-1
都试图访问无效的索引。使矩阵1
大于所需的矩阵,并使range
从1开始并转到N + 1。
Delta = 1/100
N = 100
Matrix = np.zeros(N+1,N+1), int)
for i in range(1,N+1):
Matrix[i][i] = ((2*D)/pow(Delta,2) + SigmaA)
Matrix[i][i-1] = -(D/pow(Delta,2))
Matrix[i][i+1] = -(D/pow(Delta,2))