""" Example code from lecture 5/11. The simplest version of the SIR model class, with lifelong immunity (i.e., no flux of people from R back to S) Implemented as a class, which makes it easy to run the model for different choices of parameters beta and nu. """ from ODESolver import RungeKutta4 import matplotlib.pyplot as plt class SIR: def __init__(self, beta = 1.44, nu = 0.2): self.beta = beta self.nu = nu def __call__(self, t, u): beta = self.beta nu = self.nu S, I, R = u[0], u[1], u[2] N = S + I + R dS = -beta * S * I / N dI = beta * S * I / N - nu * I dR = nu * I return [dS, dI, dR] model = SIR() 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.show()