Created
June 15, 2020 21:24
-
-
Save sdwfrost/5f36f8541671b3cdb8c869a07d645684 to your computer and use it in GitHub Desktop.
MTK version of SIR model using rates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Libraries | |
using ModelingToolkit | |
using OrdinaryDiffEq | |
# Define size of the system | |
nstates = 3 # S,I,R | |
nrates = 2 # infection, recovery | |
# Model specific parameters | |
@parameters β c γ S₀ I₀ R₀ | |
# Define time | |
@parameters t | |
# Rates | |
@variables N[1:nrates](t) | |
# Model is with respect to time | |
@derivatives D'~t | |
# Stoichiometry matrix | |
A = [[-1 1 0]; | |
[0 -1 1]] | |
# Derived variables | |
x₀ = [S₀, I₀, R₀] | |
X = x₀ .+ A'N | |
# Transition rates | |
(S,I,R) = X | |
λ = [β*c*I*S/(S+I+R),γ*I] | |
# ODE for rates | |
neqs = D.(N) .~ λ | |
tspan = (0.0,40.0) | |
nu0 = [λ[1] => 0.0, λ[2] => 0.0] | |
p = [β => 0.05, | |
c => 10.0, | |
γ => 0.25, | |
S₀ => 990.0, | |
I₀ => 10.0, | |
R₀ => 0.0] | |
nsys = ODESystem(neqs) | |
nprob = ODEProblem(nsys,nu0,tspan,p) | |
nsol = solve(nprob,Tsit5(),saveat=1.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment