二维拉普拉斯矩形板传热方程的求解

2024-09-29 01:19:29 发布

您现在位置: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("")

Tags: andtheinfornprangepltdelta
1条回答
网友
1楼 · 发布于 2024-09-29 01:19:29

你需要

X, Y = np.meshgrid(dimX, dimY, indexing='ij')

docs开始:

This function supports both indexing conventions through the indexing keyword argument. Giving the string ‘ij’ returns a meshgrid with matrix indexing, while ‘xy’ returns a meshgrid with Cartesian indexing. In the 2-D case with inputs of length M and N, the outputs are of shape (N, M) for ‘xy’ indexing and (M, N) for ‘ij’ indexing.

(默认值为indexing='xy'


顺便说一句,你的初始条件应该是
T[:, -1] = Ttop
T[:, 0] = Tbottom
T[0, :] = Tright
T[-1, :] = Tleft

相关问题 更多 >