Did you know you can solve 2D partial differential equations (PDEs) in Excel without resorting to macros?
In this post, we’ll look at how to solve Laplace’s Equation, , in two dimensions using nothing but Microsoft Excel.
- Introduction
- Laplace’s equation in two dimensions
- Numerical solution to Laplace’s equation
- Using Excel to solve Laplace’s equation
- Analysis of numerical stability for Excel method
- Further reading
Introduction
Heat transfer is a critical concept in chemical engineering, as engineers often need to design equipment for heating large volumes of potentially hazardous chemicals. Accurate analysis of heat distribution is crucial to prevent overheating and explosions.
What exactly does Laplace’s equation tell us about heat transfer? Well, one thing it can tell us is what the stable temperature profile of an object will look like after being exposed to a constant heat source for a long time. For example, if you very gently heat a marshmallow from the bottom (making sure that it doesn’t melt or burn!) and hold it there for a while, the temperature will eventually reach a stable profile as heat enters the marshmallow from the bottom and leaves from the sides of the marshmallow. This final temperature profile might look something like this:
The goal of Laplace’s equation is to find a formula that describes this steady state profile (in the case of the marshmallow, ).
Laplace’s equation in two dimensions
In the example above, I decided to describe the heat profile as only a function of , the vertical distance from the flame. But in reality, the steady state heat profile might vary both in the radial and vertical dimensions.
To illustrate Laplace’s equation in two dimensions, let’s consider a square plate of metal that we heat on one side, like the one below:
What if we plotted the steady-state heat profile in this plate? It will probably look something like this:
We can immediately make some guesses about what the temperature profile of the plate, expressed as , will look like:
- will decrease as increases: heat dissipates as you get further away from the flame.
- will be lowest at the non-heated edges of the plate, since heat dissipates from the non-heated edges.
If we want to explicitly solve for , we can use Laplace’s equation to set up the following PDE:
which expands to
We cannot solve this equation unless we also provide a set of boundary conditions. To create a toy model of the plate-heating scenario so that we can solve for , let’s assume that we can maintain the heated side of the plate at and the other three sides at . If we set the origin as the bottom left of the plate and set the length of the plate to , our boundary conditions are:
Numerical solution to Laplace’s equation
The PDE we specified above is very hairy to solve analytically, so we choose to take a numerical approach instead. One possible approach is creating a coarse grid of points with spacing and solving for the numerical value of at each point.
We can actually solve for the value of at each grid point all at once using a clever trick. To do this, we need to assume that each grid point’s value can be inferred from its neighbors’ values. If we can write a linear equation relating these values together, then we can have a separate linear equation for every grid point. So if we have grid points, we have linear equations, which we can solve simultaneously!
Thus, our task now is to find a linear equation that relates grid points to their neighbors. One possible way to do this is through expansions inspired by the definition of the derivative:
As you’ll notice in this expansion, the derivative of a function can be expressed as the slope of between positions and . This can inspire us to write a forward-difference approximation:
Note: In the above equation, I’m actually being messy with notation since I’ve only written it for one dimension, . The “proper” way to express this same statement for our 2D grid is as follows:
It’s not set in stone that we must do a “forward” difference by finding the slope between and . We can calculate a more accurate derivative numerically by using a central difference between and , which we express as:
What if we wanted to find the second derivative? We can do this by applying the central difference formula again:
Using this, we come up with the following approximations for the terms in Laplace’s equation:
Substituting into Laplace’s equation, , we get
which simplifies to
Solving for , we get
In other words, each cell is the average of its neighbors. If we have interior grid points and grid points on the boundary, we can start writing a system of linear equations to solve for the interior grid point values. For notation’s sake, let’s call each temperature value on the grid . Then our interior equations will have the form
while our boundary points are given values , which we obtain from our boundary value problem’s specification. Since we now have a set of equations with variables, and we can solve for the temperature profile by solving all equations simultaneously.
Using Excel to solve Laplace’s equation
Solving all equations simultaneously can require a lot of memory, which is a problem. If our grid contains a total of interior points (where is the side length of the grid, excluding edges), we will obtain equations. Then if we naively plug this into a linear equation solver, we would have to express these equations as an matrix of coefficients (one row per equation), which contains values.
If we want to avoid this, we might try an iterative method instead, where we just work directly on our grid of values. The idea behind this is to load our initial guesses into the grid, and then nudge the values around until they satisfy Laplace’s equation.
In the case of Laplace’s equation, we can nudge the values around by repeatedly applying the averaging formula we derived earlier to every grid point, and just hope that we get a stable solution.
Let’s try this in Excel. What we’re going to do is initialize the interior points with an initial value of 0 and set the boundary value to 100 on the bottom of the plate (I’ve used conditional formatting here to make higher values red):
Next, we’ll input our averaging formula into the top left interior cell, and use Excel’s click and drag feature to copy the formula to the rest of the interior cells:
Our interior values haven’t changed! Why? Because we have circular references, which Excel refuses to operate on. The value of cell B2 depends on the value of cell C2, but the value of cell C2 depends on the value of B2. To get around this, Excel has an option for iterative calculation, under Options -> Formulas -> Enable iterative calculation.
Let’s enable it and see what happens:
Amazingly, this converges! This should be surprising because everything about our implementation so far has been sloppy – we kind of threw our derivative approximations at Excel, asked it to repeatedly update the values at each grid point based on these equations, and crossed our fingers hoping that this process would converge. It’s not immediately clear why this would be numerically stable, so let’s examine why this is the case.
Analysis of numerical stability for Excel method
It turns out that what we’ve done is secretly equivalent to the Gauss-Siedel method for iteratively solving matrix equations. If we chose to solve the system using the Gauss-Siedel method instead of the Excel method outlined above, we’d have to construct an matrix representing the equations for our interior point temperatures and perform updates to a length- solution vector until we achieve the solution, . These updates work by separating into its lower triangular slice and its strictly upper triangular slice , and updating using the formula . What this formula does is the equivalent of iterating through each equation in our system, solving for in terms of , and updating with the new value of before moving on to the next equation.
In our Excel implementation, this is indeed what we are doing when we repeatedly apply our averaging formula: we express each element of in terms of other elements of , update the value in place, and move on to the next value. Since Excel executes iterated calculations from left to right, top to bottom, it performs the Gauss-Siedel iteration the same way each time it goes through the entire matrix.
Why does our implementation converge? The Gauss-Siedel method is guaranteed to converge for strictly diagonally dominant systems, or systems where the magnitude of all diagonal elements of are strictly greater than the sum of the magnitude of all other entries in the same row, i.e.,
In the case of our averaging formula, , the diagonal elements of (i.e., the variables we are solving for) will have a magnitude (i.e., the coefficient of ) and the four off-diagonal elements will have magnitude (i.e., the other coefficients in the equation). Our convergence criterion is not met since the diagonal elements have magnitudes that are equal to the sum of the magnitudes of the off-diagonal elements: . Thus, the Gauss-Siedel method is not guaranteed to converge for . However, if we provide reasonable initial conditions, it is still possible for the method to converge – it just isn’t guaranteed.
Further reading
- https://edisciplinas.usp.br/pluginfile.php/41896/mod_resource/content/1/LeVeque%20Finite%20Diff.pdf
- https://www.andrew.cmu.edu/course/24-681/handouts/lectures/fdm_for_laplace_equation.pdf
- https://aquaulb.github.io/book_solving_pde_mooc/solving_pde_mooc/notebooks/05_IterativeMethods/05_01_Iteration_and_2D.html
- http://www.decisionmodels.com/calcsecretsc.htm
Leave a comment