用雅可比法求解拉普尔方程PYTHON(using jacobi method to solve laplace equation PYTHON)

编程入门 行业动态 更新时间:2024-10-28 18:28:37
用雅可比法求解拉普尔方程PYTHON(using jacobi method to solve laplace equation PYTHON)

我对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 v

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. 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

更多推荐

本文发布于:2023-07-05 14:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1038507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:普尔   方程   PYTHON   jacobi   equation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!