Poisson Equation Solver

JJ Ben-Joseph headshot JJ Ben-Joseph

The Poisson Equation

The Poisson equation 2 u = f arises in electrostatics, gravitation, and many areas of physics and engineering. It describes how a source distribution f generates a potential field u . On a two-dimensional unit square with zero boundary conditions, the equation becomes

Formula: &partial^2 / u &partialx^2 + &partial^2 / u &partialy^2 = f x, y

&partial 2 u &partialx 2 + &partial 2 u &partialy 2 = f x , y

Even for simple Poisson sources, closed-form solutions can be cumbersome. This calculator uses Gauss–Seidel relaxation to approximate u by sweeping across the grid repeatedly until the updates settle down.

Discretization

For this Poisson equation solver, we divide the unit square into n intervals in each direction, giving grid spacing h = 1 n . Interior grid points are indexed by i and j . The discrete Poisson equation with Dirichlet boundary conditions reads

Formula: u_i,j = (u_i+1 + u_i-1 + u_i,j+1 + u_i,j-1 - h^2 f_i,j) / 4

u i , j = u i + 1 + u i - 1 + u i , j + 1 + u i , j - 1 - h 2 f i , j 4

In this Poisson grid solve, starting with u zero everywhere lets the source term pull the interior values away from the boundary while the edges remain fixed at zero. After enough iterations, the grid converges toward the discrete solution that best balances the four neighboring values against the source at each interior point.

Poisson Equation Formula: Understanding the Gauss–Seidel Update

In this Poisson equation solver, the Gauss–Seidel method updates points sequentially and uses the newest available values immediately. That ordering usually converges faster than the Jacobi method, which updates every point from the previous sweep. The convergence rate depends on the grid spacing and the spectral properties of the discrete Laplacian. For smooth sources, doubling n roughly quadruples the number of iterations needed for similar accuracy, illustrating the cost of finer grids.

One way to gauge convergence in this discrete Poisson solve is to monitor the residual: the difference between the left-hand side and right-hand side of the update formula. As iterations proceed, the residual diminishes, showing that the current grid more closely satisfies the finite-difference equation.

Introduction: Why Solve the Poisson Equation?

This Poisson equation calculator is useful because a source term by itself does not tell the whole story; the equation turns that source into a smooth field that can be inspected and compared. In electrostatics, u might represent an electric potential generated by a charge density f . In heat conduction, it describes the steady-state temperature distribution given an internal heat source. Gravitational potential, membrane deflection, and even image processing tasks rely on similar equations. Developing numerical intuition via a simple solver provides a gateway to more advanced simulation techniques.

While this Poisson calculator enforces zero boundary conditions for simplicity, real-world problems may have arbitrary boundaries or Neumann conditions on flux. Finite element methods and multigrid algorithms extend the same ideas to complex domains and improve efficiency dramatically.

Stopping Criteria and Accuracy for the Poisson Solver

In this Poisson equation solver, the new tolerance field lets you decide how small the grid changes should be before the solver halts. After each sweep through the interior points, the script tracks the largest change between the old and new value. Once this change drops below your chosen tolerance, further iterations yield only minor improvements and the algorithm stops automatically. A smaller tolerance means a closer approach to the exact solution but requires more iterations and therefore more computation time. Conversely, a loose tolerance produces a quick approximate answer that may be sufficient for rough intuition or classroom demonstrations.

The maximum iterations box in this Poisson calculator provides a safety cap in case the solution converges slowly or not at all. If the solver reaches the maximum number of iterations without satisfying the tolerance, it returns the current grid along with the final observed change. This approach mirrors how scientific computing libraries handle iterative problems: a mix of convergence criteria and iteration limits prevents endless loops while still giving you control over the trade-off between speed and precision.

How to use this Poisson Equation Solver Step by Step

To use this Poisson equation solver, begin by defining the source term f . The input box accepts any expression that Math.js understands, including functions such as sin, exp, or polynomials in x and y. Next, choose a grid size. Larger grids capture more detail but require more memory and time; doubling n roughly quadruples the number of interior points that must be updated each iteration. Then select a tolerance that reflects your desired accuracy and a maximum iteration count that balances patience with practicality. Once you press Solve, the algorithm iterates until the maximum change between successive sweeps drops below the tolerance or the iteration limit is reached. The calculator then reports the value at the center of the domain along with the number of iterations performed and the final max change, giving a concise summary of the convergence behavior.

Boundary Conditions and Extensions for the Poisson Solver

