{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

## Here is some very basic S-I-R code!

\n", "\n", "

This corresponds to the SIR program that appears on page 44 of the printed text, or p. 50 in the pdf version. (You don't need to understand what this does until we get to that part of the book; for now, you can just use the code and perhaps change a few constants.) But using this code, you can set S, I, R, starting time, ending time, and number of increments within the time interval. It prints out t, S, I, R for every time-step.

\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t = 0\n", "S = 45400\n", "I = 2100\n", "R = 2500\n", "deltat = 1\n", "nsteps = 3\n", "for step in range(nsteps):\n", " S_prime = -.00001*S*I\n", " I_prime = .00001*S*I - I/14\n", " R_prime = I/14\n", " deltaS = S_prime*deltat\n", " deltaI = I_prime*deltat\n", " deltaR = R_prime*deltat\n", " t = t + deltat\n", " S = S + deltaS\n", " I = I + deltaI\n", " R = R + deltaR\n", " print(t)\n", " print(S)\n", " print(I)\n", " print(R)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

There are lots of ways to make this code better and more useful, and we'll get to them later. Well, unless you're impatient... let's just make the code a tiny bit better.\n", "\n", "First, let's make the output a bit tidier, by placing t, S, I, and R all on one line each time:

\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t = 0\n", "S = 45400\n", "I = 2100\n", "R = 2500\n", "deltat = 1\n", "nsteps = 3\n", "for step in range(nsteps):\n", " S_prime = -.00001*S*I\n", " I_prime = .00001*S*I - I/14\n", " R_prime = I/14\n", " deltaS = S_prime*deltat\n", " deltaI = I_prime*deltat\n", " deltaR = R_prime*deltat\n", " t = t + deltat\n", " S = S + deltaS\n", " I = I + deltaI\n", " R = R + deltaR\n", " print(t,S,I,R)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

You could imagine that we might want to look far into the future and see what happens, but not actually print out each intermediate value for t, S, I, and R every time. Let's just print out the last one...

" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t = 0\n", "S = 45400\n", "I = 2100\n", "R = 2500\n", "deltat = 1\n", "nsteps = 3\n", "for step in range(nsteps):\n", " S_prime = -.00001*S*I\n", " I_prime = .00001*S*I - I/14\n", " R_prime = I/14\n", " deltaS = S_prime*deltat\n", " deltaI = I_prime*deltat\n", " deltaR = R_prime*deltat\n", " t = t + deltat\n", " S = S + deltaS\n", " I = I + deltaI\n", " R = R + deltaR\n", "print(t,S,I,R)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how subtle the difference is between this code and the previous code. (Lesson: tabs/spacing matter in Sage!) Can you figure out how the computer is reading these two blocks of code differently? That is, what does the computer see as the difference in meaning?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath 8.0", "language": "", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 2 }