Solving the SIR model's equation using Euler's method
4 min read

Solving the SIR model's equation using Euler's method

What is the SIR model?

The SIR model is a compartmental model used to model the propagation of a virus. There are other compartmental models (SEIR for example) but SIR is the most basic one. But what is it actually?
This model supposes there are three "types" of person, first of all, there are the susceptible people, the infected people, and finally the people who recovered (hence the name SIR).
Each type of person is linked to the other type of person through an equation, which says that the more infected people there are, the more people will get infected, for example. In this model, once a person has recovered, they cannot be re-infected.

A representation of the SIR model

We also have to introduce two constants: β and γ, which are respectively the infectivity rate and the recovery rate. Those two constants will show how fast the virus spreads and how fast someone can recover from it.
Next up, let's talk about the equations: the equations don't actually express the number of people in each compartment but instead they express the rate of change of people in the compartment:

Now the goal would be to solve those differential equations, given an initial population (number of susceptible people), which we would call S(0), an initial infected population, called I(0), and an initial number of people who recovered, R(0)=0 (because no one can recover if no one has the virus in the first place), we would like to express S(t), I(t), and R(t) and trace a graph of those three functions.

The problem is, that when we look into it, those equations are very hard to solve analytically and we won't be able to get an explicit form of those S, I, and R functions. One possibility is to use Euler's method to get numerical approximations of those functions and graph them without actually solving them.

What is Euler's method?

I have an article that already explains what Euler's method is, but for those not interested in reading it, here is a small explanation: Euler's method lets you rewrite differential equations to use previous values of a function to predict the next values, using its derivative. To analytically implement it, you have to rewrite differential equations in a way to rewrite derivatives:

Then the next step would be to isolate the f(x+dx) term in each equation. Let's actually go over this method with the SIR model's equations.

Using Euler's method to solve the SIR equations

First of all, let's rewrite the equations:

Then, for each equation, we'll isolate the (t+dt) term :

Next, we have to ask ourselves, what value will we choose for S(0), I(0), R(0), and dt ?
Well, this is pretty arbitrary, but let's say we have an initial population of 99 and one infected person (I(0)=1). For dt, we'll choose 0.1 as that denotes the time jump between each point measured, it is arbitrary as well.

Finally, for β and γ we'll take respectively 0.01 and 0.05 (this can be changed to whatever, but in my experience those values yield a good graph for showing results).


I'm going to jump straight into the results but the idea is to have the initial point and then calculate each subsequent point and store it in a list to graph it as a function of time. Of you're interested in the details, download the code below:

graph for the propagation of a virus, the infected are in yellow

To conclude, we were able to graph the solutions of differential equations without ever solving them, which is definitely very cool!
Consider subscribing to my newsletter, it's free and keeps me motivated!