二阶椭圆型第一边值问题的数值解法(五点差分格式和有限体积法)附matlab代码
这里我们介绍五点差分格式和有限体积法求椭圆型第一边值问题,
题目:
分别采用矩形网格的五点差分格式和有限体积法求椭圆型第一边值问题,
−
∂
2
u
∂
x
2
−
∂
2
u
∂
y
2
+
a
∂
u
∂
x
=
0
,
a
>
0
,
-\frac{\partial^2u}{\partial x^2}-\frac{\partial^2u}{\partial y^2}+a\frac{\partial u}{\partial x}=0,a>0,
−∂x2∂2u−∂y2∂2u+a∂x∂u=0,a>0,
u
(
0
,
y
)
=
s
i
n
(
π
y
)
,
u
(
1
,
y
)
=
0
,
0
<
y
<
1
,
u\left(0,y\right)=sin\left(\pi y\right),u\left(1,y\right)=0,0<y<1,
u(0,y)=sin(πy),u(1,y)=0,0<y<1,
u
(
x
,
0
)
=
u
(
x
,
1
)
=
0
,
0
<
x
<
1.
u\left(x,0\right)=u\left(x,1\right)=0,0<x<1.
u(x,0)=u(x,1)=0,0<x<1.
取参数 a = 40,得到不同网格最大误差和收敛阶。
问题分析
(i) 矩形网格的五点差分法:
Step1:网格剖分。取定沿 x,y 轴方向的步长分别为:
h
x
,
h
y
h_x,h_y
hx,hy,便于计算我们取
h
x
=
h
y
h_x=h_y
hx=hy,令
h
=
h
x
2
+
h
y
2
h =\sqrt{h_x^2+h_y^2}
h=hx2+hy2,作两族平行于 x,y 轴的直线
x
=
x
i
,
y
=
y
i
x=x_i,y=y_i
x=xi,y=yi,其中
x
i
=
x
0
+
i
h
x
,
i
=
0
,
1
,
2
,
⋯
,
N
,
y
j
=
y
0
+
j
h
y
,
j
=
0
,
1
,
2
,
⋯
,
N
x_i=x_0+ih_x,i=0,1,2,\cdots,N,y_j=y_0+jh_y,j=0,1,2,\cdots,N
xi=x0+ihx,i=0,1,2,⋯,N,yj=y0+jhy,j=0,1,2,⋯,N,两族直线的交点
(
i
h
x
,
j
h
y
)
(ih_x,jh_y)
(ihx,jhy)称为网点或者节点,记为
(
x
i
,
y
j
)
(x_i , y_j)
(xi,yj) .
Step2:微分算子离散.
在正则节点
(
x
i
,
y
j
)
(x_i , y_j)
(xi,yj)上沿 x,y 方向分别使用中心差商公式对
u
x
x
u_{xx}
uxx和
u
y
y
u_{yy}
uyy 作近似,得到方程的五点差分格式为:
−
(
u
i
+
1
,
j
−
2
u
i
,
j
+
u
i
−
1
,
j
h
x
2
+
u
i
,
j
+
1
−
2
u
i
,
j
+
u
i
,
j
−
1
h
y
2
)
+
a
u
i
+
1
,
j
−
u
i
−
1
,
j
2
h
x
=
0
-\left(\frac{u_{i+1,j}-2u_{i,j}+u_{i-1,j}}{h_x^2}+\frac{u_{i,j+1}-2u_{i,j}+u_{i,j-1}}{h_y^2}\right)+a\frac{u_{i+1,j}-u_{i-1,j}}{2h_x}=0
−(hx2ui+1,j−2ui,j+ui−1,j+hy2ui,j+1−2ui,j+ui,j−1)+a2hxui+1,j−ui−1,j=0
⇒
\Rightarrow
⇒
(
−
1
h
x
2
−
a
2
h
x
)
u
i
−
1
,
j
+
(
2
h
x
2
+
2
h
y
2
)
u
i
,
j
+
(
−
1
h
x
2
+
a
2
h
x
)
u
i
+
1
,
j
+
(
−
1
h
y
2
)
u
i
,
j
−
1
+
(
−
1
h
y
2
)
u
i
,
j
+
1
=
0
,
i
,
j
=
1
,
2
,
⋯
,
N
−
1
\left(-\frac{1}{h_x^2}-\frac{a}{2h_x}\right)u_{i-1,j}+\left(\frac{2}{h_x^2}+\frac{2}{h_y^2}\right)u_{i,j}+\left(-\frac{1}{h_x^2}+\frac{a}{2h_x}\right)u_{i+1,j}+\left(-\frac{1}{h_y^2}\right)u_{i,j-1}+\left(-\frac{1}{h_y^2}\right)u_{i,j+1}=0,i,j=1,2,\cdots,N-1
(−hx21−2hxa)ui−1,j+(hx22+hy22)ui,j+(−hx21+2hxa)ui+1,j+(−hy21)ui,j−1+(−hy21)ui,j+1=0,i,j=1,2,⋯,N−1
Let
A
=
[
2
h
x
2
+
2
h
y
2
−
1
h
x
2
+
a
2
h
x
0
⋯
0
0
−
1
h
x
2
−
a
2
h
x
2
h
x
2
+
2
h
y
2
−
1
h
x
2
+
a
2
h
x
⋯
0
0
⋯
⋯
⋯
⋯
⋯
⋯
0
0
0
⋯
−
1
h
x
2
−
a
2
h
x
2
h
x
2
+
2
h
y
2
]
A=\left[\begin{matrix}\frac{2}{h_x^2}+\frac{2}{h_y^2}&-\frac{1}{h_x^2}+\frac{a}{2h_x}&0&\cdots&0&0\\-\frac{1}{h_x^2}-\frac{a}{2h_x}&\frac{2}{h_x^2}+\frac{2}{h_y^2}&-\frac{1}{h_x^2}+\frac{a}{2h_x}&\cdots&0&0\\\cdots&\cdots&\cdots&\cdots&\cdots&\cdots\\0&0&0&\cdots&-\frac{1}{h_x^2}-\frac{a}{2h_x}&\frac{2}{h_x^2}+\frac{2}{h_y^2}\\\end{matrix}\right]
A=
hx22+hy22−hx21−2hxa⋯0−hx21+2hxahx22+hy22⋯00−hx21+2hxa⋯0⋯⋯⋯⋯00⋯−hx21−2hxa00⋯hx22+hy22
为一个
(
N
−
1
)
×
(
N
−
1
)
\left(N-1\right)\times\left(N-1\right)
(N−1)×(N−1)矩阵,
B
=
[
−
1
h
y
2
0
⋯
0
0
−
1
h
y
2
⋯
0
⋯
⋯
⋯
⋯
0
0
⋯
−
1
h
y
2
]
是一个
(
N
−
1
)
×
(
N
−
1
)
B=\left[\begin{matrix}-\frac{1}{h_y^2}&0&\cdots&0\\0&-\frac{1}{h_y^2}&\cdots&0\\\cdots&\cdots&\cdots&\cdots\\0&0&\cdots&-\frac{1}{h_y^2}\\\end{matrix}\right]是一个\left(N-1\right)\times\left(N-1\right)
B=
−hy210⋯00−hy21⋯0⋯⋯⋯⋯00⋯−hy21
是一个(N−1)×(N−1)矩阵,
K
=
[
A
B
0
⋯
0
0
0
B
A
B
⋯
0
0
0
⋯
⋯
⋯
⋯
⋯
⋯
⋯
0
0
0
⋯
0
B
A
]
,
K=\left[\begin{matrix}A&B&0&\cdots&0&0&0\\\ B&A&B&\cdots&0&0&0\\\cdots&\cdots&\cdots&\cdots&\cdots&\cdots&\cdots\\0&0&0&\cdots&0&B&A\\\end{matrix}\right],
K=
A B⋯0BA⋯00B⋯0⋯⋯⋯⋯00⋯000⋯B00⋯A
,是一个
(
N
−
1
)
2
×
(
N
−
1
)
2
\left(N-1\right)^2\times\left(N-1\right)^2
(N−1)2×(N−1)2矩阵,
b
1
=
(
(
1
h
x
2
+
a
2
h
x
)
u
0
,
1
+
1
h
y
2
u
1
,
0
,
1
h
y
2
u
2
,
0
,
⋯
,
1
h
y
2
u
N
−
2
,
0
,
1
h
y
2
u
N
−
1
,
0
+
(
1
h
x
2
−
a
2
h
x
)
u
N
,
1
)
T
b_1=\left(\left(\frac{1}{h_x^2}+\frac{a}{2h_x}\right)u_{0,1}+{\frac{1}{h_y^2}u}_{1,0},{\frac{1}{h_y^2}u}_{2,0},\cdots,{\frac{1}{h_y^2}u}_{N-2,0},{\frac{1}{h_y^2}u}_{N-1,0}+\left(\frac{1}{h_x^2}-\frac{a}{2h_x}\right)u_{N,1}\right)^T
b1=((hx21+2hxa)u0,1+hy21u1,0,hy21u2,0,⋯,hy21uN−2,0,hy21uN−1,0+(hx21−2hxa)uN,1)T,
b
i
=
(
(
1
h
x
2
+
a
2
h
x
)
u
0
,
i
,
0
,
⋯
,
0
,
(
1
h
x
2
−
a
2
h
x
)
u
N
,
i
)
T
,
i
=
2
,
3
,
⋯
,
N
−
2
,
b
N
−
1
=
(
(
1
h
x
2
+
a
2
h
x
)
u
0
,
N
−
1
+
1
h
y
2
u
1
,
N
,
1
h
y
2
u
2
,
N
,
⋯
,
1
h
y
2
u
N
−
2
,
N
,
1
h
y
2
u
N
−
1
,
N
+
(
1
h
x
2
−
a
2
h
x
)
u
N
,
N
−
1
)
T
b_i=\left(\left(\frac{1}{h_x^2}+\frac{a}{2h_x}\right)u_{0,i},0,\cdots,0,\left(\frac{1}{h_x^2}-\frac{a}{2h_x}\right)u_{N,i}\right)^T,i=2,3,\cdots,N-2, b_{N-1}=\left(\left(\frac{1}{h_x^2}+\frac{a}{2h_x}\right)u_{0,N-1}+{\frac{1}{h_y^2}u}_{1,N}\ ,{\ \frac{1}{\ h_y^2}u}_{2,N}\ ,\ \cdots,\ {\frac{1}{h_y^2}u}_{N-2,N},{\frac{1}{h_y^2}u}_{N-1,N}+\left(\frac{1}{h_x^2}-\frac{a}{2h_x}\right)u_{N,N-1}\right)^T
bi=((hx21+2hxa)u0,i,0,⋯,0,(hx21−2hxa)uN,i)T,i=2,3,⋯,N−2,bN−1=((hx21+2hxa)u0,N−1+hy21u1,N , hy21u2,N , ⋯, hy21uN−2,N,hy21uN−1,N+(hx21−2hxa)uN,N−1)T,
b
=
(
b
1
,
b
2
,
⋯
,
b
N
−
1
)
T
,
U
=
(
u
1
,
1
,
u
2
,
1
,
⋯
u
N
−
1
,
1
,
u
1
,
2
,
u
2
,
2
,
⋯
,
u
N
−
1
,
2
,
⋯
u
1
,
N
−
1
,
u
2
,
N
−
1
,
⋯
u
N
−
1
,
N
−
1
)
T
b=\left(b_1,b_2,\cdots,b_{N-1}\right)^T, U =\left(u_{1,1},u_{2,1},\cdots u_{N-1,1,}u_{1,2},{u_{2,2},\cdots,u_{N-1,2},\cdots u_{1,N-1},u_{2,N-1},\cdots u_{N-1,N-1}}\right)^T
b=(b1,b2,⋯,bN−1)T,U=(u1,1,u2,1,⋯uN−1,1,u1,2,u2,2,⋯,uN−1,2,⋯u1,N−1,u2,N−1,⋯uN−1,N−1)T.
因此,KU=b,解出U即可。
在本题中,因为
u
(
0
,
y
)
=
s
i
n
(
π
y
)
,
u
(
1
,
y
)
=
0
,
0
<
y
<
1
,
u
(
x
,
0
)
=
u
(
x
,
1
)
=
0
,
0
<
x
<
1
u\left(0,y\right)=sin\left(\pi y\right),u\left(1,y\right)=0,0<y<1,\ u\left(x,0\right)=u\left(x,1\right)=0,0<x<1
u(0,y)=sin(πy),u(1,y)=0,0<y<1, u(x,0)=u(x,1)=0,0<x<1.
所以
b
1
=
(
(
1
h
x
2
+
a
2
h
x
)
s
i
n
(
π
y
1
)
,
0
,
⋯
,
0
,
0
)
T
,
b
i
=
(
(
1
h
x
2
+
a
2
h
x
)
s
i
n
(
π
y
i
)
,
0
,
⋯
,
0
,
0
)
T
,
i
=
2
,
3
,
⋯
,
N
−
2
,
b
N
−
1
=
(
(
1
h
x
2
+
a
2
h
x
)
s
i
n
(
π
y
N
−
1
)
,
0
,
⋯
,
0
,
0
)
T
b_1=\left(\left(\frac{1}{h_x^2}+\frac{a}{2h_x}\right)sin\left(\pi y_1\right),0,\cdots,0,0\right)^T, b_i=\left(\left(\frac{1}{h_x^2}+\frac{a}{2h_x}\right)sin\left(\pi y_i\right),0,\cdots,0,0\right)^T,i=2,3,\cdots,N-2, b_{N-1}=\left(\left(\frac{1}{h_x^2}+\frac{a}{2h_x}\right)sin\left(\pi y_{N-1}\right)\ ,0\ ,\ \cdots,0,0\right)^T
b1=((hx21+2hxa)sin(πy1),0,⋯,0,0)T,bi=((hx21+2hxa)sin(πyi),0,⋯,0,0)T,i=2,3,⋯,N−2,bN−1=((hx21+2hxa)sin(πyN−1) ,0 , ⋯,0,0)T.
(ii) 有限体积法
我们需要作对偶剖分。记
x
i
−
1
2
=
(
i
−
1
2
)
h
x
,
y
j
−
1
2
=
(
j
−
1
2
)
h
y
x_{i-\frac{1}{2}}=\left(i-\frac{1}{2}\right)h_x,y_{j-\frac{1}{2}}=\left(j-\frac{1}{2}\right)h_y
xi−21=(i−21)hx,yj−21=(j−21)hy
对于任一正则内点
(
x
i
,
y
j
)
(x_i , y_j)
(xi,yj) . 考虑对偶剖分的网点:
A
(
x
i
−
1
2
,
y
j
−
1
2
)
,
B
(
x
i
+
1
2
,
y
j
−
1
2
)
,
C
(
x
i
+
1
2
,
y
j
+
1
2
)
,
D
(
x
i
−
1
2
,
y
j
+
1
2
)
A(x_{i-\frac{1}{2}},y_{j-\frac{1}{2}}),B(x_{i+\frac{1}{2}},y_{j-\frac{1}{2}}),C(x_{i+\frac{1}{2}},y_{j+\frac{1}{2}}),D(x_{i-\frac{1}{2}},y_{j+\frac{1}{2}})
A(xi−21,yj−21),B(xi+21,yj−21),C(xi+21,yj+21),D(xi−21,yj+21),用
G
i
j
G_{ij}
Gij表示以A,B,C,D为顶点的矩形域,
∂
G
i
j
\partial G_{ij}
∂Gij为其边界,利用格林公式,我们有
−
∫
∂
G
i
j
∂
u
∂
n
+
∫
∫
G
i
j
a
∂
u
∂
x
d
x
d
y
=
0
-\int_{\partial G_{ij}}\frac{\partial u}{\partial n}+\int\int_{G_{ij}}{a\frac{\partial u}{\partial x}dxdy}=0
−∫∂Gij∂n∂u+∫∫Gija∂x∂udxdy=0
⇒
\Rightarrow
⇒
u
i
,
j
−
u
i
,
j
−
1
h
y
h
x
+
u
i
,
j
−
u
i
+
1
,
j
h
x
h
y
+
u
i
,
j
−
u
i
,
j
+
1
h
y
h
x
+
u
i
,
j
−
u
i
−
1
,
j
h
x
h
y
+
a
2
(
u
i
+
1
,
j
−
u
i
−
1
,
j
)
h
y
=
0
\frac{u_{i,j}{-u}_{i,j-1}}{h_y}h_x+\frac{u_{i,j}-u_{i+1,j}}{h_x}h_y+\frac{u_{i,j}{-u}_{i,j+1}}{h_y}h_x+\frac{u_{i,j}{-u}_{i-1,j}}{h_x}h_y+\frac{a}{2}{(u}_{i+1,j}-u_{i-1,j})h_y=0
hyui,j−ui,j−1hx+hxui,j−ui+1,jhy+hyui,j−ui,j+1hx+hxui,j−ui−1,jhy+2a(ui+1,j−ui−1,j)hy=0
⇒
\Rightarrow
⇒
(
−
h
y
h
x
−
a
2
h
y
)
u
i
−
1
,
j
+
(
2
h
x
h
y
+
2
h
y
h
x
)
u
i
,
j
+
(
−
h
y
h
x
+
a
2
h
y
)
u
i
+
1
,
j
−
h
x
h
y
u
i
,
j
−
1
−
h
x
h
y
u
i
,
j
+
1
=
0
(-{\frac{h_y}{h_x}-\frac{a}{2}h_y)u}_{i-1,j}+{\left(\frac{2h_x}{h_y}+\frac{2h_y}{h_x}\right)u}_{i,j}+(-{\frac{h_y}{h_x}+\frac{a}{2}h_y)u}_{i+1,j}-\frac{h_x}{h_y}u_{i,j-1}-\frac{h_x}{h_y}u_{i,j+1}=0
(−hxhy−2ahy)ui−1,j+(hy2hx+hx2hy)ui,j+(−hxhy+2ahy)ui+1,j−hyhxui,j−1−hyhxui,j+1=0
Let
A
=
[
2
h
x
h
y
+
2
h
y
h
x
−
h
y
h
x
+
a
2
h
y
0
⋯
0
0
−
h
y
h
x
−
a
2
h
y
2
h
x
h
y
+
2
h
y
h
x
−
h
y
h
x
+
a
2
h
y
⋯
0
0
⋯
⋯
⋯
⋯
⋯
⋯
0
0
0
⋯
−
h
y
h
x
−
a
2
h
y
2
h
x
h
y
+
2
h
y
h
x
]
A=\left[\begin{matrix}\frac{2h_x}{h_y}+\frac{2h_y}{h_x}&-\frac{h_y}{h_x}+\frac{a}{2}h_y&0&\cdots&0&0\\-\frac{h_y}{h_x}-\frac{a}{2}h_y&\frac{2h_x}{h_y}+\frac{2h_y}{h_x}&-\frac{h_y}{h_x}+\frac{a}{2}h_y&\cdots&0&0\\\cdots&\cdots&\cdots&\cdots&\cdots&\cdots\\0&0&0&\cdots&-\frac{h_y}{h_x}-\frac{a}{2}h_y&\frac{2h_x}{h_y}+\frac{2h_y}{h_x}\\\end{matrix}\right]
A=
hy2hx+hx2hy−hxhy−2ahy⋯0−hxhy+2ahyhy2hx+hx2hy⋯00−hxhy+2ahy⋯0⋯⋯⋯⋯00⋯−hxhy−2ahy00⋯hy2hx+hx2hy
为一个
(
N
−
1
)
×
(
N
−
1
)
\left(N-1\right)\times\left(N-1\right)
(N−1)×(N−1)矩阵,
B
=
[
−
h
x
h
y
0
⋯
0
0
−
h
x
h
y
⋯
0
⋯
⋯
⋯
⋯
0
0
⋯
−
h
x
h
y
]
为一个
(
N
−
1
)
×
(
N
−
1
)
B=\left[\begin{matrix}-\frac{h_x}{h_y}&0&\cdots&0\\0&-\frac{h_x}{h_y}&\cdots&0\\\cdots&\cdots&\cdots&\cdots\\0&0&\cdots&-\frac{h_x}{h_y}\\\end{matrix}\right] 为一个\left(N-1\right)\times\left(N-1\right)
B=
−hyhx0⋯00−hyhx⋯0⋯⋯⋯⋯00⋯−hyhx
为一个(N−1)×(N−1)矩阵,
K
=
[
A
B
0
⋯
0
0
0
B
A
B
⋯
0
0
0
⋯
⋯
⋯
⋯
⋯
⋯
⋯
0
0
0
⋯
0
B
A
]
,
K=\left[\begin{matrix}A&B&0&\cdots&0&0&0\\\ B&A&B&\cdots&0&0&0\\\cdots&\cdots&\cdots&\cdots&\cdots&\cdots&\cdots\\0&0&0&\cdots&0&B&A\\\end{matrix}\right],
K=
A B⋯0BA⋯00B⋯0⋯⋯⋯⋯00⋯000⋯B00⋯A
, 是一个
(
N
−
1
)
2
×
(
N
−
1
)
2
\left(N-1\right)^2\times\left(N-1\right)^2
(N−1)2×(N−1)2矩阵,
b
1
=
(
(
a
2
h
y
+
h
y
h
x
)
u
0
,
1
+
h
x
h
y
u
1
,
0
,
h
x
h
y
u
2
,
0
,
⋯
,
h
x
h
y
u
N
−
2
,
0
,
h
x
h
y
u
N
−
1
,
0
+
(
h
y
h
x
−
a
2
h
y
)
u
N
,
1
)
T
,
b_1=\left((\frac{a}{2}h_y+\frac{h_y}{h_x}{)u}_{0,1}+{\frac{h_x}{h_y}u}_{1,0},\ {\frac{h_x}{h_y}u}_{2,0},\cdots,\ {\frac{h_x}{h_y}u}_{N-2,0},{\ \frac{h_x}{h_y}u}_{N-1,0}+(\frac{h_y}{h_x}-\frac{a}{2}h_y)u_{N,1}\right)^T,
b1=((2ahy+hxhy)u0,1+hyhxu1,0, hyhxu2,0,⋯, hyhxuN−2,0, hyhxuN−1,0+(hxhy−2ahy)uN,1)T,
b
i
=
(
(
a
2
h
y
+
h
y
h
x
)
u
0
,
i
,
0
,
⋯
,
0
,
(
h
y
h
x
−
a
2
h
y
)
u
N
,
i
)
T
,
i
=
2
,
3
,
⋯
,
N
−
2
,
b
N
−
1
=
(
(
a
2
h
y
+
h
y
h
x
)
u
0
,
N
−
1
+
h
x
h
y
u
1
,
N
,
⋯
,
h
x
h
y
u
N
−
2
,
N
,
h
x
h
y
u
N
−
1
,
N
+
(
h
y
h
x
−
a
2
h
y
)
u
N
,
N
−
1
)
T
,
b_i=\left((\frac{a}{2}h_y+\frac{h_y}{h_x}{)u}_{0,i},0,\cdots,0,(\frac{h_y}{h_x}-\frac{a}{2}h_y)u_{N,i}\right)^T,i=2,3,\cdots,N-2, b_{N-1}=\left((\frac{a}{2}h_y+\frac{h_y}{h_x}{)u}_{0,N-1}\ +\frac{h_x}{h_y}\ u_{1,N},\ \cdots,\ {\frac{h_x}{h_y}u}_{N-2,N},{\frac{h_x}{h_y}u}_{N-1,N}+(\frac{h_y}{h_x}-\frac{a}{2}h_y)u_{N,N-1}\right)^T,
bi=((2ahy+hxhy)u0,i,0,⋯,0,(hxhy−2ahy)uN,i)T,i=2,3,⋯,N−2,bN−1=((2ahy+hxhy)u0,N−1 +hyhx u1,N, ⋯, hyhxuN−2,N,hyhxuN−1,N+(hxhy−2ahy)uN,N−1)T,
b
=
(
b
1
,
b
2
,
⋯
,
b
N
−
1
)
T
,
U
=
(
u
1
,
1
,
u
2
,
1
,
⋯
u
N
−
1
,
1
,
u
1
,
2
,
u
2
,
2
,
⋯
,
u
N
−
1
,
2
,
⋯
u
1
,
N
−
1
,
u
2
,
N
−
1
,
⋯
u
N
−
1
,
N
−
1
)
T
.
b=\left(b_1,b_2,\cdots,b_{N-1}\right)^T, U =\left(u_{1,1},u_{2,1},\cdots u_{N-1,1,}u_{1,2},{u_{2,2},\cdots,u_{N-1,2},\cdots u_{1,N-1},u_{2,N-1},\cdots u_{N-1,N-1}}\right)^T.
b=(b1,b2,⋯,bN−1)T,U=(u1,1,u2,1,⋯uN−1,1,u1,2,u2,2,⋯,uN−1,2,⋯u1,N−1,u2,N−1,⋯uN−1,N−1)T.
因此,KU=b,解出U即可。
在本题中,因为
u
(
0
,
y
)
=
s
i
n
(
π
y
)
,
u
(
1
,
y
)
=
0
,
0
<
y
<
1
,
u
(
x
,
0
)
=
u
(
x
,
1
)
=
0
,
0
<
x
<
1.
u\left(0,y\right)=sin\left(\pi y\right),u\left(1,y\right)=0,0<y<1,\ u\left(x,0\right)=u\left(x,1\right)=0,0<x<1.
u(0,y)=sin(πy),u(1,y)=0,0<y<1, u(x,0)=u(x,1)=0,0<x<1.
所以
b
1
=
(
(
a
2
h
y
+
h
y
h
x
)
s
i
n
(
π
y
1
)
,
0
,
⋯
,
0
,
0
)
T
,
b
i
=
(
(
a
2
h
y
+
h
y
h
x
)
s
i
n
(
π
y
i
)
,
0
,
⋯
,
0
,
0
)
T
,
i
=
2
,
3
,
⋯
,
N
−
2
,
b
N
−
1
=
(
(
a
2
h
y
+
h
y
h
x
)
s
i
n
(
π
y
N
−
1
)
,
0
,
⋯
,
0
,
0
)
T
.
b_1=\left(\left(\frac{a}{2}h_y+\frac{h_y}{h_x}\right)sin\left(\pi y_1\right),0,\cdots,0,0\right)^T,\\ b_i=\left(\left(\frac{a}{2}h_y+\frac{h_y}{h_x}\right)sin\left(\pi y_i\right),0,\cdots,0,0\right)^T,i=2,3,\cdots,N-2,\\ b_{N-1}=\left(\left(\frac{a}{2}h_y+\frac{h_y}{h_x}\right)sin\left(\pi y_{N-1}\right)\ ,0\ ,\ \cdots,0,0\right)^T.
b1=((2ahy+hxhy)sin(πy1),0,⋯,0,0)T,bi=((2ahy+hxhy)sin(πyi),0,⋯,0,0)T,i=2,3,⋯,N−2,bN−1=((2ahy+hxhy)sin(πyN−1) ,0 , ⋯,0,0)T.
为了得到两种格式的收敛阶,我们分别取N=8,16,32,64, 求出不同的N对应的最大误差
ϵ
m
a
x
N
=
m
a
x
(
∣
v
i
,
j
−
u
i
,
j
∣
)
,
i
,
j
=
1
,
2
,
⋯
,
N
−
1.
其中,
u
i
,
j
为
(
x
i
,
y
j
)
处的数值解,
v
i
,
j
为
(
x
i
,
y
j
)
处的解析解,由于分割份数
N
的增量均为
2
倍关系,即以
2
为底的
N
次幂增加,由数值分析理论知识可知,收敛阶计算公式为:
s
N
=
l
o
g
2
(
ϵ
m
a
x
N
ϵ
m
a
x
2
N
)
\epsilon_{max}^N=max\left(\left|v_{i,j}-u_{i,j}\right|\right), i,j=1,2,\cdots,N-1.其中,u_{i,j}为(x_i , y_j) 处的数值解,v_{i,j}为(x_i , y_j) 处的解析解,由于分割份数N的增量均为 2 倍关系,即以 2 为底的N次幂增加,由数值分析理论知识可知,收敛阶计算公式为:s_N=log_2\left(\frac{\epsilon_{max}^N}{\epsilon_{max}^{2N}}\right)
ϵmaxN=max(∣vi,j−ui,j∣),i,j=1,2,⋯,N−1.其中,ui,j为(xi,yj)处的数值解,vi,j为(xi,yj)处的解析解,由于分割份数N的增量均为2倍关系,即以2为底的N次幂增加,由数值分析理论知识可知,收敛阶计算公式为:sN=log2(ϵmax2NϵmaxN)
全部代码如下(matlab)
function [E,U,xx,yy]=Fivepoints(N)%五点差分法
fun=inline('sin(pi*y)');
x0=0;
x1=1
y0=0;
y1=1;
h=1/N;
x=zeros(1,N);
x(1)=x0+h;
for i=1:(N-1)
x(i+1)=x(i)+h;
end
y=zeros(1,N);
y(1)=y0+h;
for i=1:(N-1)
y(i+1)=y(i)+h;
end
A=zeros(N-1,N-1);
A(1,1)=4/(h*h);
A(1,2)=-1/(h*h)+20/h;
A(N-1,N-1)=4/(h*h);
A(N-1,N-2)=-1/(h*h)-20/h;
for i=2:(N-2)
A(i,i)=4/(h*h);
A(i,i+1)=-1/(h*h)+20/h;
A(i,i-1)=-1/(h*h)-20/h;
end
B=zeros(N-1,N-1);
for i=1:(N-1)
B(i,i)=-1/(h*h);
end
K=kron(diag(ones(1,N-2),1)+diag(ones(1,N-2),-1),B)+kron(diag(ones(1,N-1)),A);
b=zeros(1,(N-1)*(N-1));
for i=1:(N-1)
b((i-1)*(N-1)+1)=(1/(h*h)+20/h)*feval(fun,y(i));
end
U=K\b';
for i=1:(N-1)
for j=1:(N-1)
yy(1,(i-1)*(N-1)+j)=y(i);
end
end
for i=1:(N-1)
for j=1:(N-1)
xx(1,(i-1)*(N-1)+j)=x(j);
end
end
fun1=inline('sin(pi.*y).*(-exp(20+sqrt(400+pi*pi))/(exp(20-sqrt(400+pi*pi))-exp(20+sqrt(400+pi*pi)))*(exp(20-sqrt(400+pi*pi))).^x+exp(20-sqrt(400+pi*pi))/(exp(20-sqrt(400+pi*pi))-exp(20+sqrt(400+pi)))*(exp(20+sqrt(400+pi))).^x)','x','y');
for i=1:(N-1)*(N-1)
z(i)=feval(fun1,xx(i),yy(i));
e(i)=abs(U(i)-z(i));
end
E=max(e);
function [E,U,xx,yy]=V2(N)%有限体积法
fun=inline('sin(pi*y)');
x0=0;
x1=1
y0=0;
y1=1;
h=1/N;
x=zeros(1,N);
x(1)=x0+h;
for i=1:(N-1)
x(i+1)=x(i)+h;
end
y=zeros(1,N);
y(1)=y0+h;
for i=1:(N-1)
y(i+1)=y(i)+h;
end
A=zeros(N-1,N-1);
A(1,1)=4;
A(1,2)=-1+20*h;
A(N-1,N-1)=4;
A(N-1,N-2)=-1-20*h;
for i=2:(N-2)
A(i,i)=4;
A(i,i+1)=-1+20*h;
A(i,i-1)=-1-20*h;
end
B=zeros(N-1,N-1);
for i=1:(N-1)
B(i,i)=-1;
end
K=kron(diag(ones(1,N-2),1)+diag(ones(1,N-2),-1),B)+kron(diag(ones(1,N-1)),A);
b=zeros(1,(N-1)*(N-1));
for i=1:(N-1)
b((i-1)*(N-1)+1)=(20*h+1)*feval(fun,y(i));
end
U=K\b';
for i=1:(N-1)
for j=1:(N-1)
yy(1,(i-1)*(N-1)+j)=y(i);
end
end
for i=1:(N-1)
for j=1:(N-1)
xx(1,(i-1)*(N-1)+j)=x(j);
end
end
fun1=inline('sin(pi.*y).*(-exp(20+sqrt(400+pi*pi))/(exp(20-sqrt(400+pi*pi))-exp(20+sqrt(400+pi*pi)))*(exp(20-sqrt(400+pi*pi))).^x+exp(20-sqrt(400+pi*pi))/(exp(20-sqrt(400+pi*pi))-exp(20+sqrt(400+pi)))*(exp(20+sqrt(400+pi))).^x)','x','y');
for i=1:(N-1)*(N-1)
z(i)=feval(fun1,xx(i),yy(i));
e(i)=abs(U(i)-z(i));
end
E=max(e);
%调试
clear all;clc;close all;
[E,U1,xx1,yy1]=Fivepoints(20);%五点差分法
[E,U2,xx2,yy2]=V2(20);%有限体积法
[X1,Y1,Z1]=griddata(xx1,yy1,U1,linspace(0,1,20)',linspace(0,1,20),'v4');%插值
[X2,Y2,Z2]=griddata(xx2,yy2,U2,linspace(0,1,20)',linspace(0,1,20),'v4');%插值
subplot(2,2,1);
surf(X1,Y1,Z1)%三维曲面
title('五点差分法');
hold on;
subplot(2,2,2);
surf(X2,Y2,Z2)%三维曲面
title('有限体积法');
[x,y]=meshgrid(0:1/20:1);
z=sin(pi.*y).*(-exp(20+sqrt(400+pi*pi))/(exp(20-sqrt(400+pi*pi))-exp(20+sqrt(400+pi*pi)))*(exp(20-sqrt(400+pi*pi))).^x+exp(20-sqrt(400+pi*pi))/(exp(20-sqrt(400+pi*pi))-exp(20+sqrt(400+pi)))*(exp(20+sqrt(400+pi))).^x);
subplot(2,2,3);
mesh(x,y,z);%画出精确解
title('解析解');
N=[8,16,32,64];
for i=1:4
e1(i,1)=Fivepoints(N(i));
e2(i,1)=V2(N(i));
end
for i=1:3
s1(i,1)=log2(e1(i,1)/e1(i+1,1));
s2(i,1)=log2(e2(i,1)/e2(i+1,1));
end
问题结果
(N=20,h=0.05时,两种方法与解析解的图像如下所示)文章来源:https://www.toymoban.com/news/detail-421426.html
从图中我们发现,数值解与解析解的图像高度吻合。为了得到两种方法的收敛阶,我们求出N=8,16,32,64对应的最大误差,并用收敛阶计算公式:
s
N
=
l
o
g
2
(
ϵ
m
a
x
N
ϵ
m
a
x
2
N
)
s_N=log_2\left(\frac{\epsilon_{max}^N}{\epsilon_{max}^{2N}}\right)
sN=log2(ϵmax2NϵmaxN)计算收敛阶。
从第一张表中我们可以看出,两种方法的最大误差高度相似,初步判定两种方法拥有一样的收敛阶。接着利用收敛阶计算公式,由计算结果可见,随着N值的增大,两种方法的收敛阶越来越接近于2,从而说明了五点差分法与有限体积法的收敛阶为2阶。文章来源地址https://www.toymoban.com/news/detail-421426.html
到了这里,关于二阶椭圆型第一边值问题的数值解法(五点差分格式和有限体积法)附matlab代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!