一维热传到方程求数值解
本文主要利用泰勒展开将方程中的一阶还有二阶偏导数进行离散化,推导出一种可以用程序求解的形式
求解原理
一维热传导方程
{
∂
u
∂
x
(
x
,
t
)
=
a
2
∂
2
u
∂
x
2
u
(
x
,
t
)
+
f
(
x
,
t
)
u
(
x
,
0
)
=
φ
(
x
)
u
(
a
,
t
)
=
γ
1
(
t
)
u
(
b
,
t
)
=
γ
2
(
t
)
\begin{align} \begin{cases} \frac{\partial u}{\partial x} \left ( x,t \right ) &=a^{2} \frac{\partial^{2}u}{\partial x^2}u(x,t)+f(x,t)\\ u(x,0)&=\varphi({x})\\ u(a,t)&=\gamma_{1}(t)\\ u(b,t)&=\gamma_2(t) \end{cases} \end{align}
⎩
⎨
⎧∂x∂u(x,t)u(x,0)u(a,t)u(b,t)=a2∂x2∂2uu(x,t)+f(x,t)=φ(x)=γ1(t)=γ2(t)
由于热传导方程较为复杂,只能将方程中的一阶和二阶偏导进行离散化。和欧拉法采用相同的思路,下面进行推导:
-
将 x x x与 t t t分别在横坐标与纵坐标上进行划分
x x x步长: Δ x = b − a N \Delta{x}= \frac{b-a}{N} Δx=Nb−a,得到关于 x j x_j xj与 t n t_n tn的表达式:
x j = a + ( j − 1 ) Δ x t n = 0 + ( n − 1 ) Δ t \begin{aligned} x_j &= a + (j-1)\Delta{x} \\ t_n &= 0 + (n-1)\Delta{t} \\ \end{aligned} xjtn=a+(j−1)Δx=0+(n−1)Δt
将函数进行近似替换 u j n ≈ u ( x j , t n ) u_j^n\approx u(x_j,t_n) ujn≈u(xj,tn) -
根据泰勒展开将公式进行代换
对于任意一个 x j x_j xj对 t t t进行展开:
u ( x j , t n + Δ t ) = u ( x j , t n ) + ∂ u ∂ t ( x j , t n ) Δ t + ⋅ ⋅ ⋅ u(x_j,t_n+\Delta{t})=u(x_j,t_n)+\frac{\partial u}{\partial t} (x_j,t_n)\Delta{t}+··· u(xj,tn+Δt)=u(xj,tn)+∂t∂u(xj,tn)Δt+⋅⋅⋅
由于很难求出函数的偏导,所以需要将其所有偏导形式转换成容易求解出来的离散形式首先用一维热传导方程进行替换∂ u ∂ t ( x j , t n ) = a 2 ∂ 2 u ∂ x 2 ( x j , t n ) + f ( x j , t n ) \frac{\partial u}{\partial t} (x_j,t_n) = a^2 \frac{\partial^{2}u}{\partial x^2}(x_j,t_n)+f(x_j,t_n) ∂t∂u(xj,tn)=a2∂x2∂2u(xj,tn)+f(xj,tn)
利用上式联立下面两个式子u ( x j + Δ x , t n ) = u ( x j , t n ) + ∂ u ∂ x ( x j , t n ) Δ x + 1 / 2 ∂ 2 u ∂ x 2 ( x j , t n ) Δ x 2 + ⋅ ⋅ ⋅ u ( x j − Δ x , t n ) = u ( x j , t n ) − ∂ u ∂ x ( x j , t n ) Δ x + 1 / 2 ∂ 2 u ∂ x 2 ( x j , t n ) Δ x 2 − ⋅ ⋅ ⋅ ∂ 2 u ∂ x 2 ( x j , t n ) ≈ u j + 1 n + u j − 1 n − 2 u j n Δ x 2 \begin{aligned} u(x_j+\Delta{x},t_n) &= u(x_j,t_n)+\frac{\partial u}{\partial x} (x_j,t_n)\Delta{x}+1/2\frac{\partial^{2}u}{\partial x^2}(x_j,t_n)\Delta{x}^2+···\\ u(x_j-\Delta{x},t_n) &= u(x_j,t_n)-\frac{\partial u}{\partial x} (x_j,t_n)\Delta{x}+1/2\frac{\partial^{2}u}{\partial x^2}(x_j,t_n)\Delta{x}^2-··· \\ \frac{\partial^{2}u}{\partial x^2}(x_j,t_n) &\approx \frac{u_{j+1}^n+u_{j-1}^n-2u_j^n}{\Delta{x}^2} \end{aligned} u(xj+Δx,tn)u(xj−Δx,tn)∂x2∂2u(xj,tn)=u(xj,tn)+∂x∂u(xj,tn)Δx+1/2∂x2∂2u(xj,tn)Δx2+⋅⋅⋅=u(xj,tn)−∂x∂u(xj,tn)Δx+1/2∂x2∂2u(xj,tn)Δx2−⋅⋅⋅≈Δx2uj+1n+uj−1n−2ujn
最后得到递推关系式
u j n + 1 = u j n + [ a 2 u j + 1 n + u j − 1 n − 2 u j n Δ x 2 + f j n ] Δ t u_j^{n+1}=u_j^n+[a^2\frac{u_{j+1}^n+u_{j-1}^n-2u_j^n}{\Delta{x}^2}+f_j^n]\Delta{t} ujn+1=ujn+[a2Δx2uj+1n+uj−1n−2ujn+fjn]Δt
化成易于用程序求解的形式
-
在时间维度上进行递推
首先设置两个时间向量,将所有的位置包括其中
u n = ( u 1 n ⋮ ⋮ u N + 1 n ) u n + 1 = ( u 1 n + 1 ⋮ ⋮ u N + 1 n + 1 ) u^n= \begin{pmatrix} u_1^n\\ \vdots\\ \vdots\\ u_{N+1}^n \end{pmatrix} \qquad u^{n+1}= \begin{pmatrix} u_1^{n+1}\\ \vdots\\ \vdots\\ u_{N+1}^{n+1} \end{pmatrix} un=⎝ ⎛u1n⋮⋮uN+1n⎠ ⎞un+1=⎝ ⎛u1n+1⋮⋮uN+1n+1⎠ ⎞
-
建立系数矩阵
( ϕ 第一取值 ⋮ 第 N 取值 ϕ ) = ( − 2 1 0 0 ⋅ ⋅ ⋅ 1 − 2 1 0 ⋅ ⋅ ⋅ ⋮ ⋅ ⋅ ⋅ 0 1 − 2 1 ⋅ ⋅ ⋅ 0 0 1 − 2 ) ( u 1 n u 2 n ⋮ ⋮ u N + 1 n ) \begin{pmatrix} \phi \\ 第一取值 \\ \vdots \\ 第N取值 \\ \phi \end{pmatrix}= \begin{pmatrix}-2\quad1\quad0\quad0\quad··· \\1\quad-2\quad1\quad0\quad··· \\\vdots \\···\quad0\quad1\quad-2\quad1 \\···\quad0\quad0\quad1\quad-2 \end{pmatrix} \begin{pmatrix}u_1^n \\u_2^n \\\vdots \\\vdots \\u_{N+1}^n \end{pmatrix} ⎝ ⎛ϕ第一取值⋮第N取值ϕ⎠ ⎞=⎝ ⎛−2100⋅⋅⋅1−210⋅⋅⋅⋮⋅⋅⋅01−21⋅⋅⋅001−2⎠ ⎞⎝ ⎛u1nu2n⋮⋮uN+1n⎠ ⎞
为何矩阵要这么建立,系数矩阵 A A A的第二行为例,与右边的列向量相乘得到结果 u 1 n − 2 u 2 n + u 3 n u_1^n - 2u_2^n + u_3^n u1n−2u2n+u3n 将结果表示成以下列向量。由于初值和末值原方程会给出,所以开始两个值可能不正确,所以先不用管。
( ϕ u 1 n − 2 u 2 n + u 3 n u 2 n − 2 u 3 n + u 4 n ⋮ u N − 1 n − 2 u N n + u N + 1 n ϕ ) \begin{pmatrix} \phi \\ u_1^n - 2u_2^n + u_3^n \\ u_2^n - 2u_3^n + u_4^n \\ \vdots \\ u_{N - 1}^n - 2u_N^n + u_{N + 1}^n \\ \phi \\ \end{pmatrix} ⎝ ⎛ϕu1n−2u2n+u3nu2n−2u3n+u4n⋮uN−1n−2uNn+uN+1nϕ⎠ ⎞ -
扩散源或热源表示
f n = ( f 1 n ⋮ ⋮ f N + 1 n ) f^n= \begin{pmatrix} f_1^n\\ \vdots\\ \vdots\\ f_{N+1}^n \end{pmatrix} fn=⎝ ⎛f1n⋮⋮fN+1n⎠ ⎞ -
得到最终表达式
u n + 1 = u n + ( a 2 1 Δ x 2 A u n + f n ) Δ t u^{n+1}=u^n+(a^2\frac{1}{\Delta{x}^{2}}Au^n+f^n)\Delta{t}\\ un+1=un+(a2Δx21Aun+fn)Δt以下为初始条件:
u 1 n + 1 = γ 1 n + 1 = γ ( t n + 1 ) u N + 1 n + 1 = γ 2 n + 1 u 1 = γ γ j = γ ( x j ) \begin{aligned} u_1^{n+1} &= \gamma_1^{n+1} = \gamma(t_{n+1}) \\ u_{N+1}^{n+1} &= \gamma_2^{n+1} \\ u^1 &= \gamma \\ \gamma_j &= \gamma(x_j) \\ \end{aligned} u1n+1uN+1n+1u1γj=γ1n+1=γ(tn+1)=γ2n+1=γ=γ(xj)
通过程序可以设定步长 Δ x \Delta x Δx 和 Δ t \Delta t Δt 其它量就可以通过边界条件推出。
示例与代码
设方程如下:
∂
u
∂
x
(
x
,
t
)
=
a
2
∂
2
u
∂
x
2
u
(
x
,
t
)
+
f
(
x
,
t
)
u
(
x
,
0
)
=
0
u
(
a
,
t
)
=
0
+
0.0
×
s
i
n
(
t
)
u
(
b
,
t
)
=
0
−
0.0
×
s
i
n
(
t
)
\begin{align} \frac{\partial u}{\partial x} \left ( x,t \right ) &=a^{2} \frac{\partial^{2}u}{\partial x^2}u(x,t)+f(x,t)\\ u(x,0) &= 0\\ u(a,t) &= 0 + 0.0 \times sin(t)\\ u(b,t) &= 0 - 0.0 \times sin(t)\\ \end{align}
∂x∂u(x,t)u(x,0)u(a,t)u(b,t)=a2∂x2∂2uu(x,t)+f(x,t)=0=0+0.0×sin(t)=0−0.0×sin(t)
求解
m
a
t
l
a
b
matlab
matlab代码如下:
clc; clear
a = 1;
dx = 0.01;
x = 0 : dx : 1;
dt = 0.00005;
t = 0 : dt : 1;
u = zeros(length(x), length(t));
u(:,1) = 0;
f = 5 * exp(-20 * (x - 1 / 2) .^ 2);
m1 = 0 + 0.0 * sin(t);
m2 = 0 - 0.0 * sin(t);
A = -2 * eye(length(x)) + diag(ones(1,length(x)-1),1) + diag(ones(1,length(x)-1),-1)
for n = 1 : length(t)-1
u(:, n+1) = u(:,n) + (a^2 / dx^2 * A * u(:,n) + f') * dt;
u(1, n+1) = m1(n+1);
u(end, n+1) = m2(n+1);
plot(x, u(:,end))
axis([x(1) x(end) 0 1])
getframe;
end