Double-Slit Experiment Simulation

This post is about simulating the double-slit experiment using a water tank simulation in Matlab. The simulation is really used to create a rain drop animation, but I modified it slightly to do the double-slit experiment.

In the video I show the rain drop simulation, the double-slit and an interesting case where I let a drop pulsate and reflect off circles. The waves behave much like light, behind the circles we have shadows. It makes for cool images any way:

The reason for doing this project was to attempt to modify a very simple algorithm and get something quite complex out of it. I started with a droplet simulation described:

The reason I thought of the double-slit experiment is because light is thought of as particles and waves (like drops in a water tank). I have previously created a post on how I create a 1-dimensional wave using vertical springs, check it out: This project is in 2 dimensions and it is actually much simpler to implement than my spring solution.

The program starts off with an empty matrix, and like the game of life, if things happens on the board, the surrounding cells will be affected. The update stage is done to a temporary matrix and this is copied over the primary, just like in the game of life.

The droplet code create waves that propagates through the medium in all directions. The waves are odd, because they are discretized. So I added a smoothing filter to the matrix, just for visualization.

The beauty of the code is that we don’t use any ray tracing or describe any circles, it’s all embedded in the way the matrices are used.

In the double-slit experiment we use a laser beam, a double-slit plate and a wall at a certain distance from the plate. The laser beam illuminates the plate and because of the wave-particle nature of light, the light waves from each slit will spread and interfere with itself forming interference patterns on the wall. Light will interact and cancel out itself.

One way to demonstrate this effect is to use a water tank, a double-slit and a plane wave. In order to get a plane wave from this experiment I thought that a plane wave bascially is a train of drops.

In my simulation the next to last column of the matrix is updated using a line controlled by a cosine. It is simply constantly creating plane waves.

To create the double slit I simply set the matrix elements to zero for each iteration. I use a mask matrix to do this. I have defined the intervals of the slits in slit1 and slit2:

mask         = ones(n, n); %keep all elements

mask(1:slit1(2), slit1(1)) = 0;
mask(slit1(2)+slit1(3):slit2(2), slit1(1)) = 0;
mask((slit2(2)+slit2(3)):end, slit2(1)) = 0;

We also need to add a boundary, otherwise we will get weird reflections:

mask(:, 1)    = 0;
mask(:, end ) = 0;
mask(1, : )   = 0;
mask(end, : ) = 0;

The plane waves from the right were created using

Buffer1(1:n, end-1) = 5*cos(iteration/2);

where iteration is the step in the animation. I then update the matrix and switch between the buffers just like in the game of life. I also had to apply a gaussian filter for the visualization:

filter = fspecial('gaussian', 2, 1);

For each iteration, after the generation of the wave I apply the mask:

Buffer1 = Buffer1.*mask;

The update routine:

    for x = 2:n-2
        for y = 2:n-1

            Buffer2(x, y) = 0.5*( Buffer1(x-1, y) + Buffer1(x+1, y) +...
                Buffer1(x, y+1) + Buffer1(x, y-1) )...
                - Buffer2(x, y);
            Buffer2(x, y) = Buffer2(x, y) * damping;


tmp = imfilter(Buffer2, filter, 'replicate');


%switch buffers
    tmp     = Buffer1;
    Buffer1 = Buffer2;
    Buffer2 = tmp;

Damping is 0.99 in all my visualizations.

See more about the experiment:


  1. Hi,
    Is your website still active and do you code in Matlab for Project Euler?
    – Michael

    • Hi Michael, I’ve been busy with work so I ‘ve simply neglected my website, but I have a few projects that I would like to research/write about. For instance, I would like to try Deep Learning with Tensorflow, VR in Unity and sprite drawing in WebGL, the latter is something I’m experimenting on now.

      As for your question about Euler, I know about it, but again, I’ve been too busy, it’s pretty cool though. Do you use Matlab or do you have any other language you feel at home with?

      • At the moment I only code in Matlab (I’ve been told it’s not the best suited for Project Euler) but your website seems like it could teach me a lot and all the topics you mentioned above are super interesting to me. If you do decide to post more I’ll definitely keep an eye out for it 😉 In general, I’m trying to learn more about Matlab’s other uses since I’ve just been practicing syntax and dynamic programming so far. I’d love to learn more about how you started some of your particular projects and the best resources to do so, however if your super busy I completely understand.

        • Hi Michael. Nice you like the site, I appreciate it. Matlab is good, I enjoy prototyping and solve simpler problems using Matlab, but I quite enjoy using Python as well. So, I would suggest taking a look at Python too, because it is free and it is quite popular, especially in my field of media/3d (3d Studio Max, Maya, ZBrush, VRed, Deadline to name a few). Also, because I will write about Python in the near future (tensor flow: deep learning).

        • The project ideas comes from labs I did at the University (taxi cab simulation for instance) and other things I just researched for a course (valentine bunnies) and some free lancing (how to train your quadcopter). So, a tip would be to play with the code, look online for popular algorithms (BOIDS, knapsack problem, travelling salesman,…), and just have fun taking those steps into a larger world 😉

Leave a Reply

Your email address will not be published. Required fields are marked *