python - 求解通过矩形板的传热的二维拉普拉斯方程
问题描述
在尝试执行通过矩形板进行传热的 Python 代码时,其尺寸在 X 方向为 3 米,在 Y 方向为 5 米。我在尝试绘制网格时遇到错误,错误是 x (50, 30) 和 z (30, 50) 的形状不匹配。
这是代码
# Simple Numerical Laplace Equation Solution using Finite Difference Method
import numpy as np
import matplotlib.pyplot as plt
# Set maximum iteration
maxIter = 700
# Set Dimension and delta
lenX = 30
lenY = 50
delta = 1
# Boundary condition
Tleft = 100
Tright = 75
Ttop = 250
Tbottom = 300
# Initial guess of interior grid
Tguess = 0
# Set colour interpolation and colour map
colorinterpolation = 50
colourMap = plt.cm.coolwarm
# Set meshgrid
n1, n2 = (30, 50)
dimX = np.linspace(0, 30, n1)
dimY = np.linspace(0, 50, n2)
X, Y = np.meshgrid(dimX, dimY)
# Set array size and set the interior value with Tguess
T = np.empty((lenX, lenY))
T.fill(Tguess)
# Set Boundary condition
T[(lenY-1):, :] = Ttop
T[:1, :] = Tbottom
T[:, (lenX-1):] = Tright
T[:, :1] = Tleft
# Iteration (We assume that the iteration is convergence in maxIter = 700)
print("Please wait for a moment")
for iteration in range(0, maxIter):
for i in range(1, lenX-1, delta):
for j in range(1, lenY-1, delta):
T[i, j] = 0.25 * (T[i+1][j] + T[i-1][j] + T[i][j+1] + T[i][j-1])
print("Iteration finished")
# Configure the contour
plt.title("Contour of Temperature")
plt.contourf(X, Y, T, colorinterpolation, cmap=colourMap)
# Set Colorbar
plt.colorbar()
# Show the result in the plot window
plt.show()
print("")
解决方案
你需要
X, Y = np.meshgrid(dimX, dimY, indexing='ij')
从文档:
此函数通过 indexing 关键字参数支持两种索引约定。给字符串 'ij' 返回一个带有矩阵索引的网格,而 'xy' 返回一个带有笛卡尔索引的网格。在输入长度为 M 和 N 的二维情况下,对于“xy”索引,输出形状为 (N, M),对于“ij”索引,输出形状为 (M, N)。
(默认为indexing='xy'
)
顺便说一句,您的初始条件似乎应该是
T[:, -1] = Ttop
T[:, 0] = Tbottom
T[0, :] = Tright
T[-1, :] = Tleft
推荐阅读
- python - 您如何使用在网络选项卡中不生成 api 的 scrapy 抓取网站?
- python - 如何在 Oozie 工作流中执行 Python?
- azure-data-factory-2 - 如何比较 ADF 数据流中的 2 个数据集并保存差异?
- c - 你如何存储来自同一输入的 2 个不同的值
- php - 我如何将域(例如 example.com)重定向到 AWS Route53 中的子目录 example.com/data/
- r - 如何根据每行的一部分按字母顺序对文本行进行排序?
- java - 如何使用 ZonedDateTime 将日期字符串格式化为日期和时间?
- symfony - Symfony 5 在条件下切换缓存适配器
- c++ - 持有数组内存布局的类数组
- linux - 是否可以更换实时安装的 BTRFS RAID 上的驱动器?