Poisson Equation Solver

JJ Ben-Joseph headshot JJ Ben-Joseph

Enter function and parameters.

The Poisson Equation

The Poisson equation 2u=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

&partial;2u&partial;x2+&partial;2u&partial;y2=fx,y

Even for simple sources, closed-form solutions may be cumbersome. Iterative methods provide a practical way to approximate u. This calculator uses the Gauss–Seidel approach to relax the grid repeatedly until changes become small.

Discretization

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

ui,j = ui+1 + ui-1 + ui,j+1 + ui,j-1 - h2 fi,j 4

Starting with u zero everywhere, we repeatedly update each interior point according to this formula. The boundary remains fixed at zero. After enough iterations, the grid converges toward the true solution.

Understanding the Algorithm

The Gauss–Seidel method updates points sequentially and uses the newest available values immediately. This tends to converge faster than the Jacobi method, which updates all points simultaneously from the previous step. 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 is to monitor the residual: the difference between the left-hand side and right-hand side of the discrete equation. As iterations proceed, the residual diminishes, indicating that the current grid closely satisfies the Poisson equation.

Why Solve Poisson?

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 our 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

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 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 nonlinear problems: a mix of convergence criteria and iteration limits prevents endless loops while giving you control over the trade-off between speed and precision.

Using the Calculator Step by Step

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

This 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.

Practical Applications

Engineers and scientists encounter the Poisson equation in diverse settings. 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 our calculator belies the breadth of these applications; experimenting with different source functions can mimic charges, heat sources, or other physical phenomena.

To illustrate, 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 complex charge distributions. Observing how the center value changes with grid size and tolerance teaches valuable lessons about discretization error and numerical stability.

Common Pitfalls

Iterative solvers 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.

Further Reading

If you wish to dive deeper, many numerical analysis texts explore the Poisson 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.

Frequently Asked Questions

Why is the value at the center reported? 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? 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 are the same, 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.

Limitations

This 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.

Related Calculators

Poisson Distribution Calculator - Rare Event Probability

Calculate the probability of observing a given number of events using the Poisson distribution. Useful for physics, biology, and reliability engineering.

Poisson distribution calculator event probability statistics

Heat Equation Solver - 1D Diffusion

Solve the one-dimensional heat equation with Dirichlet boundary conditions.

heat equation solver diffusion PDE

Wave Equation Solver

Solve the one-dimensional wave equation with fixed ends using a finite difference method.