简体   繁体   English

使用雅可比方法求解拉普拉斯方程PYTHON

[英]using jacobi method to solve laplace equation PYTHON

I 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. 我对python相当陌生,正在尝试使用laplace方程和jacobi方法在金属盒中重新创建电势。 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. 我已经编写了一个似乎最初可以工作的代码,但是出现错误:IndexError:索引8超出了轴0的大小7的范围,无法弄清原因。 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

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 您只是在索引中添加了额外的i ,这肯定超出范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM