# Load the deSolve package
library(deSolve)
# Define the SIR model
<- function(time, state, parameters) {
sir_model with(as.list(c(state, parameters)), {
<- -beta * S * I
dS <- beta * S * I - gamma * I
dI <- gamma * I
dR return(list(c(dS, dI, dR)))
})
}
# Initial state values
<- c(S = 0.99, I = 0.01, R = 0.0)
initial_state
# Parameters
<- c(beta = 0.3, gamma = 0.1)
parameters
# Time points
<- seq(0, 1600, by = 1)
times
# Solve the system of ODEs
<- Sys.time()
start_time for(i in 1:100) {
<- ode(y = initial_state, times = times, func = sir_model, parms = parameters)
output
}<- Sys.time()
end_time <- end_time - start_time
r_runtime
print("Runtime for deSolve in R:")
[1] "Runtime for deSolve in R:"
print(r_runtime)
Time difference of 1.270186 secs