<a href="https://colab.research.google.com/github/john-s-butler-dit/CaseStudy_PredatorPrey/blob/master/SIR_Model.ipynb" target="_parent"></a>

# SIR Differential Equations


In this notebook we will model the spread of disease using the Kermack-McKendrick Model model which is a compartment model.


```python
## Library
import numpy as np
import math 
import matplotlib.pyplot as plt
```

The model is a system of three non-linear first order differential equations:
\begin{equation}
\begin{array}{cl}
\frac{dS}{dt}=-\frac{\beta IS}{N},\\
\frac{dI}{dt}=\frac{\beta IS}{N}-\gamma I,\\
\frac{dR}{dt}=\gamma I
\end{array}
\end{equation}
where 
S is the people who are susceptible in the population, 
I is the number of people infected, 
R is the number of removed population (either by death or recovery), and 
N is the sum of these three $$ N=S(t_0)+I(t_0)+R(t_0).$$
$\beta$ is the infection rate, and $\gamma$ is the recovery rate.
$$ R_{0}={\frac{\beta }{\gamma }},$$

the so-called basic reproduction number (also called basic reproduction ratio).



```python
beta=1
gamma=1

```

## Discrete Domain 
To numiercally approximate the solution we discretise the continuous domain using a step size, $h=0.001$, 
which gives
\begin{equation}
t_i=0+i h,
\end{equation}
for $i=0,...n$.
The figure below illustrate the discrete domain.


```python
h=0.001
t=np.arange(0,10+h/2,h)
fig = plt.figure(1,figsize=(5,4))
ax = fig.add_subplot(1,1,1)
ax.plot(t,0*t,'o:',color='k')
ax.set_xlabel('Time (yrs)')
plt.title('Discrete Time Domain')
plt.show()
```


```python
n=len(t) # Number of Steps
S=np.zeros(n) # Numerical Solution Hares
S[0]=99 # Intial Hare population
I=np.zeros(n) # Numerical Solution Lynxes
I[0]=1# Initial Lynx population
R=np.zeros(n) # Numerical Solution Lynxes
R[0]=0# Initial Lynx population

N=100

```

We numerically approximate the differential equation using Eulers method to give the difference equation
\begin{equation}
S[i+1] = S[i]+ h*\big(-beta*S[i]*I[i]/N \big)
\end{equation}

\begin{equation}
I[i+1] = I[i]+ h*\big(\beta*S[i]*I[i] /N- gamma I[i] \big)
\end{equation}

\begin{equation}
R[i+1] = R[i]+ h*\big(gamma I[i] \big)
\end{equation}


where $h$ is the stepsize and  $H[i]$ and $L[i]$ is the numerical approximation of the susceptible $S(t_i)$, infected $I(t_i)$  and recovered $R(t_i)$ at time $t_i$, for $i=0,...,n$



```python
for i in range (0,n-1):
    S[i+1]=S[i]+h*(-beta*S[i]*I[i]/N) 
    I[i+1]=I[i]+h*(beta*S[i]*I[i]/N-gamma*I[i])# INSERT EQUATION HERE 
    R[i+1]=R[i]+h*gamma*I[i]

```

## Results



```python
## Plotting Figure
fig = plt.figure(1,figsize=(10,4))

ax = fig.add_subplot(1,1,1)

ax.plot(t,S,':',color='green',label='Sus')
ax.plot(t,I,':',color='red',label='Inf')
ax.plot(t,R,':',color='blue',label='Rec')


ax.legend()
ax.set_xlabel('Time (yrs)')
ax.set_ylabel('Population (tens of thousands)')
#ax.legend(loc='best')
plt.tight_layout()
plt.subplots_adjust(top=0.85)
plt.show()

```

## Phase Plane Plot



```python
## Plotting Figure
fig = plt.figure(1,figsize=(5,5))

ax = fig.add_subplot(1,1,1)

ax.plot(I,S,'-',color='black',label='I vs S')
ax.plot(I,R,'-',color='red',label='I vs R')


ax.set_xlabel('Infected Population (tens of thousands)')
ax.set_ylabel('Population (tens of thousands)')
ax.legend(loc='best')
plt.tight_layout()
plt.ylim(0,110)
plt.xlim(0,110)

plt.show()
```

# References

[1] Weisstein, Eric W. "Kermack-McKendrick Model." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/Kermack-McKendrickModel.html

[2]  Wikipedia contributors. (2021, February 28). Compartmental models in epidemiology. In Wikipedia, The Free Encyclopedia. Retrieved 18:12, March 15, 2021, from https://en.wikipedia.org/w/index.php?title=Compartmental_models_in_epidemiology&oldid=1009414539



```python

```

340e694b4d13353e979834ceb0b32cd6ec44dccc
ipynb
Jupyter Notebook
SIR_Model.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
SIR_Model.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
SIR_Model.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
Qwen/Qwen-72B
1. YES
2. YES