In [1]:
# import the necessary modules
import numpy as np 
import matplotlib.pyplot as plt
%matplotlib inline

# for pretty plot
import seaborn as sns
sns.set()

Below, we define our parameters

In [2]:
# parameters for our ODE
N_0 = 1
k = 0.03 # min^-1 

# parameters for the our integration 
dt = 0.1 # min 
total_time = 120 # mins

Set up the integration

In [3]:
# determine number of time steps that will be taken
num_steps = int(total_time/dt)

# intilize an array of length num_steps 
# into which we will store our values of N
N_t = np.zeros(num_steps)
N_t[0] = N_0

Actually do the integration

In [4]:
# numerically integrate by looping through N_t 
for t in range(1, num_steps):
    
    # calculate dN, using previous N_t entry
    dN = k * dt * N_t[t-1]
    
    # update current N_t entry
    N_t[t] = N_t[t-1] + dN 

Plot the results

In [5]:
# get x values for time 
times = np.arange(num_steps)*dt # in units of minutes

# plot 
plt.plot(times,N_t)
plt.xlabel("time (mins)")
plt.ylabel("N");

Let's compare to a "real" exponential growth curve

In [6]:
# compute the known solution
soln = N_0 * np.exp(k*times)

# plot known solution and our solution
plt.plot(times, N_t)
plt.plot(times, soln)
plt.xlabel("time (mins)")
plt.ylabel("N")
plt.legend(["numerical integration","known solution"])
Out[6]:
<matplotlib.legend.Legend at 0x1a195ff400>