Analytical and Numerical solution of a Boundary Value Problem

Introduction to the problem

t2x(t)+3tx(t)+x(t)=t(t>1)t^2x{''}(t)+3tx{'}(t)+x(t) = t \qquad (t>1)
Initial condition: x(1)=0,x(1)=1x(1) = 0,x{'}(1) = 1

Analytical condition

在這裏插入圖片描述
在這裏插入圖片描述

Numerical solution

This problem has the form:
u=p(x)u+q(x)u+r(x) u^{''}=p(x)u^{'}+q(x)u+r(x)
where: u(a)=αu(a)={\alpha}u(a)=βu^{'}(a)={\beta}
Application of the central difference approximations:
u=ui+1ui12h u^{'}=\frac{u_{i+1}-u_{i-1}}{2h}
u=ui+12ui+ui1h2 u^{''}=\frac{u_{i+1}-2u_{i}+u_{i-1}}{h^{2}}
yields the stencil:
Li[ui1]+Di[ui]+Ui[ui+1]=h2riL_{i}\left[u_{i-1}\right]+D_{i}\left[u_{i}\right]+U_{i}\left[u_{i+1}\right]=-h^{2}r_{i}
where the terms LiL_{i}, DiD_{i}, and uiu_{i} are given by:
Li=1h2pi L_{i}=-1-\frac{h}{2}p_{i}
Di=2+h2qiD_{i}=2+h^{2}q_{i}
Ui=1+h2piU_{i}=-1+\frac{h}{2}p_{i}
For Neumann Boundary Condition:
u(t)=βu^{'}(t) = \beta
Using 2nd order forward difference:
3xi+4xi+1xi+22h=β\frac{-3x_i+4x_{i+1}-x_{i+2}}{2h} = \beta
Combining these equations with the boundary conditions yields the system of equations:
[α0L1D1U1L2D2U2...LN1DN1UN1LNDNUN341][u0u1u2...uN2uN1uN]=[αh2r1h2r2...h2rN2h2rN12hβ]\begin{bmatrix} \alpha & 0 & & & & & & & \\ L_{1} & D_{1} & U_{1} & & & & & & \\ & L_{2} & D_{2} & U_{2} & & & & & \\ & & &. & & & & & \\ & & & & . & & & & \\ & & & & & . & & & \\ & & & & & L_{N-1} & D_{N-1} & U_{N-1} & \\ & & & & & & L_{N} & D_{N} & U_{N} \\ -3 &4 &-1 & & & & & & \\ \end{bmatrix} \begin{bmatrix} u_{0} \\ u_{1} \\ u_{2} \\ . \\ . \\ . \\ u_{N-2} \\ u_{N-1} \\ u_{N} \\ \end{bmatrix} = \begin{bmatrix} {\alpha} \\ -h^{2}r_{1} \\ -h^{2}r_{2} \\ . \\ . \\ . \\ -h^{2}r_{N-2} \\ -h^{2}r_{N-1} \\ {2h\beta} \end{bmatrix}

Code

clear all
N = 90; % Number of sub-intervals
a = 1; % Location of boundary ‘a’
b = 10; % Location of boundary ‘b’
alpha = 0; % Boundary Condition at boundary ‘a’
beta = 1; % Initial Condition at boundary ‘a’
p = @(t) -3/t; % Defining coefficient ‘p(ii)’
q = @(t) -1/(t^2); % Defining coefficient ‘q(ii)’
r = @(t) 1/t;

x = linspace(a,b,N+1); % Defining locations ‘x’
h = (b-a)/N; % Sub-interval size
A = zeros(N+1,N+1);
A(1,1) = 1; % Top-left entry of ‘A’
%A(N+1,N+1) = 1; % Bottom-right entry of ‘A’
A(N+1,1:3) = [-3 4 -1];
B(1,1) = alpha; % First entry of ‘B’
B(N+1,1) = 2hbeta; % Last entry of ‘B’
for ii = 2:(N) % For interior nodes:

L = -1 - (h/2)p(x(ii)); % Defining Sub-diagonal entry ‘L(ii)’
D = 2 + h^2
q(x(ii)); % Defining Diagonal entry ‘D(ii)’
U = -1 + (h/2)*p(x(ii)); % Defining Super-diagonal entry ‘U(ii)’

A(ii,ii-1) = L; % Inserting ‘L(ii)’ in row ii of matrix ‘A’
A(ii,ii) = D; % Inserting ‘D(ii)’ in row ii of matrix ‘A’
A(ii,ii+1) = U; % Inserting ‘U(ii)’ in row ii of matrix ‘A’

B(ii) = -h^2r(x(ii));
end
u = A\B;
%uu is the analytical sloution
uu = x/4-1./(4
x)+log(x)./(2*x);
plot(x,u)
hold on
plot(x,uu)
axis equal
legend(‘Numerical solution’,‘Analytical solution’)
結果高度一致

最後閒扯兩句。
自己開這個博客的本意就是,想把這一年來學過的數值分析和傅里葉變換解決BVP問題做個對比記錄下來,也不知道自己還能夠堅持做這件事多久。近來的世界變化對於我的未來和人生觀產生了巨大的影響。我甚至不知道寫這個博客是否還有意義,因爲很有可能以後再也用不到了。希望讀者看到數值方法的簡潔與強大的同時能夠看到解析解的優美。就本例子而言,這個複雜的非線性微分方程的解可以通過計算特徵根,特徵方程和特解,代入邊界條件求解,其過程較爲繁瑣但是原理並不複雜。有限差分法過程簡單,但是其中誤差的來源構成複雜。希望有耐心看到這裏的朋友們能夠欣賞到數值方法和解析方法之間的微妙關係。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章