Why is the graph of charge distribution at a PN junction rectangular? Like the one on this Wikipedia page, all of the diagrams I've seen so far seem to show a rectangular graph of charge density at a PN junction. I'd like to understand why.
I tried to simulate a process where I iteratively diffused the free charge carriers, and then recombined the overlapping charge densities of positive and negative carriers. While I do get a sharp transition in the middle, I don't get the sharp transitions at the edges of the depletion region. My idea is that I've incorrectly modeled diffusion, as I've just used a double moving average filter.
I'm attaching the generated graphs, where each row represents a step (either diffusion or recombination), while the columns represent (from left to right) static charge, the charge of free carriers and total charge.

What am I doing wrong?
 A: There are a few mistakes in this simulation

*

*When carriers recombine you lose one majority and one minority carrier. They recombine and are gone. The process is not converting majority carriers to minority carriers. (Although when majority carriers diffuse across the junction they do become excess minority carriers.)

*You should start with many more majority carriers than minority carriers on each side of the junction. You want at least 10000x more majority than minority. (That is what makes them majority carriers)

*You don't seem to be accounting for the ionized donors and acceptors. These are fixed charges that do not move. These are typically very close to equal to the number of majority carriers in equilibrium, but of the opposite charge.

*You need to track "holes" and "electrons" and not "majority" and "minority" carriers. As diffusion occurs across the junction you will at first have excess holes on the n side and excess electrons on the p side. It is the electrons and holes that diffuse. The majority/minority distinction is a label we apply based on where the carrier currently is. It is not an intrinsic property of the charge carrier.

*If you don't account for the electric field in the depletion region you will never get the rectangular charge profiles you seek.

