File Exchange

image thumbnail

Simulitis (A Coronavirus Simulation)

version 1.1.2 (6.22 KB) by Joshua Gafford
MATLAB code to replicate Washington Post's Coronavirus simulation


Updated 11 Apr 2020

View Version History

View License

I'm sure by now you've all seen the Washington Post article with the COVID-19 simulation, speaking to the public health benefits of social isolation in the face of a highly infectious disease. If you haven't, here's the article:

Well, out of my quarantine boredom, I recreated this simulation in MATLAB, with more knobs to turn and parameters to play around with. For example, what if some of the carriers can die? What if the disease is Ebola instead (~50% mortality rate)?

This function simulates disease transmission among a set of n carriers, in a confined space, with some proportion of carriers socially isolating themselves. A simple multibody physics model (elastic collision between two equal-mass particles) determines carrier trajectory.

Hopefully this gives you a fun (albeit brief) respite from the doldrums of social isolation!

Cite As

Joshua Gafford (2021). Simulitis (A Coronavirus Simulation) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (35)

Miguel L.

Joshua Gafford

Hi Angelo, look for the block of code under the title 'MODEL INPUTS.' You can change any of those parameters.

Angelo Garcia

Thanks for creating this Joshua. I am a High School student and I want to use your simulation for a project on COVID-19 to explore different scenarios. I downloaded a trial offer for Matlab and ran the Simulitis code but can't seem to control change parameters. I love the simulation but for me to use this in my project I will need to be able to play with parameters to simulate different scenarios. Where should I go for help with this? Is the code you put up on this site the one with the controls? Thanks!

Mohand Oukaci

i am not getting any error and video_save = true
i will check again

Joshua Gafford

Hi Mohand,

I'm not sure, I just tested it and the video appears to save correctly for me. Have you made sure to set video_save = true? Are you getting any errors?

Mohand Oukaci

Hi Joshua,
For whatever reason in the new version the video is not saved.
Checked and everything seemed fine but still video not saved.
Any clue?

Mohand Oukaci

Joshua, Your changes are very much appreciated. Thanks a lot

Vira Roy

Thanks Joshua for the modification. Of course you were modelling the Washington post article, but seeing the code is so compact and beautiful, i might humbly suggest two simple modifications to make it closer to real world.

1. We see lots of patients test positive after getting recovered and this is a major issue in tackling this pandemic. So a modification in the sense that few recovered carriers(blue ones) turn into infected ones (red ones). I did that by assigning a finite probability of re-infection and later comparing with recovered ones.

2. Lots of patients are asymptomatic carriers, i.e they don't show symptoms yet can infect others. I tried modelling this by introducing a new carrier.

While I could do the first one, the second one is eluding me. Would it be possible to modify with these modifications.

Thanks anyways. The code is as i said, compact and beautiful.

Joshua Gafford

Hi Vira, I have added the ability to simulate 4-quadrant quarantine with a horizontal wall. Setting the parameter 'w_loc_h' determines the placement of this wall (as a fraction of the total spatial limit).

Hi Mohand, I have replaced the title string with the real-time stats as you've suggested. I can't think of a great way to run three simulations at once, aside from running three separate simulations and comparing them after the fact, or using an external script to run three in parallel. I'll think about this a bit more.

Mohand Oukaci

Hi Joshua,
I tried to do two things but I am not comfortble enough in Matlabe to do it.
1) Have a boz to display the numbers that are shown in each of the three curves, basically like a counter on the top of the Figure with Unaffected xx, Infected xx, Recovered xx, Deceased xx
2) Suppose someone build three scenarios. s there a way to show them at the same time on the same page Video1, Video2, Video3
Thnaks for your extraordinary work

Vira Roy

wonderful Code Sir. I don't know if this is a correct place to ask you but i was playing with your code to try few different things. I wanted to add a horizontal wall along with the vertical one so as to model the system as four different quarantined region and see the effect of different community under quarantine. Could you may be help how to achieve that? Or would it be okay to ask this as question on mathworks forum.

Any hint would also be fine. Thank you.

Mohand Oukaci

Joshua Gafford

Hi Mohand, good suggestion, I just added the ability to stop the gate from fully opening. Setting the 'w_open' parameter to a value between 0 and 1 determines the final gate opening width (as a fraction of the total width).

Mohand Oukaci

Wonderful application
Can I keep the gate open at some level not go to wide open


Joshua Gafford

@ James - you're right! Figured the refreshdata calls were necessary to push the new data to the axes handles but I guess drawnow takes care of that like you said. Runs much faster now!

James Smith


Do you need those two refreshdata calls? They eat up a lot of time. I commented them out and it seems to run just fine and a whole lot faster ... I think the "drawnow" takes care of updating the figure.

Mostafa Essuri

@Joshua , Thanks.
I'm checking it now.

Tsotne Marghia

@Joshua: Thank you very much for such quick responses. Just to note: it might be a good idea to turn a direction trajectory into a random walk, so on each step it will choose a new direction vector cos(2*pi*rand).

Joshua Gafford

@ mike: looks great and ran perfectly for me!

Joshua Gafford

@ Tsotne: you are absolutely right. Modified the code so the initial infection rate is only applied to those behind (to the left of) the quarantine wall. Thanks!

Sophie Kargo

Tsotne Marghia

@Joshua: Thank you for your response and update of the codes. However, once I ran them, I noticed that some infected are on both side of the gate. I thought that the whole idea of quarantine gate was to keep infected ones separately in the quarantine zone behind the gates.

mike fienen

@Joshua: here's the python code. Keen on any feedback.

Joshua Gafford

@ mike: awesome! would love to see the code if you happen to have a github repository

@ Mostafa: Just added quarantine gate functionality, check it out. Probably not the most robust/efficient way since I was strapped for time, but it works. You can set the location of the quarantine gate through w_loc, and control the opening time and opening speed through w_init and w_speed, respectively.

mike fienen

This is super cool! Thanks for posting. I took the liberty of translating to Python (with attribution). Really timely work and nicely coded. Thanks for doing it!

Mostafa Essuri

Thanks for your work.
How to include a simulation with opened gate during simulation.
Similar to that in the article of weshenton post

Joshua Gafford

Hello, the spatial limits variable 'lim' just refers to the 'size' of the 2D arena in nondimensional units. So lim = 200 generates an arena of 200 x 200. The carriers variable ('n') refers to the total number of particles (people), and you can modify the fraction initially infected by playing around with 'p_init'.

Tsotne Marghia

Hello, thank you very much for your work.
I'm interested in how to decide the number for spatial limits correctly. And by carriers do you mean the whole population or only the fraction that is infected?
Thank you

Joshua Gafford

Thanks for reporting this. I suppose there was a problem with the vector size in that element-wise operation since v is [n x 2] and isolate is [n x 1] (my MATLAB version I guess automatically assumes row-wise element multiplication if the column sizes differ, but maybe older versions don't?). Anyways it should work for you now.


Sorry I was commenting and accidentally pressed enter. Im having this error
Error using .*
Matrix dimensions must agree.
Error in simulitis (line 131)
pos_new = pos+v.*(~isolate).*delT;

Joshua Gafford

Hi Abigail, what problems are you having? Any errors?

Abigail Jimenez

It has some bugs... I cannot run it.

E. Cheynet

Stephen Cobeldick

Neatly written code, good help, easy to use... and most timely: thank you :)

MATLAB Release Compatibility
Created with R2018b
Compatible with R2018b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!