“Part 1.2 – Simulating an Exponential

Introduction

This part contains a python computer program that solves the differential equation for an exponential function. However, the exponential function has a simple mathematical form:

    \[  f(t) = A_o  e^{\beta t}  \]

Here,  A_0 is the value of the function at time zero and \beta controls how fast the exponential grows.  In order to compute the doubling time use the formula below.

    \[ T_{\frac{1}{2}} = \frac{ln(2)}{\beta} \]

Unfortunately, in most cases there is no simple simple mathematical expression  for the solution of our epidemiological models.  Consequently, the solution of many models require some form of computer application. 

Even though there is a simple solution for an exponential, simulating this simple case provides insight into how to proceed in more complicated cases.  Also, solving problems where we know the answer also gives us some insight into the accuracy of the techniques.

Differential Equation for the exponential

Some models take the form of a differential equation.  In this case, the differential equation for the exponential is just

    \[ \frac{df(t)}{dt} = \beta f(t) \]

The next three models we encounter will also have a description using differential equations.  Formulas exist for the solutions of the first two models.   For the third we will rely on a computer generated solution.

Probably the best way of solving this equation on a computer is to use a standard package.  I will illustrate using the odeint package in the scipy python package.

Python Code





                    

# ************** exponential differential equation Solver
import numpy as np
from scipy.integrate import odeint

def f(y, t, parm):             # returns the derrivative of f(t) (y)
    E = y[0]                   # get the current value of y
    beta = parm[0]             # get the parameter
    derivative = [beta*E]      # calculate the derrivative
    print(E,beta,derivative)   # if you want to see where odeint is looking
    return derivative

beta = 0.10     # growth rate
A0 = 1.0        # Initial value
e=2.7182818284  # Euler's number (approx)

parm = [beta]   #ODE solver wants a list of Parameters
y0 = [A0]       #ODE also wants a list of initial values

# Make an array of the times at which we want a solution
Finaltime = 50.
Increment = 1.0
times = np.arange(0.0, Finaltime, Increment)

# Call the ODE solver
Solutions = odeint(f, y0, times, args=(parm,))

for i in range(len(times)): # printout and compare with actual solution
    t = times[i]
    F = Solutions[i,0]
    val = A0*e**(beta*t)
    error = (val-F)/val
    print(t,F,val,error)

If you are new to Python I would recommend you install the anaconda package to start with. https://www.anaconda.com/products/individual There are many tutorials around the web and which you choose is largely dependent on how much programming experience you have.

When I first started using Python, I was skeptical. Having put Python to the test with some larger longer problems, I have been pleasantly surprised. Python will be used to illustrate the numerical evaluations of these models.

If you are not familiar with scipy, information on the scipy package can be found here. https://docs.scipy.org/doc/scipy/reference/index.html

Code output

Here is the results from the code output, without the print statement in the function definition. For now I have avoided putting in fancy formatting for the output, or dropping a .csv file, which we will do later. When you look at the output, you can see the ODE (ordinary differential equation solver) does quite well!

                    
0.0 1.0 1.0 0.0
1.0 1.10517091083 1.10517091807 6.55179013747e-09
2.0 1.22140274509 1.22140275815 1.07006143991e-08
3.0 1.34985881514 1.34985880757 -5.61355628903e-09
4.0 1.49182468997 1.49182469763 5.13613651715e-09
5.0 1.64872126622 1.64872127068 2.7084385355e-09
6.0 1.82211880002 1.82211880037 1.88127394664e-10
7.0 2.01375272726 2.01375270744 -9.84438678201e-09
8.0 2.22554102546 2.22554092845 -4.35865904776e-08
9.0 2.45960316269 2.45960311111 -2.0971776037e-08
10.0 2.71828190095 2.7182818284 -2.66893823457e-08
11.0 3.00416610865 3.00416602387 -2.82176867678e-08
12.0 3.32011701869 3.32011692265 -2.892667257e-08
13.0 3.66929678102 3.66929666752 -3.09324330815e-08
14.0 4.05520009801 4.05519996672 -3.23751385432e-08
15.0 4.48168922183 4.48168907019 -3.3835454142e-08
16.0 4.95303260853 4.95303242422 -3.7210926995e-08
17.0 5.47394761386 5.47394739153 -4.06167309107e-08
18.0 6.04964769834 6.04964746418 -3.87066411895e-08
19.0 6.68589469109 6.685894442 -3.72561476878e-08
20.0 7.38905637384 7.38905609861 -3.72488048811e-08
21.0 8.16617022331 8.1661699122 -3.80984160992e-08
22.0 9.02501384998 9.025013499 -3.88890627494e-08
23.0 9.974182848 9.97418245432 -3.94707127056e-08
24.0 11.023176822 11.0231763801 -4.00952798538e-08
25.0 12.1824944589 12.18249396 -4.09512468432e-08
26.0 13.4637385959 13.4637380342 -4.1713021308e-08
27.0 14.8797323634 14.879731724 -4.29693115595e-08
28.0 16.4446475217 16.4446467701 -4.57033773444e-08
29.0 18.1741462355 18.1741453683 -4.77136911932e-08
30.0 20.0855379059 20.0855369219 -4.89898234626e-08
31.0 22.1979524258 22.1979512799 -5.16181524432e-08
32.0 24.5325314947 24.5325301954 -5.29640075587e-08
33.0 27.1126404052 27.1126389187 -5.48275646491e-08
34.0 29.9641017602 29.9641000452 -5.72373419589e-08
35.0 33.1154538896 33.1154519562 -5.8383047583e-08
36.0 36.5982366651 36.5982344408 -6.07749759469e-08
37.0 40.4473068879 40.4473043568 -6.25767631203e-08
38.0 44.7011873517 44.7011844896 -6.40264447428e-08
39.0 49.4024523966 49.4024491013 -6.67017957485e-08
40.0 54.5981537358 54.5981500284 -6.79037322182e-08
41.0 60.3402918076 60.340287592 -6.98640784899e-08
42.0 66.6863358482 66.6863310348 -7.21791962817e-08
43.0 73.6997991008 73.6997936927 -7.33801104286e-08
44.0 81.45087484 81.4508686572 -7.59087093946e-08
45.0 90.0171382673 90.0171312917 -7.749189686e-08
46.0 99.4843234972 99.484315632 -7.90599042456e-08
47.0 109.947181424 109.947172441 -8.16998947103e-08
48.0 121.510427573 121.510417506 -8.28522896948e-08
49.0 134.28979108 134.289779671 -8.49574338369e-08

                

Next Step

Finally, here is a link to the next model, where everyone eventually gets infected. https://davidalarrabee.com/?page_id=717

A dialog between Science, economics and Religion