import numpy as np import matplotlib.pyplot as plt from math import log # Set up parameters and initial value N0 = 100 lamb = 0.5 * log(2) # Parameters for Euler's method h = 1 t_final = 10 # Compute time grid and number of steps needed t = np.arange(0, t_final, h) steps = len(t) - 1 # Initialize solution vector N = np.zeros((steps + 1,1)) N[0] = N0 # Use Euler's method. # Each iteration computes a new value for N for k in range(steps): N[k + 1] = N[k] - lamb * h * N[k] # This is the exact solution N_exact = N0 * np.exp(-lamb*t) # Do some plotting plt.plot(t, N, label = 'Simulated') plt.plot(t, N_exact, label = 'Exact') plt.legend() plt.xlabel('t') plt.ylabel('N(t)') plt.show()