Numerical solutions to second-order ordinary differential equations (central difference method and finite volume method)
Here we introduce the central difference method and the finite volume method to solve equations.
Question:
Use the central difference format of the difference method and the finite volume method to solve the two-point boundary value problem
u ′ ′ − α ( 2 x − 1 ) u ′ − 2 α u = 0 , 0 < x < 1 u ( 0 ) = u ( 1 ) = 1 , u^{\prime\prime}-\alpha\left(2x-1\right)u^\prime-2\alpha u=0,0<x<1 u\left(0\right )=u\left(1\right)=1,u′′−a( 2 x−1)u′−2 o=0,0<x<1 u(0)=u(1)=1 ,
where parameterα = 10 \alpha=10a=10 , the maximum error and convergence order of different grids are obtained.
problem analysis
Step 1: Mesh generation: First, take N + 1 nodes as: a = x 0 < x 1 < x 2 < ⋯ < x N = ba=x_0<x_1<x_2<\cdots<x_N=ba=x0<x1<x2<⋯<xN=b ,
divide the interval [a, b] into equal intervals and divide it into N small intervals, noteh = xi + 1 − xi , i = 1 , 2 , ⋯ , N − 1 h=x_{i+1} -x_i,i=1,2,\cdots,N-1h=xi+1−xi,i=1,2,⋯,N−1 is the grid step size. Then perform dual decomposition: take adjacent nodesxi + 1, xi x_{i+1}, x_ixi+1,xi 的中点 x i + 1 2 = 1 2 ( x i + 1 − x i ) , i = 1 , 2 , ⋯ , N − 1. x_{i+\frac{1}{2}}=\frac{1}{2}\left(x_{i+1}-x_i\right),i=1,2,\cdots,N-1. xi+21=21(xi+1−xi),i=1,2,⋯,N−1. The analysis of the structure of the structure.
(i) Central difference method:
1 h 2 ( ui + 1 − 2 ui + ui − 1 ) − α ( 2 xi − 1 ) 2 h ( ui + 1 − ui − 1 ) − 2 α ui = 0 \frac{1}{h^2}\left(u_{i+1}-2u_i+u_{i-1}\right)-\frac{\alpha\left (2x_i-1\right)}{2h}\left(u_{i+1}-u_{i-1}\right)-2\alpha u_i=0h21(ui+1−2 andi+ui−1)−2h _α(2xi−1)(ui+1−ui−1)−2 α ui=0
⇒ \Rightarrow ⇒
( 1 h 2 − r i ) u i + 1 + ( − 2 h 2 − 2 α ) u i + ( 1 h 2 + r i ) u i − 1 = 0 \left(\frac{1}{h^2}-r_i\right)u_{i+1}+\left(-\frac{2}{h^2}-2\alpha\right)u_i+\left(\frac{1}{h^2}+r_i\right)u_{i-1}=0 (h21−ri)ui+1+(−h22−2a ) _ui+(h21+ri)ui−1=0,
where r i = α ( 2 x i − 1 ) 2 h . r_i=\frac{\alpha\left(2x_i-1\right)}{2h}. ri=2h _α(2xi−1).
Let A = [ − 2 h 2 − 2 α 1 h 2 − r 1 0 ⋯ 0 0 1 h 2 + r 2 − 2 h 2 − 2 α 1 h 2 − r 2 ⋯ 0 0 ⋯ ⋯ ⋯ ⋯ ⋯ ⋯ 0 0 0 ⋯ 1 h 2 + r N − 1 − 2 h 2 − 2 α ] , A=\left[\begin{matrix}-\frac{2}{h^2}-2\alpha&\frac{1}{h^2}-r_1&0&\cdots&0&0\\\frac{1}{h^2}+r_2&-\frac{2}{h^2}-2\alpha&\frac{1}{h^2}-r_2&\cdots&0&0\\\cdots&\cdots&\cdots&\cdots&\cdots&\cdots\\0&0&0&\cdots&\frac{1}{h^2}+r_{N-1}&-\frac{2}{h^2}-2\alpha\\\end{matrix}\right], A=
−h22−2 ah21+r2⋯0h21−r1−h22−2 a⋯00h21−r2⋯0⋯⋯⋯⋯00⋯h21+rN−100⋯−h22−2 a
,
U = ( u 1 , u 2 , ⋯ , u N − 1 ) T , b = ( − u 0 ( 1 h 2 + r 1 ) , 0 , 0 , ⋯ , 0 , − u N ( 1 h 2 + r N − 1 ) ) T U=\left(u_1,u_2,\cdots,u_{N-1}\right)^T,\\ b=\left(-u_0\left(\frac{1}{h^2}+r_1\right),0,0,\cdots,0,-u_N\left(\frac{1}{h^2}+r_{N-1}\right)\right)^T U=(u1,u2,⋯,uN−1)T,b=(−u0(h21+r1),0,0,⋯,0,−uN(h21+rN−1))T .
Then AU=b.
Then solve for U.
(ii) Finite volume method:
Let W ( x ) = dudx W\left(x\right)=\frac{du}{dx}W(x)=dxof u, then d W d x − α ( 2 x − 1 ) W ( x ) − 2 α u = 0. \frac{dW}{dx}-\alpha\left(2x-1\right)W\left(x\right)-2\alpha u=0. dxdW−a( 2 x−1)W(x)−2 o=0.
两边同时积分得:
W ( x i + 1 2 ) − W ( x i − 1 2 ) − ∫ x i − 1 2 x i + 1 2 α ( 2 x − 1 ) W ( x ) d x − ∫ x i − 1 2 x i + 1 2 2 α u d x = 0. W(x_{i+\frac{1}{2}})-W(x_{i-\frac{1}{2}})-\int_{x_{i-\frac{1}{2}}}^{x_{i+\frac{1}{2}}}α(2x-1)W(x)dx-\int_{x_{i-\frac{1}{2}}}^{x_{i+\frac{1}{2}}}2αudx=0. W(xi+21)−W(xi−21)−∫xi−21xi+21α(2x−1)W(x)dx−∫xi−21xi+212αudx=0.
运用积分中值定理可得:
W ( x i + 1 2 ) − W ( x i − 1 2 ) − W ( x i ) ∫ x i − 1 2 x i + 1 2 α ( 2 x − 1 ) d x − u i ∫ x i − 1 2 x i + 1 2 2 α d x = 0. W(x_{i+\frac{1}{2}})-W(x_{i-\frac{1}{2}})-W(x_i)\int_{x_{i-\frac{1}{2}}}^{x_{i+\frac{1}{2}}}α(2x-1)dx-u_i\int_{x_{i-\frac{1}{2}}}^{x_{i+\frac{1}{2}}}2αdx=0. W(xi+21)−W(xi−21)−W(xi)∫xi−21xi+21α(2x−1)dx−ui∫xi−21xi+212αdx=0.
⇒ \Rightarrow ⇒
W ( x i + 1 2 ) − W ( x i − 1 2 ) − W ( x i ) ( x i + 1 2 + x i − 1 2 − 1 ) h α − 2 α u i h = 0 W(x_{i+\frac{1}{2}})-W(x_{i-\frac{1}{2}})-W(x_i)(x_{i+\frac{1}{2}}+x_{i-\frac{1}{2}}-1)hα-2α u_ih=0 W(xi+21)−W(xi−21)−W(xi)(xi+21+xi−21−1)hα−2 α uih=0
⇒ \Rightarrow ⇒
1 h ( u i + 1 − 2 u i + u i − 1 ) − α 4 ( x i + 1 + 2 x i + x i − 1 ) ( u i + 1 − u i − 1 ) + α 2 ( u i + 1 − u i − 1 ) − 2 α u i h = 0 \frac{1}{h}\left(u_{i+1}-2u_i+u_{i-1}\right)-\frac{\alpha}{4}\left(x_{i+1}+2x_i+x_{i-1}\right)\left(u_{i+1}-u_{i-1}\right)+\frac{\alpha}{2}\left(u_{i+1}-u_{i-1}\right)-{2\alpha\ u}_ih=0 h1(ui+1−2 andi+ui−1)−4a(xi+1+2x _i+xi−1)(ui+1−ui−1)+2a(ui+1−ui−1)−2 α u ih=0
⇒ \Rightarrow ⇒
( 1 h − l i + α 2 ) u i + 1 + ( − 2 h − 2 α h ) u i + ( 1 h + l i − α 2 ) u i − 1 = 0 , \left(\frac{1}{h}-l_i+\frac{\alpha}{2}\right)u_{i+1}+\left(-\frac{2}{h}-2\alpha h\right)u_i+\left(\frac{1}{h}+l_i-\frac{\alpha}{2}\right)u_{i-1}=0, (h1−li+2a)ui+1+(−h2−2 α h )ui+(h1+li−2a)ui−1=0,
where l i = α 4 ( x i + 1 + 2 x i + x i − 1 ) {\ l}_i=\frac{\alpha}{4}\left(x_{i+1}+2x_i+x_{i-1}\right) li=4a(xi+1+2x _i+xi−1) _
LetA = [ − 2 h − 2 α h 1 h − l 1 + α 2 0 ⋯ 0 0 1 h + l 2 − α 2 − 2 h − 2 α h 1 h − l 2 + α 2 ⋯ 0 0 ⋯ ⋯ ⋯ ⋯ ⋯ ⋯ 0 0 0 ⋯ 1 h + l N − 1 − α 2 − 2 h − 2 α h ] , A=\left[\begin{matrix}-\frac{2}{h}-2\ alpha h&\frac{1}{h}-l_1+\frac{\alpha}{2}&0&\cdots&0&0\\\frac{1}{h}+l_2-\frac{\alpha}{2}&-\frac {2}{h}-2\alpha h&\frac{1}{h}-l_2+\frac{\alpha}{2}&\cdots&0&0\\\cdots&\cdots&\cdots&\cdots&\cdots&\cdots\\0&0&0& \cdots&\frac{1}{h}+l_{N-1}-\frac{\alpha}{2}&-\frac{2}{h}-2\alpha h\\\end{matrix}\ right],A=
−h2−2 α hh1+l2−2a⋯0h1−l1+2a−h2−2 α h⋯00h1−l2+2a⋯0⋯⋯⋯⋯00⋯h1+lN−1−2a00⋯−h2−2 α h
,
U = ( u 1 , u 1 , ⋯ , u N − 1 ) T , F = ( − u 0 ( 1 h + l 1 − α 2 ) , 0 , 0 , ⋯ , 0 , − u N ( 1 h − l N − 1 + α 2 ) ) T U=\left(u_1,u_1,\cdots,u_{N-1}\right)^T,F=\left(-u_0\left(\frac{1}{h}+l_1-\frac{\alpha}{2}\right),0,0,\cdots,0,-u_N\left(\frac{1}{h}-l_{N-1}+\frac{\alpha}{2}\right)\right)^T U=(u1,u1,⋯,uN−1)T,F=(−u0(h1+l1−2a),0,0,⋯,0,−uN(h1−lN−1+2a))T
Then AU=F.
Then solve for U.
All codes are as follows (matlab)
function E=Centered(h)%中心差分法
u0=1;
uN=1;
N=1/h;
x=zeros(1,N+1);
for i=1:(N)
x(i+1)=x(i)+h;
end
R=zeros(1,N+1);
for i=1:(N+1)
R(i)=10*(2*x(i)-1)/(2*h);
end
A=zeros(N-1,N-1);
A(1,1)=-2/(h*h)-20;
A(1,2)=1/(h*h)-R(2);
A(N-1,N-1)=-2/(h*h)-20;
A(N-1,N-2)=1/(h*h)+R(N)
for i=2:(N-2)
A(i,i)=-2/(h*h)-20;
A(i,i+1)=1/(h*h)-R(i+1);
A(i,i-1)=1/(h*h)+R(i+1);
end
b=zeros(1,N-1);
b(1)=-u0*(1/(h*h)+R(2));
b(N-1)=-uN*(1/(h*h)-R(N));
y=A\b';
Y=zeros(N+1,1);
Y(1,1)=1;
Y(N+1,1)=1;
for i=2:N
Y(i,1)=y(i-1,1);
end
plot(x,Y ,'r*-');
hold on;
exa = dsolve('D2u = 10*(2*x-1)*Du+20*u', 'u(0)=1','u(1)=1', 'x'); %求出解析解
ezplot(exa, [0,1]); %画出解析解的图像
legend('中心差分法数值解','解析解' );
u=zeros(1,N+1);
exa=inline(exa);
for i=1:(N+1)
u(i)=feval(exa,x(i));
e(i)=abs(u(i)-Y(i));
end
E=max(e);
function E=V(h)%有限体积法
u0=1;
uN=1;
N=1/h;
x=zeros(1,N+1);
for i=1:(N)
x(i+1)=x(i)+h;
end
R=zeros(1,N+1);
for i=2:N
R(i)=2.5*(x(i+1)+2*x(i)+x(i-1));
end
A=zeros(N-1,N-1);
A(1,1)=-2/h-20*h;
A(1,2)=1/h+5-R(2);
A(N-1,N-1)=-2/h-20*h;
A(N-1,N-2)=1/h-5+R(N);
for i=2:(N-2)
A(i,i)=-2/h-20*h;
A(i,i+1)=1/h+5-R(i+1);
A(i,i-1)=1/h-5+R(i+1);
end
b=zeros(1,N-1);
b(1)=-u0*(1/h-5+R(2));
b(N-1)=-uN*(1/h+5-R(N));
y=A^(-1)*b';
Y=zeros(N+1,1);
Y(1,1)=1;
Y(N+1,1)=1;
for i=2:N
Y(i,1)=y(i-1,1);
end
plot(x,Y ,'r*-');
hold on;
exa = dsolve('D2u = 10*(2*x-1)*Du+20*u', 'u(0)=1','u(1)=1', 'x'); %求出解析解
ezplot(exa, [0,1]); %画出解析解的图像
legend('有限体积法数值解','解析解' );
u=zeros(1,N+1);
exa=inline(exa);
for i=1:(N+1)
u(i)=feval(exa,x(i));
e(i)=abs(u(i)-Y(i));
end
E=max(e);
%调试
clear all;clc;close all;
h=[0.1,0.2,0.01,0.02,0.05];
for i=1:5
E(i)=V(h(i));
F(i)=Centered(h(i));
end
Question result
We found that the maximum error of the central difference method and the finite volume method have a small difference and the order is the same. Therefore, we can think that the convergence orders of the central difference method and the finite volume method are the same, and we know that the convergence of the central difference method is the same. The order is 2nd order, so the convergence order of the finite volume method is also 2nd order.
(Taking h=0.1 as an example, we drew the images of the numerical solution and the analytical solution of the two methods)