首页 > 解决方案 > 求解通过矩形板的传热的二维拉普拉斯方程

问题描述

在尝试执行通过矩形板进行传热的 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("")

标签: pythonnumpy

解决方案


你需要

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

推荐阅读