Two plates, one at +50 V and the other at −50 V, and the question of what the field does around them. I solved it in C for a course in 2017. The code still runs, and the formula it was being checked against is wrong by nearly three quarters.
Part one — the 2017 solution
01 The problem
Two conducting plates in a square box. One at +50 V, the other at −50 V, walls grounded, find the potential everywhere else.
With no charge in the gaps the potential obeys Laplace's equation, which on a grid reads directly: every free point is the average of its four neighbours. So I sweep the grid replacing each point with that average, and keep sweeping until nothing moves. The plates and the walls never take an average — they are the boundary, and the solution grows out from them.
02 The serial solver
Seventy lines of C. The grid is one flat array with an index function doing the arithmetic:
int transformer(int i, int j)
{
return i*m + j;
}
The boundary goes in once, and the sweep refuses to touch those two rows:
for(a = x0; a <= x1; a++)
{
array[transformer(y0, a)] = V0/2;
array[transformer(y1, a)] = -V0/2;
}
Geometry comes from five constants — box 5 m, plates 2 m long and 1 m apart, 100 V, step 0.02 m — with the plate coordinates computed rather than typed, so the shape changes in one place. This version works. Run it today and the gap field is 100 V/m against the 100 V/m the textbook predicts.
03 Going parallel
The second half of the assignment, and the more interesting code. The array is already flat, so the decomposition is a cut of it: each rank takes round(m*m/world_size) contiguous cells, plus a halo row offset by one row width, because averaging needs the rows above and below. Each rank relaxes its slab, then one collective call rebuilds the grid on every rank at once:
MPI_Allgather(V_new, a, MPI_DOUBLE, V1, a, MPI_DOUBLE, MPI_COMM_WORLD);
The part worth keeping is the boundary. The serial code protects the plates with a test inside the inner loop, which gets awkward once the array is in slabs — a rank would have to work out which of its cells are plate cells. So the parallel version re-stamps them at the top of every sweep instead: let the averaging damage the plates, then write them back before anyone reads them. No per-cell test, no ownership question, same answer.
04 Five defects
The architecture is sound. The C is not. Compiling it in 2026 gets as far as:
Entrega.c:160:12: error: expected ';' after expression
160 | V[i]=V1[i]
It never compiled. One missing semicolon, and the parallel deliverable could not have been run by anyone, including me. Behind it, four more:
| # | The code | What it does |
|---|---|---|
| 1 | V[i]=V1[i] | No semicolon. Hard compile error |
| 2 | n=+1; | Parses as n = +1, not n += 1. The counter sticks at 1 and the loop never ends |
| 3 | double h = 5/512; | Both operands int, so C divides in integer arithmetic to 0 and converts afterwards. Every plate coordinate then divides by zero |
| 4 | if(world_rank==world_size) | Ranks run 0 to world_size-1. Never true, so the last slab is never relaxed |
| 5 | if(!(world_rank==0 && world_rank==world_size)) | A rank cannot be both, so always true. Rank 0 runs two branches |
Two and three are the language being exactly as advertised. =+ is two tokens that look like one operator, and in 5/512 the type on the left of an assignment does not reach back into the expression on the right. Four and five are off-by-one in the rank arithmetic, the kind that only appears with more than two processors.
The serial version has two of its own. malloc instead of calloc, where the outer row and column are never written by the sweep — those cells are the grounded box, the answer depends on them being zero, and malloc leaves whatever the allocator last held. And init returns the pointer it is given, so V_new = init(..., V) made both names one array, turning the intended Jacobi iteration into Gauss–Seidel. That converges twice as fast, so the bug helped, which is why I never found it.
Part two — the rewrite
05 What changed
Not the method. The 2017 index arithmetic carries a -1 that puts the plates half a cell off centre, so the solution is not quite antisymmetric; an odd grid with a true centre row makes that error exactly zero. And the fixed count went: 2·m² sweeps is 125 000 at m = 250 because that is a number large enough to be safe. Stopping on the actual change per sweep gets the same answer in 32 813, and red-black over-relaxation in 689.
| Field, gap centre | 100.9 | V/m |
|---|---|---|
| Field, ideal V/d | 101.0 | V/m |
| Field, strongest cell | 180.8 | V/m |
| C′, from the grid | 2.998e-11 | F/m |
| C′, textbook ε₀l/d | 1.771e-11 | F/m |
| Edge excess | +69 | % |
06 What the formula leaves out
Every course gives the same two results: the field between the plates is the voltage over the separation, and the capacitance is ε₀ times area over separation. Both assume the field is uniform between the plates and zero everywhere else.
The first holds — 100.2 V/m against 100.0, with the potential ramping linearly across the gap. The second does not. Integrating the field energy over the box gives 3.06 × 10⁻¹¹ F/m where ε₀l/d gives 1.77 × 10⁻¹¹. The capacitor stores 73 % more than the formula says.
The difference is the field outside, which the derivation assumes away. In FIG. 1, past the plate ends the lines bow out and keep going, and all of it stores energy. For plates only twice as long as their separation, the part the formula discards is almost half the answer.
It is not wrong so much as asymptotic — exact for infinite plates. At l/d = 0.5 it is off by a factor of three, at l/d = 2 by 73 %, at l/d = 3.5 still by 54 %. For the long thin capacitors inside real circuits it is a good approximation. For the square-ish one on the blackboard it is not close. The curve stops at 3.5 because in a 5 m box, longer plates come near enough to the grounded walls that the walls start setting the answer.
07 The number that is not a number
The field is strongest at the plate tips and it is tempting to quote that. On the 2017 code's 250 × 250 grid the corner cell reads 183 V/m against 100 in the gap, which sounds like a result. It is not: refine the grid and it goes up, refine again and it goes up more.
The charge density at a sharp conducting edge genuinely diverges, so there is no finite tip field to converge to — what the solver prints is a property of the grid spacing. The capacitance, on the same runs, moves 0.6 % between the last two grids and stops. Nothing on the screen says which is which. You find out by refining and seeing which one holds still.
08 Heat, which was never wrong
The repository was called 2d-heat-equation-solver for nine years, which is how it stayed lost — I went looking for a capacitor.
The name was not a mistake. Relaxation is a diffusion process: run the heat equation on a plate with its edges held at fixed temperatures and it settles on exactly the field this solver returns. Steady-state heat and electrostatics are the same equation, and a Jacobi sweep is that equation taking a step in time. The name described the method; I had been reading it as the problem.
It is now 2d-field-solvers, and the next two make it literal: same stencil, same grid, one time derivative for heat and two for waves.
09 What it says
- The parallel-plate formula is off by 73 % for the geometry it is usually drawn with. The error is the field outside, which the derivation assumes away.
- A number that changes when you refine the grid is not a measurement. The tip field is one; the capacitance is not.
- The 2017 parallel code had the right architecture and did not compile. Only one of those was graded.
