Published
Edited
Sep 14, 2022
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
py`import sys
sys.version`
Insert cell
Insert cell
deps_installed = py`import micropip
await micropip.install("numpy")
await micropip.install("scipy")
"Done"`
Insert cell
Insert cell
sir = py`
# ${deps_installed}
import numpy as np
from scipy.integrate import solve_ivp

# Model definition
def model(t, u, p):
b,g = p
S,I,R = u
dS = -b*S*I
dI = b*S*I-g*I
dR = g*I
return [dS,dI,dR]

tspan = [0.0,40.0]
u0 = [999,1,0]
p = [${b},0.25]

sol = solve_ivp(lambda t, u: model(t, u, p),
tspan,
u0)

output = [{"Time":sol.t[i], "Value":sol.y[0][i], "Population":"S"} for i in range(len(sol.t))]
output += [{"Time":sol.t[i], "Value":sol.y[1][i], "Population":"I"} for i in range(len(sol.t))]
output += [{"Time":sol.t[i], "Value":sol.y[2][i], "Population":"R"} for i in range(len(sol.t))]
output
`
Insert cell
Insert cell
viewof b = Inputs.range([0, 0.001], {step: 0.0001, label: "Infectivity parameter b"})
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.line(sir, {x: "Time", y: "Value", curve: "catmull-rom", stroke: "Population"})
],
color: {
type: "ordinal",
scheme: "category10",
legend: "ramp",
}
})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more