Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

instead of fixed beta and gama, i want to implement the formula likes this beta = ( Number of New Infections per Unit Time

instead of fixed beta and gama, i want to implement the formula likes this \beta =(Number of New Infections per Unit Time)/(Number of Susceptible Individuals)\times (Number of Infectious Individuals),
\gamma =(Number of Recoveries per Unit Time)/(Number of Infectious Individuals),import numpy as np
import matplotlib.pyplot as plt
# Parameters
beta =0.222 # transmission rate
gamma =0.122 # recovery rate
# Initial conditions
initial_infectious = infectious_data.iloc[0]
initial_susceptible = susceptible_data.iloc[0]
initial_recoveries = recoveries_data.iloc[0]
# Time settings
num_days = len(dates)
h =10.5 # time step
# SIR model differential equations
def sir_model(t, y, beta, gamma):
S, I, R = y
dSdt =-beta * S * I / population
dIdt = beta * S * I / population - gamma * I
dRdt = gamma * I
return [dSdt, dIdt, dRdt]
# RK-4 method
def rk4_method(t, y, h, beta, gamma):
k1= np.array(sir_model(t, y, beta, gamma))
k2= np.array(sir_model(t + h/2, y + h/2* k1, beta, gamma))
k3= np.array(sir_model(t + h/2, y + h/2* k2, beta, gamma))
k4= np.array(sir_model(t + h, y + h * k3, beta, gamma))
return y + h/6*(k1+2*k2+2*k3+ k4)
# Heun's method
def heun_method(t, y, h, beta, gamma):
y_predictor = y + h * np.array(sir_model(t, y, beta, gamma))
y_corrector = y + h/2*(np.array(sir_model(t, y, beta, gamma))+ np.array(sir_model(t + h, y_predictor, beta, gamma)))
return y_corrector
# Initial conditions vector
initial_conditions_rk4=[initial_susceptible, initial_infectious, initial_recoveries]
initial_conditions_heun =[initial_susceptible, initial_infectious, initial_recoveries +10] # Adding 10 to make initial conditions different
# Initialize arrays to store results
result_rk4= np.zeros((num_days, 3))
result_heun = np.zeros((num_days, 3))
# Set initial conditions
result_rk4[0, :]= initial_conditions_rk4
result_heun[0, :]= initial_conditions_heun
# Time integration loop
for i in range(1, num_days):
t = i * h
result_rk4[i, :]= rk4_method(t, result_rk4[i-1, :], h, beta, gamma)
result_heun[i, :]= heun_method(t, result_heun[i-1, :], h, beta, gamma)
print(f"Day {i}: RK4={result_rk4[i, :]}, Heun ={result_heun[i, :]}")
# Plot results for RK-4
dates = np.arange(1, len(result_rk4)+1) # result_rk4 contains data for each day
plt.figure(figsize=(10,6))
plt.plot(dates, result_rk4[:,0], label='Susceptible (RK-4)')
plt.plot(dates, result_rk4[:,1], label='Infectious (RK-4)')
plt.plot(dates, result_rk4[:,2], label='Recovered (RK-4)')
# Plot results for Heun
dates = np.arange(1, len(result_heun)+1) #result_heun contains data for each day
plt.plot(dates, result_heun[:,0], label='Susceptible (Heun)')
plt.plot(dates, result_heun[:,1], label='Infectious (Heun)')
plt.plot(dates, result_heun[:,2], label='Recovered (Heun)')
# Customize plot
plt.xlabel('Days')
plt.ylabel('Number of Cases')
plt.title('SIR Model with RK-4 and Heun Methods')
plt.legend(loc='upper right')
plt.show()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions