使用居中方法在python中显示和求解二维热方程

时间:2020-05-18 08:53:37

标签: python numerical-methods

我是编程的新手,我在大学里使用Mapple做下面的小练习,但是当我尝试使用python时,我陷入了中间。 练习基本上是在二维拉普拉斯方程的矩形板上应用5点方案离散化(居中方法):

∂^ 2T /∂x^ 2 +∂^ 2T /∂y^ 2 = 0

我基本上想做的事情(我附上了练习的图片):

1-分配边界条件, 2-绘制板的网格(带有每个点的名称,例如:T [1,2],其中1和2是X,Y),Attached example。 3-遍历每个离散离散方程式和每个点的印刷方程式,并带有和不带有边界条件的简化,Attached example 4-打印矩阵,A * B = T Here is an example 5-求解方程组,给出每个点的T。 Example solution

在这里我被困在python中,以防万一我没有走错路径(请注意,我在寻找数值解和数学表示法):

import numpy as np
import sympy as sp
from sympy.solvers import solve

L = 12
H = 12

dx = 3
dy = 3

ndx = int(L/dx)
ndy = int(H/dy)

i_max = ndx + 1
j_max = ndy + 1

Tb = 10
Tl = 40
Tr = 60
Tt = 90

N = (ndx-1)*(ndy-1)


T = np.ones(([ndx, ndy]))


# boundary conditions assignement:

T[:, 0] = Tl
T[:, -1] = Tr
T[0, :] = Tt
T[-1, :] = Tb    
T[0, 0] = (Tt + Tl)/2
T[0, -1] = (Tt + Tr)/2
T[-1, 0] = (Tb + Tl)/2
T[-1, -1] = (Tb + Tr)/2


# ------------

i, j = sp.symbols('i, j')
T_function = sp.Function('-4*T[i, j] + T[i-1, j] + T[i+1, j] + T[i, j-1] + T[i, j+1]')

for i in range(1, i_max-1):
    for j in range(1, j_max-1):
        solve(T_function, T[i, j])


# -4*T[i, j] + T[i-1, j] + T[i+1, j] + T[i, j-1] + T[i, j+1]

非常感谢您的时间!

0 个答案:

没有答案