This Poisson equation demonstration focuses on a unit square with zero values along the edges—Dirichlet boundary conditions. Many real-world problems involve nonzero boundaries or Neumann conditions specifying the derivative normal to the edge. Incorporating arbitrary boundary functions would require setting those edge values before the iteration loop, while Neumann conditions demand modifying the discrete equations near the boundary. Although the current interface does not expose these options, understanding how they fit into the finite-difference framework helps when progressing to more sophisticated solvers. Another natural extension is visualizing the solution surface or contour map. Plotting the grid with a library such as Plotly would reveal how the potential bends and twists in response to the source term, making the mathematics more tangible.

Poisson Equation Practical Applications

The Poisson equation solver mirrors situations in which a source distribution must be translated into a smooth potential field. Electrical engineers analyze electrostatic potentials within capacitors or around charge distributions. Mechanical engineers model the deflection of thin plates under uniform load, where the source term represents pressure and the solution corresponds to displacement. Geophysicists study gravitational potential fields, while computer graphics practitioners solve Poisson equations for seamless image editing and gradient-domain processing. The simplicity of this calculator belies the breadth of these applications; experimenting with different source functions can mimic charges, heat sources, or other physical phenomena.

To illustrate a Poisson equation source, consider f (x,y)=1, a uniform source. The solution resembles a gently curved bowl, highest at the center and zero along the edges. Replacing it with sin(π x)sin(π y) yields a sharper peak because the source is strongest near the middle. You can also explore negative sources to simulate sinks or combine multiple sine terms to represent more complex charge distributions. Observing how the center value changes as you adjust the source, the grid size, and the tolerance teaches useful lessons about discretization error and numerical stability without leaving the unit-square setting.

Poisson Equation Common Pitfalls

When you use a Poisson equation solver, iterative methods are sensitive to the scale of the problem. Very fine grids or extremely tight tolerances may lead to long run times and can even expose the limitations of double-precision arithmetic within JavaScript. If the solver fails to converge, try increasing the tolerance or reducing the grid size. Non-smooth or highly oscillatory source terms also challenge basic Gauss–Seidel iteration because the error propagates slowly across the grid. Techniques such as successive over-relaxation, conjugate gradients, or multigrid methods accelerate convergence dramatically, but they require more elaborate code than fits in this introductory example.

Poisson Equation Further Reading

If you want to go deeper into Poisson equation solvers, many numerical analysis texts explore the equation and its relatives in detail. Classic references include Numerical Recipes and the works of Strang and Saad on partial differential equations and iterative methods. Online resources and open-source libraries like FEniCS, deal.II, or SciPy's sparse solvers provide professional-grade tools once you outgrow the simple finite-difference approach. By experimenting with the calculator here, you build intuition that transfers readily to those more advanced environments.

Poisson Equation Frequently Asked Questions

Why is the value at the center reported? For this Poisson equation solver, the center lies farthest from the boundaries, so it best reflects the influence of the source term without the immediate effect of the fixed edges. It also provides a single number that is easy to compare across different runs.

How do I know what tolerance to choose? For the Poisson equation iteration, start with a modest value like 1e-4. If you change the tolerance and the reported center value remains the same to several decimal places, the solution is essentially converged. For high accuracy studies, decrease the tolerance gradually while monitoring computation time.

Can this approach handle three-dimensional problems? The principles behind this Poisson solver are the same in three dimensions, but the grid becomes cubic and the discrete equation involves six neighbors instead of four. Implementing a 3D solver significantly increases memory requirements and makes convergence slower, so more sophisticated acceleration techniques become valuable.

Poisson Equation Limitations

This Poisson equation solver is intentionally straightforward. It operates on a fixed square domain, uses Gauss–Seidel iteration without over-relaxation, and stores the entire grid in memory. JavaScript runs on a single thread and may become sluggish for very large n . Nevertheless, the code captures the essence of finite-difference methods. Enhancements such as vectorization, GPU acceleration, or adaptive meshing could yield enormous speedups but would complicate the implementation. Treat this tool as a learning sandbox: simple, transparent, and ready for modification.

Worked example: the default sine source on the unit square

If you keep the default Poisson source f (x,y)=sin(pi*x)*sin(pi*y), the solution should remain smooth and symmetric, with the strongest response in the interior and zero along the boundary. Tightening the tolerance changes how completely the Gauss–Seidel sweep relaxes the grid, while changing the source expression changes the shape of the potential itself. That contrast is useful when you want to decide whether an unexpected output comes from the physics encoded in f or from the stopping settings that control the iteration.

Enter function and parameters.

Arcade Mini-Game: Poisson Equation Solver Calibration Run

Use this quick arcade run to practice spotting the inputs that matter most in a Poisson equation solve before you trust the center value.

Score: 0 Timer: 30s Best: 0

Start the game, then use your pointer or arrow keys to catch useful inputs and avoid bad assumptions.