May-03-2019, 01:03 AM
I slightly rewrote your code, you can adapt it for your needs.
import numpy as np import matplotlib.pylab as plt def volterra(P, K=1, r=1): return r * P * (1 - P / K) def euler(fun, Po, h, N, **kwargs): acc = [Po] times = np.cumsum(np.r_[0, np.ones(N) * h]) for _ in range(N): acc.append(acc[-1] + h * fun(acc[-1], K=K, r=r)) # for autonomous system's only return np.array(acc), times K = 12 r = 0.43 Po = 1 N = 30; step_values = [1, 0.5, 0.1] fig = plt.figure() ax = fig.add_subplot(111) for h, c in zip(step_values, 'rgb'): Ps, Ts = euler(volterra, Po, h, N, K=K, r=r) ax.plot(Ts, Ps, c) plt.show( )