边界条件在热方程和Crank-Nicholson有限差分解中的应用
下面的代码求解一维热方程,该方程表示一根杆,其两端保持在零温度,初始条件为 10*np.sin(np.pi*x)。
狄利克雷边界条件(两端为零温度)如何应用于此计算?有人告诉我矩阵 A 的上下两行包含两个非零元素,缺少的第三个元素是狄利克雷条件。但我不明白这个条件通过什么机制影响计算。如果 A 中缺少元素,u_{0} 或 u_{n} 怎么可能为零?
下面的有限差分法使用 Crank-Nicholson。
import numpy as np
import scipy.linalg
# Number of internal points
N = 200
# Calculate Spatial Step-Size
h = 1/(N+1.0)
k = h/2
x = np.linspace(0,1,N+2)
x = x[1:-1] # get rid of the '0' and '1' at each end
# Initial Conditions
u = np.transpose(np.mat(10*np.sin(np.pi*x)))
# second derivative matrix
I2 = -2*np.eye(N)
E = np.diag(np.ones((N-1)), k=1)
D2 = (I2 + E + E.T)/(h**2)
I = np.eye(N)
TFinal = 1
NumOfTimeSteps = int(TFinal/k)
for i in range(NumOfTimeSteps):
# Solve the System: (I - k/2*D2) u_new = (I + k/2*D2)*u_old
A = (I - k/2*D2)
b = np.dot((I + k/2*D2), u)
u = scipy.linalg.solve(A, b)
The code below solves the 1D heat equation that represents a rod whose ends are kept at zero temparature with initial condition 10*np.sin(np.pi*x).
How are the Dirichlet boundary conditions (zero temp at both ends) worked into this calculation? I was told the upper, lower rows of matrix A contain two non-zero elements, and the missing third element is the Dirichlet condition. But I do not understand by which mechanism this condition affects the calculation. With missing elements in A, how can u_{0} or u_{n} be zero?
The finite difference method below uses Crank-Nicholson.
import numpy as np
import scipy.linalg
# Number of internal points
N = 200
# Calculate Spatial Step-Size
h = 1/(N+1.0)
k = h/2
x = np.linspace(0,1,N+2)
x = x[1:-1] # get rid of the '0' and '1' at each end
# Initial Conditions
u = np.transpose(np.mat(10*np.sin(np.pi*x)))
# second derivative matrix
I2 = -2*np.eye(N)
E = np.diag(np.ones((N-1)), k=1)
D2 = (I2 + E + E.T)/(h**2)
I = np.eye(N)
TFinal = 1
NumOfTimeSteps = int(TFinal/k)
for i in range(NumOfTimeSteps):
# Solve the System: (I - k/2*D2) u_new = (I + k/2*D2)*u_old
A = (I - k/2*D2)
b = np.dot((I + k/2*D2), u)
u = scipy.linalg.solve(A, b)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们看一个简单的例子。我们假设
N = 3
,即三个内部点,但我们首先还将边界点包含在描述近似二阶导数的矩阵D2
中:第一行表示近似
x_1
处的二阶导数为1/h^2 * (u_0 - 2*u_1 + u_2)
。不过,我们知道u_0 = 0
,由于齐次狄利克雷边界条件,因此我们可以简单地从方程中省略它,并且 e 得到与矩阵相同的结果由于
u_0
和u_{n+1}
不是真正的未知数——它们已知为零——我们可以将它们完全从矩阵中删除,然后我们得到矩阵中缺失的条目确实对应于边界条件为零的事实。
Let's have a look at a simple example. We assume
N = 3
, i.e. three inner points, but we will first also include the boundary points in the matrixD2
describing the approximate second derivatives:The first line means the approximate second derivative at
x_1
is1/h^2 * (u_0 - 2*u_1 + u_2)
. We know thatu_0 = 0
though, due to the homgeneous Dirichlet boundary conditions, so we can simply omit it from the equation, and e get the same result for the matrixSince
u_0
andu_{n+1}
are not real unknowns -- they are known to be zero -- we can completely drop them from the matrix, and we getThe missing entries in the matrix really correspond to the fact that the boundary conditions are zero.
我不确定你被告知了什么,但这是我对这个问题的了解。
狄利克雷边界条件确定了电势值(本例中为温度)。诺伊曼边界条件将指定点处的通量或一阶导数。如果表面有对流边界条件,则需要此功能。
对于处理狄利克雷边界条件,您可以在不首先考虑边界条件的情况下制定系统矩阵。如果给定节点有固定温度,可以这样处理:
I'm not sure what you've been told, but here's what I know about this problem.
Dirichlet boundary conditions fix the value of the potential (temperature in this case). A Neumann boundary condition will specify flux or first derivative at a point. You'll need this if you have convection boundary conditions at a surface.
As for treating Dirichlet boundary conditions, you formulate the system matrix without considering boundary conditions first. If you have a fixed temperature at a given node, you can handle it this way: