🎓 A1Learner
🔍
0 of 47 lessons completed

Julia Simulation Project

A simulation steps a small model forward in fixed steps. Each step updates the counts from the counts of the step before.

One step updates all three counts

The run starts with 90 susceptible, 10 infected, and 0 recovered. One step gives the new counts.

function simulate(s, i, r, steps)
for step in 1:steps
newcases = min(s, div(i, 2))
recovered = div(i, 5)
s = s - newcases
i = i + newcases - recovered
r = r + recovered
end
return s, i, r
end
println(simulate(90, 10, 0, 1))

The total stays at 100 after three steps

Three steps move cases between groups. The sum of the three counts is still 100.

function simulate(s, i, r, steps)
for step in 1:steps
newcases = min(s, div(i, 2))
recovered = div(i, 5)
s = s - newcases
i = i + newcases - recovered
r = r + recovered
end
return s, i, r
end
counts = simulate(90, 10, 0, 3)
println(counts)
println(sum(counts))

A different start gives a different end

The same rules run from any starting counts. A start of 50, 20, and 0 runs for two steps.

function simulate(s, i, r, steps)
for step in 1:steps
newcases = min(s, div(i, 2))
recovered = div(i, 5)
s = s - newcases
i = i + newcases - recovered
r = r + recovered
end
return s, i, r
end
println(simulate(50, 20, 0, 2))

Example

function simulate(s, i, r, steps)
for step in 1:steps
newcases = min(s, div(i, 2))
recovered = div(i, 5)
s = s - newcases
i = i + newcases - recovered
r = r + recovered
end
return s, i, r
end
println(simulate(90, 10, 0, 1))
println(simulate(90, 10, 0, 2))
println(simulate(90, 10, 0, 3))

Key points

  • Step: a step is one fixed update of the model counts.
  • Susceptible: susceptible people are healthy people who can still catch the illness.
  • Infected: infected people are people who currently carry the illness.
  • Recovered: recovered people are people who have cleared the illness.
  • New cases: new cases move from susceptible to infected in a step.
  • Total: the total of the three counts stays the same across steps.

In this example, simulate runs the same epidemic rules for one step, then two steps, then three steps. Each printed triple shows susceptible, infected, and recovered in order. The counts move between groups while the total stays fixed.

Note

integer counts keep every step exact and easy to check.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

The run starts with 90 susceptible, 10 infected, and 0 recovered. What are the counts after three steps?