#!/usr/bin/python3 # Euler's method, numerical stability import numpy as np import matplotlib.pyplot as plt lam = 10. def f(x, t): return -lam * x a, b, = 0.0, 2.0 # interval for h in [0.03, 0.15, 0.3]: x = 1.0 tpoints = np.arange(a, b, h) t2 = np.linspace(a, tpoints[-1], 100) xpoints = [] for t in tpoints: xpoints.append(x) x += h * f(x, t) plt.figure() plt.xlabel("t") plt.ylabel("x(t)") plt.plot(tpoints, xpoints, "ro") plt.plot(t2, np.exp(-lam * t2)) plt.show()