SIRPLOT
This page runs a BASIC program that graphs S, the number of susceptible people, over three days. 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 is the same Euler’s method as in SIRVALUE, now with tfinal = 3 and numberofsteps = 30, so deltat = 0.1. The new part is the drawing. On each pass through the loop, LINE (t, S)-(t + deltat, S + deltaS) draws a straight segment from the current point to the next one, and together the 30 segments form a piecewise-linear approximation to the graph of S. WINDOW (0, 40000)-(3, 45400) sets the coordinates of the screen, so t runs from 0 to 3 across and S runs from 40,000 to 45,400 up. (SCREEN 12 just switches the computer into graphics mode.)
In the Python, window(...) and line(...) do the same jobs as WINDOW and LINE. Try setting numberofsteps to 3 to see the individual segments, or to 300 for a smoother curve.
To see how the approximation depends on the step size, check Compare step sizes. That adds code to the end of the program that runs Euler’s method again with 3, 6, 15, and 300 steps and draws each run in its own color, on the same graph as the original 30-step curve in blue. The legend under the graph lists the colors with their segment counts, so you can match each curve to its step size. With 3 steps the segments are a whole day long and the corners are easy to see; with 300 they are too short to notice. The coarse runs end up too high, about 41,400 with 3 steps compared with about 40,500 with 300, because each Euler step uses the slope at the start of the step, and S is falling faster and faster over these three days. As the steps get smaller the curves crowd together, closing in on the true graph of S. Edit the list of pairs to try other step sizes, and uncheck the box to take the comparison away again.
Notes from the textbook
Add notes here.
1 DEFDBL A-Z
2 SCREEN 12
3 WINDOW (0, 40000)-(3, 45400)
4 tinitial = 0
5 tfinal = 3
6 t = tinitial
7 S = 45400
8 I = 2100
9 R = 2500
10 numberofsteps = 30
11 deltat = (tfinal - tinitial) / numberofsteps
12 FOR k = 1 TO numberofsteps
13 Sprime = -.00001 * S * I
14 Iprime = .00001 * S * I - I / 14
15 Rprime = I / 14
16 deltaS = Sprime * deltat
17 deltaI = Iprime * deltat
18 deltaR = Rprime * deltat
19 LINE (t, S)-(t + deltat, S + deltaS)
20 t = t + deltat
21 S = S + deltaS
22 I = I + deltaI
23 R = R + deltaR
24 NEXT k
Graph
Each line(...) draws one straight segment, like BASIC's LINE;
an optional third number picks its color. window(...) sets the visible range, like WINDOW.
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.