Introduction to the problem
Initial condition:
Analytical condition
Numerical solution
This problem has the form:
where: ,
Application of the central difference approximations:
yields the stencil:
where the terms , , and are given by:
For Neumann Boundary Condition:
Using 2nd order forward difference:
Combining these equations with the boundary conditions yields the system of equations:
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^2q(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./(4x)+log(x)./(2*x);
plot(x,u)
hold on
plot(x,uu)
axis equal
legend(‘Numerical solution’,‘Analytical solution’)
最後閒扯兩句。
自己開這個博客的本意就是,想把這一年來學過的數值分析和傅里葉變換解決BVP問題做個對比記錄下來,也不知道自己還能夠堅持做這件事多久。近來的世界變化對於我的未來和人生觀產生了巨大的影響。我甚至不知道寫這個博客是否還有意義,因爲很有可能以後再也用不到了。希望讀者看到數值方法的簡潔與強大的同時能夠看到解析解的優美。就本例子而言,這個複雜的非線性微分方程的解可以通過計算特徵根,特徵方程和特解,代入邊界條件求解,其過程較爲繁瑣但是原理並不複雜。有限差分法過程簡單,但是其中誤差的來源構成複雜。希望有耐心看到這裏的朋友們能夠欣賞到數值方法和解析方法之間的微妙關係。