我对python相当陌生,我试图用拉普尔方程和雅可比方法在金属盒中重新创建电位。 我已经写了一个似乎最初工作的代码,但是我收到错误:IndexError:索引8超出了轴7的大小为7的界限,并且无法找出原因。 任何帮助都是极好的!
from visual import* from visual.graph import* import numpy as np lenx = leny = 7 delta = 2 vtop = [-1,-.67,-.33,.00,.33,.67,1] vbottom = [-1,-.67,-.33,.00,.33,.67,1] vleft = -1 vright = 1 vguess= 0 x,y = np.meshgrid(np.arange(0,lenx), np.arange(0,leny)) v = np.empty((lenx,leny)) v.fill(vguess) v[(leny-1):,:] = vtop v [:1,:] = vbottom v[:,(lenx-1):] = vright v[:,:1] = vleft maxit = 500 for iteration in range (0,maxit): for i in range(1,lenx): for j in range(1,leny-1): v[i,j] = .25*(v[i+i][j] + v[i-1][j] + v[i][j+1] + v[i][j-1]) print vI am fairly new to python and am trying to recreate the electric potential in a metal box using the laplace equation and the jacobi method. I have written a code that seems to work initially, however I am getting the error: IndexError: index 8 is out of bounds for axis 0 with size 7 and can not figure out why. any help would be awesome!
from visual import* from visual.graph import* import numpy as np lenx = leny = 7 delta = 2 vtop = [-1,-.67,-.33,.00,.33,.67,1] vbottom = [-1,-.67,-.33,.00,.33,.67,1] vleft = -1 vright = 1 vguess= 0 x,y = np.meshgrid(np.arange(0,lenx), np.arange(0,leny)) v = np.empty((lenx,leny)) v.fill(vguess) v[(leny-1):,:] = vtop v [:1,:] = vbottom v[:,(lenx-1):] = vright v[:,:1] = vleft maxit = 500 for iteration in range (0,maxit): for i in range(1,lenx): for j in range(1,leny-1): v[i,j] = .25*(v[i+i][j] + v[i-1][j] + v[i][j+1] + v[i][j-1]) print v最满意答案
只需快速浏览一下你的代码,就好像索引错误发生在这个部分一样,并可以相应地改变:
# you had v[i+i][j] instead if v[i+1][j] v[i,j] = .25*(v[i+1][j] + v[i-1][j] + v[i][j+1] + v[i][j-1])您只需将i添加到您的索引中,而这些索引肯定会超出范围
Just from a quick glance at your code it seems as though the indexing error is happening at this part and can be changed accordingly:
# you had v[i+i][j] instead if v[i+1][j] v[i,j] = .25*(v[i+1][j] + v[i-1][j] + v[i][j+1] + v[i][j-1])You simply added and extra i to your indexing which would have definitely been out of range
更多推荐
发布评论