I'm trying to solve a 1D Poisson equation with pure Neumann boundary conditions. I've found many discussions of this problem, e.g.
1) Poisson equation with Neumann boundary conditions
2) Writing the Poisson equation finite-difference matrix with Neumann boundary conditions
3) Discrete Poisson Equation with Pure Neumann Boundary Conditions
4) Finite differences and Neumann boundary conditions
And many more.
Some of the answers seem unsatisfactory though. For example, the answer in 2) contains a document that explains how the matrix $A$ changes when applying Neumann boundary conditions, but does not explain how to solve the singular system. The comment left in 2) by @Evgeni Sergeev is a reference to a problem with mixed boundary conditions, and not pure Neumann boundary conditions.
That said, there are some useful things I've found. I do like the second comment given in the 1) by @Sumedh Joshi. It suggests to subtract the mean from the RHS. Other references I've read suggest using Dirichlet BCs at one location in the computational domain which should result in a unique solution, however, I prefer removing the mean since this seams more elegant.
The problem setup is:
$\frac{\partial^2 u}{\partial x^2} = f, \qquad f = -(2\pi)^2 cos(2\pi x), \qquad 0 \le x\le 1, \qquad \left(\frac{\partial u}{\partial x}\right)_{0} = 0, \qquad \left(\frac{\partial u}{\partial x}\right)_{1} = 0$
I have ghost points in my computational domain, so my matrix $A$ has some extra zeros (I remove them later). Using central differences everywhere in the computational domain, matrix $A$ takes the following form:
$A = \frac{1}{\Delta x^2} \left[ \begin{array}{ccccccccc} 0 & 0 & 0 & & & & & & \\ 1 & -2 & 1 & & & & & & \\ 0 & 1 & -2 & 1 & & & & & \\ & & & \ddots & \ddots & \ddots & & & \\ & & & & & 1 & -2 & 1 & 0 \\ & & & & & & 1 & -2 & 1 \\ 0 & & & & & & 0 & 0 & 0 \\ \end{array} \right] $
Once applying the ghost points and adjusting the matrix $A$, I end up with
$A = \frac{1}{\Delta x^2} \left[ \begin{array}{ccccccccc} 0 & 0 & 0 & & & & & & \\ 0 & -2 & 2 & & & & & & \\ 0 & 1 & -2 & 1 & & & & & \\ & & & \ddots & \ddots & \ddots & & & \\ & & & & & 1 & -2 & 1 & 0 \\ & & & & & & 2 & -2 & 0 \\ 0 & & & & & & 0 & 0 & 0 \\ \end{array} \right] $
Correspondingly, the RHS has changed to
$\frac{- 2u_b + 2u_i}{\Delta x^2} = f_b - \frac{-2\theta \Delta x}{\Delta x^2}$
Where $u_b,u_i,f_b,\theta$ are the $u$ at the boundary, $u$ at the first interior point, $f$ at the boundary and the slope of the solution at the boundary (zero in this case) respectively. The interior of this matrix is the same as the one discussed in the document given in the accepted answer in 2).
Here I show the setup and results of a small MATLAB script where I print out $A, f, mean(f)$ and the (attempted) solution $u$ using MATLAB's backslash operator.
A =
-100.0000 100.0000 0 0 0 0 0 0 0 0 0
100.0000 -200.0000 100.0000 0 0 0 0 0 0 0 0
0 100.0000 -200.0000 100.0000 0 0 0 0 0 0 0
0 0 100.0000 -200.0000 100.0000 0 0 0 0 0 0
0 0 0 100.0000 -200.0000 100.0000 0 0 0 0 0
0 0 0 0 100.0000 -200.0000 100.0000 0 0 0 0
0 0 0 0 0 100.0000 -200.0000 100.0000 0 0 0
0 0 0 0 0 0 100.0000 -200.0000 100.0000 0 0
0 0 0 0 0 0 0 100.0000 -200.0000 100.0000 0
0 0 0 0 0 0 0 0 100.0000 -200.0000 100.0000
0 0 0 0 0 0 0 0 0 100.0000 -100.0000
F =
-19.7392
-31.9387
-12.1995
12.1995
31.9387
39.4784
31.9387
12.1995
-12.1995
-31.9387
-19.7392
meanF =
-9.6892e-016
Warning: Matrix is singular to working precision.
> In main at 121
u =
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
-Inf
-Inf
My question is why is this not working? I thought that removing the mean of the RHS would work as suggested in @Sumedh Joshi's comment. Any help is greatly appreciated.
UPDATE:
I found out that this same exact problem setup works perfectly fine for 12 unknowns (and 15 and much larger, e.g. 100), but does not work for 10 unknowns (as posted). So I suppose that the problem is setup correctly. This still begs the question though, what is going on here? It seems that this may be more of a question regarding numerical analysis.
Here are the matlab results for 12 unknowns:
A =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 -1.0 1.0 0 0 0 0 0 0 0 0 0 0 0 0
0 1.0 -2.0 1.0 0 0 0 0 0 0 0 0 0 0 0
0 0 1.0 -2.0 1.0 0 0 0 0 0 0 0 0 0 0
0 0 0 1.0 -2.0 1.0 0 0 0 0 0 0 0 0 0
0 0 0 0 1.0 -2.0 1.0 0 0 0 0 0 0 0 0
0 0 0 0 0 1.0 -2.0 1.0 0 0 0 0 0 0 0
0 0 0 0 0 0 1.0 -2.0 1.0 0 0 0 0 0 0
0 0 0 0 0 0 0 1.0 -2.0 1.0 0 0 0 0 0
0 0 0 0 0 0 0 0 1.0 -2.0 1.0 0 0 0 0
0 0 0 0 0 0 0 0 0 1.0 -2.0 1.0 0 0 0
0 0 0 0 0 0 0 0 0 0 1.0 -2.0 1.0 0 0
0 0 0 0 0 0 0 0 0 0 0 1.0 -2.0 1.0 0
0 0 0 0 0 0 0 0 0 0 0 0 1.0 -1.0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
f =
0
-19.7392
-34.1893
-19.7392
-0.0000
19.7392
34.1893
39.4784
34.1893
19.7392
0.0000
-19.7392
-34.1893
-19.7392
0
mean(f)
ans =
-9.4739e-016
A(2:end-1,2:end-1)\f(2:end-1)
ans =
0.9167
0.7796
0.4051
-0.1065
-0.6181
-0.9926
-1.1297
-0.9926
-0.6181
-0.1065
0.4051
0.7796
0.9167