Physics
15 Lines of Python: Poisson’s Equation in N Dimensions
Use Python magic to solve the Poisson equation in any number of dimensions
No matter if you want to calculate heat conduction, the electrostatic or gravitational potential: it’s always the Poisson equation in one form or the other that you have to solve. Due to its importance, you will find a lot of resources on the internet showing you how to solve the Poisson equation numerically. Typically, they show you how to do it in one dimension or in two. Maybe in three. But have you ever seen how to write code that can solve it in an arbitrary number of dimensions? Probably not. This is what this article is about. We will use Python’s magic to solve the problem in a compact and high-performing way.
The basic solution scheme
The Poisson equation reads
where 𝑓 and 𝜌 are real-valued functions of 𝑁 variables and Δ is the 𝑁-dimensional Laplace operator. For instance, for 𝑁=3 dimensions, we have
We still need to supply boundary conditions. So for concreteness, we say that we know the function values on the boundary. The easiest (though not fastest!) way to solve the Poisson equation numerically is using the Jacobi method. Basically, you discretize the…