python - 用 Gauss Seidel 和鬼点求解边界点的拉普拉斯方程
问题描述
import numpy as np
import matplotlib.pyplot as plt
#Error
error = 0
#Set Maximum Iteration
maxIter = 500
#Set Dimension and Delta
lenX = lenY = 1
delta = 0.1
#Initial guess of interior grid
Tguess = 0
#Set meshgrid
X,Y = np.meshgrid(np.arange(0.1,1.2,delta),np.arange(0.1,1.2,delta))
Tx = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2]
#Texact
Texact = np.empty((13,13))
Texact.fill(Tguess)
for jx in np.arange(0.1,1.3,0.1) :
for jy in np.arange(0.1,1.3,0.1):
Texact = ((np.cos((np.pi)*jx))*(np.sinh((np.pi)*jy)))/(np.sinh(np.pi))
#Set array size and set the interior value with Tguess
T = np.empty((13,13))
T.fill(Tguess)
Tnew = np.empty((13,13))
Tnew.fill(Tguess)
#Set boundary condition
T[0:1 , 11: ] = np.cos ((np.pi))
T[1:1, : ] = T [ 11 :11 , : ]
T[1 : 1 , 1 : 1] = 0
#Iteration
print ("Please wait for a moment")
nof = 0
Tnew = T
Tnew = T .copy()
while (error < 10**(-7)) :
for i in (0,1,2,3,4,5,6,7,8,9,10,11,12,13):
for j in (0,1,2,3,4,5,6,7,8,9,10,11,12,13):
Tnew[i,j]= (0.25 * (T[i+1,j] + Tnew[i-1,j])+(0.25 *( T[i,j+1] + Tnew[i,j-1])))
T[0 , j ] = T [1 ,j ]
T[12 , j ] = T[10,j]
error = np.abs((Texact) - (Tnew))
nof = nof + 1
print(nof)
print("Iteration finished")
我正在尝试用我所说的方法解决拉普拉斯方程,但我的代码没有收敛。问题域为 1-1,网格尺寸为 11*11。我们必须使用在上次迭代中更新的每个点的数量,尤其是之前更新的 i-1 和 j-1,并将这些数量用于 Tnew。我想要的迭代次数和错误必须低于 10 ** -2 但它不在我的代码中。在域的顶部,我们有狄利克雷 bc,它是 cos(pi),它的底部必须是 0。左右壁必须是 Neumann bc(在这种情况下,T 根据 x 的推导必须为 0,我们必须使用高斯点 i+1 和 i-1 应该用一个我不知道该怎么写的函数来更新,而 Neumann bc 是 Ti+1 - Ti-1 =0。我们的域是 1-1,11 个点乘以 0.1步。
解决方案
推荐阅读
- node.js - 如何将nestjs项目部署到cpanel
- python - 如何将 .txt 文件中的 RGB 值转换为在 Python 中显示图像
- security - 每个 Windows 用户是否有唯一的 ID?
- javascript - TypeError:null 不是对象(评估“storagedTechs.split”)
- node.js - 父节点死亡时节点子进程不会死亡
- json - json的颠簸转换-如何为空值添加移位操作
- python - 如何在视图中仅获取模型的一个实例
- android - 返回 null getrequest 改造 kotlin
- r - 仅当另一个选定列符合条件时,如何比较两个单独列的日期
- karate - 使用 karate-netty 模拟复杂端点的最佳实践