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问题做个对比记录下来,也不知道自己还能够坚持做这件事多久。近来的世界变化对于我的未来和人生观产生了巨大的影响。我甚至不知道写这个博客是否还有意义,因为很有可能以后再也用不到了。希望读者看到数值方法的简洁与强大的同时能够看到解析解的优美。就本例子而言,这个复杂的非线性微分方程的解可以通过计算特征根,特征方程和特解,代入边界条件求解,其过程较为繁琐但是原理并不复杂。有限差分法过程简单,但是其中误差的来源构成复杂。希望有耐心看到这里的朋友们能够欣赏到数值方法和解析方法之间的微妙关系。