Modelling Diffusion.

In the animation above a darker color means a higher concentration, C, of some substance mixed or dissolved in some other substance, presumably liquid. C is a function of two variables, the horizontal position x, and time t. In this simulation the x-axis is divided into 96 separate positions. That is, there are 96 values for C(x,t) at any given time. Let Dx be the distance between these discrete x-values. Let Dt be the time interval used by the simulation. Ok, the problem we want to solve initially is this: given all the 96 values of C(x,t) at some time t, what are the 96 values of C(x,t+Dt) at the next interval? (Note: on my machine it takes at least 20 minutes for the digital stuff in the simulation to diffuse fairly evenly from left to right; it's up to you to decide if you want to wait it out; but then, have you something better to do?)
Let's look at an arbitrary value of x, assumed to be internal, ie., not on either end. What can happen to the concentration C(x,t) as t increases by Dt? Well...
  • Some of C(x,t) might randomly jump to the left, decreasing C at position x;
  • Some of C(x,t) might randomly jump to the right, decreasing C at position x;
That is, some of C(x,t) may diffuse away in the time interval Dt. However,...
  • Some of C(x+Dx,t) might randomly jump to the left into position x, increasing C at x;
  • Some of C(x-Dx,t) might randomly jump to the right into position x, increasing C at x.
We'll assume the probability of these random jumps (diffusions) of our substance are position and direction independent (not always the case). Let p be the average proportion of C that jumps left or right at each x over any time interval Dt. This gives us an answer to our problem:
C(x,t+Dt) = C(x,t) - pC(x,t) - pC(x,t) + pC(x+Dx,t) + pC(x-Dx,t)
The five terms on the righthand side are: what we started with; what diffuses to the left; what diffuses to the right; what diffuses from the right; what diffuses from the left. (From this the reader should be able to figure out how the simulator up top works.) Rearranging terms, this can be rewritten:
C(x,t+Dt) - C(x,t) = p( [C(x+Dx,t) - C(x,t)] - [C(x,t) - C(x-Dx,t)] ).
Without going into too much mathematical detail, if we Taylor expand the three values, C(x,t+Dt), C(x+Dx,t), and C(x-Dx,t), and divide both sides by Dt, this equation reduces to:
Ct(x,t) +... = [pDx2/ Dt] Cxx(x,t) + ...
where the "+..." indicates further terms in the Taylor expansion, and Ct(x,t) is the partial derivative of C with respect to t, and Cxx(x,t) is the second partial derivative of C with respect to x. Now we let Dt and Dx go to zero in such a way that the terms in the brackets converge to some constant K, and all the other terms (+...) reduce to zero. This yields:
Ct(x,t) = KCxx(x,t),
which is the Diffusion Equation in one space dimension. In three dimensions we'd have:
Ct(x,y,z,t) = K(Cxx(x,y,z,t) + Cyy(x,y,z,t) + Czz(x,y,z,t)).
Home.