# Writing the Poisson equation finite-difference matrix with Neumann boundary conditions

I am interested in solving the Poisson equation using the finite-difference approach. I would like to better understand how to write the matrix equation with Neumann boundary conditions. Would someone review the following, is it correct?

## The finite-difference matrix

The Poisson equation,

$$\frac{\partial^2u(x)}{\partial x^2} = d(x)$$

can be approximated by a finite-difference matrix equation,

$$\frac{1}{(\Delta x)^2} \textbf{M}\bullet \hat u = \hat d$$

where $\textbf{M}$ is an $n \times n$ matrix and $\hat u$ and $\hat d$ are $1 \times n$ (column) vectors,

## Adding a Neumann boundary condition

A Neumann boundary condition enforces a know flux at the boundary (here we apply it at the left-hand side where the boundary is at $x=0$),

$$\frac{\partial u(x=0)}{\partial x} = \sigma$$ writing this boundary condition as a centred finite-difference,

NB. I originally made an error here, sign error and didn't divide by 2. The following has been corrected. $$\frac{u_2 - u_0}{2\Delta x} = \sigma$$

Note the introduction of a mesh point outside the original domain ($u_0$). This term can be eliminated by introducing the second equation, $$\frac{u_0 - 2u_1 + u_2}{(\Delta x)^2} = d_1$$

The equation arrises from having more information because of the introduction of the new mesh point. It allows us to write the double derivative of the $u_1$ as the boundary in terms of $u_0$ using a centred finite-difference.

## The part I'm not sure about

Combining these two equations $u_0$ can be eliminated. To show the working, let's first re-arrange for the unknown,

$$u_0 = -2\sigma\Delta x + u_2 \\ u_0 = (\Delta x)^2 d_1 + 2 u_1 - u_2$$

Next they are set equal and rearranged into the form,

$$\frac{u_2 - u_1}{(\Delta x)^2} = \frac{d_1}{2} + \frac{\sigma}{\Delta x}$$

I chose this form because it is the same form as the matrix equation above. Notice that the $u$ terms are divide by $(\Delta x)^2$ both here and in the original equation. Is this the correct approach?

Finally, using this equation as the first row of the matrix,

Some final thoughts,

1. Is this final matrix correct?
2. Could I have used a better approach?
3. Is there a standard way of writing this matrix?
-
There are two errors in your calculation: a centered finite difference has to be divided by $2 \Delta x$. Second, $u_0 = - \sigma \Delta x + u_2$ is also wrong, because the minus must be a plus. –  vanCompute Feb 22 at 10:02
This is worked out quite nicely in LeVeque's finite difference text, chapter 2. –  David Ketcheson Feb 23 at 11:42
These issues are also well explained in scientificpython.net/1/post/2013/01/… –  Evgeni Sergeev May 25 at 4:40

I think you are on the right way. If you correct your errors, it will look very similar to http://www.math.toronto.edu/mpugh/Teaching/Mat1062/notes2.pdf.

-
Thank you for the comments and corrections. I have updated my question with corrections. I believe it is correct now. –  boyfarrell Feb 25 at 2:25
Great observation to see that $u_0$ can be eliminated.
In a practical sense however this can be troublesome. Matrices are ill-conditioned, and solvers act unpredictably. The most common thing I have seen done is simply "pinning" the solution to a fixed point by specifying $u_0 = 0$ or to a constant relevant to the solution space.