Jupyter notebook 05 - random walks - final.ipynb
Simulating particle diffusion in different temperatures
Diffusion of particles is a random process, which is caused by the random motion of air or water molecules. Diffusion can be modeled using a random walk. Random walk describes the trajectory of a particle in subsequent timesteps, where the particle moves in a random direction and distance until bumping into another particle.
Diffusion coefficient plays an important role in the speed of particle diffusion, and it depends on several properties of a liquid or a gas, including the temperature and the density. Choosing a larger diffusion coefficient would then mean we could simulate the higher temperature.
Importing necessary libraries
Simulation of a random walk (one particle)
The first assignment is to simulate a random walk diffusion process of a single particle moving in one dimension (1D).
We need to decide how many timesteps we will allow the particle to move and what is the diffusion coefficient. We also need to define the starting position of the particle, for example the x position = 0.
For every new timestep, we will allow the particle to move "randomly" in 1D space, meaning either left or right from the current position, and by a normally distributed distance (following the normal distribution). If the particle moves +10, it will find itself on a position x = 10. If the particle next time moves -3, it would end up at a position x = 7.
You should try this same code several times, and you will see that the graph you generated is always unique!
This shows you that simulation of random walk of particles always returns different, random results.
Simulation of a random walk (many particles)
Let's try to simulate many particles diffusing in 1D space, in the same way as we did for one particle.
We now see what would happen with the random walk diffusion of many particles. It is very hard to follow and analyse all the trajectories, but we can see that they are going on average further and further away from the initial starting position.
Representing the diffusion of particles in the above example using histograms
To analyse this process of diffusion, we can make a histogram at different timesteps, to see more easily where are the particles located.
We can choose several timesteps, and see how much did the particles move away from the initial position.
If we want to compare the position of particles at 3 different timesteps, we can plot 3 histograms with the same shared axes
What we can see is that the particles diffuse in both directions, and that over time the peak of the histogram flattens.
This means that with time the particles tend to diffuse towards uniform distribution in our 1D space.
What you should try to do now is to change the parameters (number of particles and diffusion coefficient) and see how this will affect the speed of diffusion. Generate histograms at the same timesteps to be able to visually compare the differences in diffusion.