I am implementing a fourth order Runge Kutta method to solve a system of three ODEs that describe the SIR model. Based on other simulations, I would expect a different solution (mine diverges very quickly). Here is my code for the runge kutta method:
def rk4(f, s0, x0, xf, n):
x = np.linspace(x0, xf, n+1) #x grid
s = np.array((n+1)*[s0]) #array of the state of the sytem for each x
h = x[1] - x[0] #stepsize
for i in range(n): #Fourth Order Runge Kutta Method
k0 = h * f(s[i])
k1= h * f(s[i] + 0.5 * k0)
k2 = h * f(s[i] + 0.5 * k1)
k3 = h * f(s[i] + k2)
s[i+1] = s[i] + (k0 + 2*(k1 + k2) + k3) / 6.
return x, s
And here is the implementation for the SIR model:
def f(u):
dS = -a*u[0]*u[1] #dY/dt = -aS(t)I(t)
dI = a*u[0]*u[1] - b*u[1] #dI/dt = aS(t)I(t)-bI(t)
dR = b*u[1] #dR/dt = bI(t)
return np.array([dS, dI, dR])
for:
a = 0.2
b = 0.1
x, s = rk4(f, np.array([235.0, 14.0, 0.0]), 0., 109., 109000)
the solution I get is this although I expected to approach these data. What am I doing wrong?
Thank you in advance!