# Solving Poisson Equation Finite-difference using Python

Hi I'm trying to compute numerically the solution to the next Poisson equation: $$\dfrac{\partial^2 u}{\partial x^2} + \dfrac{\partial^{2}u}{\partial y^{2}} = 4$$ with the boundary conditions $$u(x,0) = x^2, \quad u(0,y) = y^2, \quad u(x,2) = (x-2)^{2}, \quad u(1,y) = (y-1)^{2}$$ for $0<x<1$ and $0<y<2$. The anallytic solution is $u(x,y) = (x-y)^{2}$.

Using Finite-Differencies. I have the code FDtoPoissonEq on python, but don't work. See the picture (Picture of Tray 1 to Poisson Equation) to undestand that I want to say.

Postdata: The code is based on algorithm 12.1 from Burden.

• @ian I used the algorithm 12.1 from burden to do this. I only traduce it to python. – Jonathan G. May 21 '16 at 1:44
• Oh, I'm sorry, I had a script disabled so I couldn't see the entire program. Anyway, your code seems way more complicated than it needs to be. The main difficulty is assembling the matrix that, given a vector of function values at all the grid points, gives the Laplacian of the function at those grid points. This is tricky because you essentially have to turn a 2D grid into a 1D vector. But there is a standard way to do it using a function which in numpy is called roll (in Matlab it is called circshift). Once you have done that the problem becomes a linear system. – Ian May 21 '16 at 1:46
• The idea in this approach is to enumerate the 2D indices. There are two convenient ways to do this, called row major and column major. The column major approach would match $(i,j)$ with $i+jm$, where $i$ is the row index, $j$ is the column index, and $m$ is the number of rows (and indexing starts from 0). The row major approach uses $in+j$ instead, where $n$ is the number of columns. Once you have done that the problem becomes a linear system that you can solve with a standard routine like numpy.linalg.solve . – Ian May 21 '16 at 1:49