""" Example code from lecture 5/11. Extended version of the SIR class, without lifelong immunity. This means two extra terms in the equation, that describe a reduction in R and a corresponding increase in S over time. The rate of the flux from R to S is described by the parameter gamma. """ from ODESolver import RungeKutta4 import matplotlib.pyplot as plt class SIR_v2: def __init__(self, beta = 1.44, nu = 0.2, gamma = 0.02): self.beta = beta self.nu = nu self.gamma = gamma def __call__(self, t, u): beta = self.beta nu = self.nu gamma = self.gamma S, I, R = u[0], u[1], u[2] N = S + I + R dS = -beta * S * I / N + gamma * R dI = beta * S * I / N - nu * I dR = nu * I - gamma * R return [dS, dI, dR] model = SIR_v2() solver = RungeKutta4(model) T_span = (0, 30) steps = 300 u0 = [50, 1, 0] solver.set_initial_condition(u0) t, u = solver.solve(T_span, steps) plt.plot(t, u) plt.legend(['S', 'I', 'R']) plt.show()