import matplotlib.pylab as plt
from math import *
import numpy as np
#parameters
n= 101
delx= 0.5
delt=0.1
D=1.0
alpha=D*delt/(delx*delx)
#initial profile
a=np.zeros(n)
a[0]=1.0
#loop for time and x
for j in range(0,500):
for i in range(1,101):
a[i]=a[i]*(1-2*alpha)+alpha*(a[i-1]+a[i+1])
a[100]=a[i]*(1-2*alpha)+2*alpha*(a[i-1])
#loop for 20 figures
for j in range(20):
plt.plot(a,'r-')
plt.show()
我得到Index out of bounds error
。我是python的新手,請解釋並修復。 (最初的代碼在Octave中),所以在Python中有這個問題。索引越界錯誤
感謝
這是[a] = a [i] *(1-2 * alpha)+ alpha *(a [i-1] + a [i + 1]) – Ganesh
@Ganesh,'a [i +1]'在這一行的最後會導致問題。 – ForceBru
@Ganesh,我不知道你在想什麼。你想在內循環的每次迭代之後進行繪圖嗎?還是外面的?或者是什麼?我想你最好重新閱讀Octave代碼,並在嘗試將其轉換爲Python之前查看它的運行情況。 – ForceBru