""" Exercise E.29 from "A primer on..." Sovle a system of two ODEs describing two radioactive substances. Also, write a test function that checks that the ratio of the two solution components converges to the right equilibrium value. """ from odesolver import ExplicitMidpoint import matplotlib.pyplot as plt class Decay: def __init__(self, tau_A, tau_B): self.tau_A = tau_A self.tau_B = tau_B def __call__(self, t, u): u_A, u_B = u du_A = u_B / self.tau_B - u_A / self.tau_A du_B = u_A / self.tau_A - u_B / self.tau_B return [du_A, du_B] def test_Decay(): tau_A, tau_B = 8, 40 problem = Decay(tau_A, tau_B) solver = ExplicitMidpoint(problem) solver.set_initial_condition([1,1]) T = 100 t, u = solver.solve((0,T), 600) tol = 1e-6 assert abs(u[-1][0] / u[-1][1] - tau_A / tau_B) < tol test_Decay() problem = Decay(8, 40) solver = ExplicitMidpoint(problem) solver.set_initial_condition([1,1]) T = 100 t, u = solver.solve((0,T), 600) plt.plot(t, u) plt.show()