Solving pure Neumann boundary value problem numerically

658 Views Asked by At
% heat.m
% MATLAB/Octave commands for solving the ordinary differential equation
% T''(x) = 0 with T'(0) = 0 and T'(1) =0 

% set the number of approximation points to be n+1=51
n = 10;

% create the set of points x_i
X = linspace(0,1,n+1);

% set the gap between points (DX)
dx = 1/n;

% create the matrix L
L = ( diag(-2*ones(1,n+1)) + diag(ones(1,n),-1) + diag(ones(1,n),1) );
L(1,1) = -1;
L(1,2) = 1;
L(n+1,n+1) = 1;
L(n+1,n) = -1;

% additional condition to ensure uniqueness
uni = zeros(1,n+1);
uni(n/2) = 1;

L = [L; uni] ;

% create the vector for the right-hand-side of the matrix equation
r = -ones(n+2,1)*dx^2;
r(1) = 0;
r(n+1) = 0;
r(n+2) = 1; % arbitrary chosen constant

% solve the matrix equation
T = L\r;

% plot the solution 
plot(X,T,'-o');
xlabel('x');
ylabel('T');

enter image description here

I'm surprised because this is inconsistent with the matrix equation. How is this possible? There derivative should be zero because I have enforced it.

>> (T(2)-T(1))/(dx)

ans =

   0.081818181818183
2

There are 2 best solutions below

0
On

You have not explicitly said that $T'(0)= T'(L)= 1$. Imagine you make a uniform partition of $[0,L]$, $x_0 = 0$, $x_1 = h$, $\dots$, $x_n = L$ with the equation $T''(x) = 0$ (as you stated at the top of your code)

$$ \frac{T_{i + 1} - 2T_i + T_{i-1}}{h^2} = 0 \tag{1} $$

And the boundary condition $T'(0) = 0$

$$ \frac{T_1 - T_0}{h} = 0 ~~~\Rightarrow~~~ T_1 = T_0 \tag{2} $$

and

And the boundary condition $T'(0) = 0$

$$ \frac{T_n - T_{n-1}}{h} = 0 ~~~\Rightarrow~~~ T_{n-1} = T_n \tag{3} $$

Now, you want to assemble your system, and this is what you get. From Eq. (1):

\begin{eqnarray} T_2 - 2T_1 + T_0 = 0 ~~&\stackrel{(1)}{\Rightarrow}&~~~ T_2 = 2T1 - T_0 = T_0 \\ T_3 - 2T_2 + T_1 ~~&\Rightarrow&~~~ T_3 = 2 T_2 - T_1 = T_0 \\ &\vdots&\\ &&~~~ T_{n-1} = T_0 = T_n \end{eqnarray}

So at the end you end up with the system of equation

$$ T_0 = T_1 = T_2 = \cdots = T_n \tag{4} $$

What you should take out of this:

  • You need to carefully track your boundary conditions at the time of assembling your FD equations

  • It is not as trivial as multiplying by a number a vector of ones and hoping for the best

0
On

Please see my answer to this question to see what such a matrix would look like (the matrix already needs to contain the boundary condition) and it should deal with only values of $x_i$ where $i \in \{1,...,n-1\}$