DOWHILE
This page runs a BASIC program that graphs S, the number of susceptible people, and keeps going until the epidemic reaches its peak. The program is rewritten in Python and runs right here in your browser. Edit the code and press Run to redraw the graph; Reset brings back the original program.
The calculation and the drawing are the same as in SIRPLOT: Euler’s method with deltat = 0.01, and LINE (t, S)-(t + deltat, S + deltaS) drawing one short segment per step. What changes is how the program decides when to stop. SIRPLOT used a FOR loop, so it ran a fixed number of steps. This program doesn’t know ahead of time how many steps it needs. Instead it uses DO WHILE Iprime > 0 ... LOOP, which repeats the loop for as long as the number of infected people is still increasing. When Iprime is no longer positive, I has reached its peak, the loop stops, and PRINT t reports when that happened.
Because the condition is checked at the top of the loop, Iprime has to be given a value before the loop starts, which is why it is calculated once on its own line before DO WHILE. WINDOW (0, 0)-(20, 47000) makes the screen wide and tall enough to hold the whole run: t from 0 to 20 days across and S from 0 to 47,000 up.
In the Python, while Iprime > 0: does the job of DO WHILE, and print(t) does the job of PRINT t; the printed time appears under the graph. You’ll see a long decimal rather than a tidy 12.28, because adding 0.01 over and over builds up tiny rounding errors. Try changing deltat to 0.1 or 0.001 to see how the peak time shifts, or change the 14 in the recovery rate. If you change the model so that Iprime never drops to 0, the loop never ends; the page stops any program that runs longer than five seconds.
Notes from the textbook
Add notes here.
1 DEFDBL A-Z
2 SCREEN 12
3 WINDOW (0, 0)-(20, 47000)
4 tinitial = 0
5 t = tinitial
6 S = 45400
7 I = 2100
8 R = 2500
9 Iprime = .00001 * S * I - I / 14
10 deltat = .01
11 DO WHILE Iprime > 0
12 Sprime = -.00001 * S * I
13 Iprime = .00001 * S * I - I / 14
14 Rprime = I / 14
15 deltaS = Sprime * deltat
16 deltaI = Iprime * deltat
17 deltaR = Rprime * deltat
18 LINE (t, S)-(t + deltat, S + deltaS)
19 t = t + deltat
20 S = S + deltaS
21 I = I + deltaI
22 R = R + deltaR
23 LOOP
24 PRINT t
Graph
Each line(...) draws one straight segment, like BASIC's LINE,
and window(...) sets the visible range, like WINDOW.
The loop stops when Iprime is no longer positive.
Hover over the graph to read coordinates.
Python code
Press Run (or Ctrl/⌘ + Enter) to redraw the graph. Press Esc, then Tab, to leave the editor.