1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate os.chdir(r'D:\projects\wordpress\ex34') os.getcwd() a, b, c, d = 0.4, 0.002, 0.001, 0.7 def f(xy, t): x, y = xy return [a * x - b * x * y, c * x * y - d * y] xy0 = [600, 400] t = np.linspace(0, 50, 250) xy_t = integrate.odeint(f, xy0, t) xy_t.shape fig, axes = plt.subplots(1, 2, figsize=(8, 4)) axes[0].plot(t, xy_t[:,0], 'r', label="Prey") axes[0].plot(t, xy_t[:,1], 'b', label="Predator") axes[0].set_xlabel("Time") axes[0].set_ylabel("Number of animals") axes[0].legend() axes[1].plot(xy_t[:,0], xy_t[:,1], 'k') axes[1].set_xlabel("Number of prey") axes[1].set_ylabel("Number of predators") fig.tight_layout() plt.savefig("example34.png", dpi=100) plt.show() plt.close() <img class=" wp-image-167 aligncenter" src="https://gantovnik.com/bio-tips/wp-content/uploads/2019/01/example34.png" alt="example34" width="600" height="300" /> |
Recent Comments