边界条件在热方程和Crank-Nicholson有限差分解中的应用

发布于 2024-10-14 21:04:59 字数 874 浏览 11 评论 0原文

下面的代码求解一维热方程,该方程表示一根杆,其两端保持在零温度,初始条件为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Bonjour°[大白 2024-10-21 21:04:59

让我们看一个简单的例子。我们假设N = 3,即三个内部点,但我们首先还将边界点包含在描述近似二阶导数的矩阵D2中:

      1  /  1 -2  1  0  0 \
D2 = --- |  0  1 -2  1  0 |
     h^2 \  0  0  1 -2  1 /

第一行表示近似x_1 处的二阶导数为 1/h^2 * (u_0 - 2*u_1 + u_2)。不过,我们知道 u_0 = 0,由于齐次狄利克雷边界条件,因此我们可以简单地从方程中省略它,并且 e 得到与矩阵相同的结果

      1  /  0 -2  1  0  0 \
D2 = --- |  0  1 -2  1  0 |
     h^2 \  0  0  1 -2  0 /

由于 u_0u_{n+1} 不是真正的未知数——它们已知为零——我们可以将它们完全从矩阵中删除,然后我们得到

      1  /  2  1  0 \
D2 = --- |  1 -2  1 |
     h^2 \  0  1 -2 /

矩阵中缺失的条目确实对应于边界条件为零的事实。

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 matrix D2 describing the approximate second derivatives:

      1  /  1 -2  1  0  0 \
D2 = --- |  0  1 -2  1  0 |
     h^2 \  0  0  1 -2  1 /

The first line means the approximate second derivative at x_1 is 1/h^2 * (u_0 - 2*u_1 + u_2). We know that u_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 matrix

      1  /  0 -2  1  0  0 \
D2 = --- |  0  1 -2  1  0 |
     h^2 \  0  0  1 -2  0 /

Since u_0 and u_{n+1} are not real unknowns -- they are known to be zero -- we can completely drop them from the matrix, and we get

      1  /  2  1  0 \
D2 = --- |  1 -2  1 |
     h^2 \  0  1 -2 /

The missing entries in the matrix really correspond to the fact that the boundary conditions are zero.

灼疼热情 2024-10-21 21:04:59

有人告诉我上排、下排
矩阵 A 包含两个非零
元素,以及缺失的第三个
元素(即零)是
狄利克雷条件。

我不确定你被告知了什么,但这是我对这个问题的了解。

狄利克雷边界条件确定了电势值(本例中为温度)。诺伊曼边界条件将指定点处的通量或一阶导数。如果表面有对流边界条件,则需要此功能。

对于处理狄利克雷边界条件,您可以在不首先考虑边界条件的情况下制定系统矩阵。如果给定节点有固定温度,可以这样处理:

  1. 将给定节点右侧向量中的行设置为等于边界温度。将左侧矩阵中该行的所有列清零,并将与该节点对应的对角元素设置为等于 1。

I was told the upper, lower rows of
matrix A contain two non-zero
elements, and the missing third
element (that is zero) is the
Dirichlet condition.

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:

  1. Set the row in the right hand side vector for the given node equal to the boundary temperature. Zero out all columns of that row in the left hand side matrix and set the diagonal element that corresponds to that node equal to one.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文