""" Ex. E.3 from "A primer on..." Solve the same ODE as in E.1 and E.2, but now using the ForwardEuler subclass from the ODESolver hierarchy. The usage of this class is identical to the class used in E.2, despite the different implementation, so most of the code is identical to ex E.2. """ import numpy as np import matplotlib.pyplot as plt from ODESolver import ForwardEuler class RHS: def __init__(self, r): self.r = r def __call__(self, t, u): return self.r * u problem = RHS(0.1) solver = ForwardEuler(problem) solver.set_initial_condition(u0 = 0.2) T = 20 N = 4 t, u = solver.solve((0,T), N) plt.plot(t, u, label=f'$\Delta t = $ {T/N}') t_fine = np.linspace(0, T, 200) plt.plot(t_fine, 0.2 * np.exp(0.1 * t_fine), label = 'Exact') plt.legend() plt.show() plt.plot(t, u, label = f'dt={T / N}') t_fine = np.linspace(0,T, 200) plt.plot(t_fine, 0.2 * np.exp(0.1 * t_fine),label = 'exact') plt.legend() plt.show()