The SIR-model in different modeling platforms

This page shall provide some basic ideas of how the SIR-model can be implemented in different modeling platforms. It is meant as a first lead for getting acquainted with these platforms.

The SIR-model with SIMULINK in MATLAB

MATLAB is currently one of the most frequently used modeling and simulation platforms. It is a proprietary software developed by MathWorks and widely used in science and engineering.

MATLAB includes SIMULINK which offers possibilities to generate models graphically with drag-and-drop options from a provided library.

Writing simulink into the MATLAB-command window followed by Enter brings up the Simulink Library Browser

Choose File > New > Model to open the Simulink-editor window

Drag and drop from the Simulink Library Browser to the empty editor window till you get the following model:


Double-clicking the blocks opens their parameter settings.

In the integrator-block S (indicated as \(\frac{1}{s}\)) set Initial condition to 100000 and State name to S

In the integrator-block I set Initial condition to 100 and State name to I

In the integrator-block R set Initial condition to 0 and State name to R

The connectors in the add-block are generated by specifying the sign + or the sign – at Main > List of signs

In the Save format-input of the three To workspace-blocks choose Array

At Simulation > Configuration Parameters set:

Start time: 0.0
Stop time: 4000.0
Solver: ode45 (Dormand-Prince)
Max step size: 10
Min step size: 1

and leave all the other inputs as they are.

Then go to the MATLAB-workspace again and put in the following parameter values:

>> beta=100;
>> mu=0.001;
>> gamma=0.4;
>> lambda=5e-6;

The inputs

>> plot(susc)
>> hold all
>> plot(infec)
>> hold all
>> plot(remov)

and >> plot(infec), and>> plot(susc,infec) generate the following three graphs:


Instead of the three To workspace-blocks select a Scope-block preceded by a Mux-block from the Simulink Library Browser. Set the Number of inputs in the Mux-block to 3 and connect the S, I and R-state-blocks to it as in the image below.
Running the simulation and subsequently clicking on the Scope-block generates the below plot immediately without returning to the workspace. (May be you have to click the ocular to focus the plot on the correct range)


The SIR-model as function in MATLAB

File > New > Function opens the Function editor. Write the following:


Into the command window write:

>> SIR_01(100000,100,0,100,0.4,0.000005,0.001,10000)

which yields:


The SIR-model with GRIND for MATLAB

GRIND is a freely available MATLAB-add-on for analyzing difference and differential equation systems, developed by Egbert van Nes of Wageningen University in the Netherlands.

Download GRIND here (last accessed 25.3.2014) and install it according to the documentation.

Writing model into the command window of MATLAB opens the GRIND model editor. Write the following into the Model equation window:

R(t+1)=R+(-mu * R + gamma * I)                    % recovered
I(t+1)=I+(-mu * I + lambda * S * I - gamma * I)   % infected
S(t+1)=S+(beta – mu * S – lambda * S * I)         % susceptible

Write the following into the Parameters / Initial conditions / Commands window:

beta = 100;
gamma = 0.4;
lambda = 5e-006;
mu = 0.001;

%%Initial values:
I = 100;
R = 0;
S = 100000;

simtime = 10000;

OK saves and creates the model.
Writing time into the command window of MATLAB yields:


The GRIND menu provides tools for analyzing. Clicking on run > forward for instance generates the following 3D phase space plot.


The SIR-model with Mathematica

Mathematica is a proprietary software platform by Wolfram-Research widely used in science and engineering.

Into the Mathematica-notebook write:


Pressing Shift together with Enter yields:


Into the Mathematica-notebook write:


Pressing Shift together with Enter yields:


The SIR-model in Python

Python is an open source programming language which currently seems on the way to become a standard in scientific computing. Information on how to install it and some suggestions for learning it are given here.

Into the Python-notebook write:

SIR in Python
SIR in Python - plot
SIR in Python - plot