import numpy as np import matplotlib.pyplot as plt def solver(m, H0, L0, dt, a, b, c, d, t0): """Solve the difference equations for H and L over m years with time step dt (measured in years.""" num_intervals = int(m/float(dt)) t = np.linspace(t0, t0 + m, num_intervals+1) H = np.zeros(t.size) L = np.zeros(t.size) print 'Init:', H0, L0, dt H[0] = H0 L[0] = L0 for n in range(0, len(t)-1): H[n+1] = H[n] + a*dt*H[n] - b*dt*H[n]*L[n] L[n+1] = L[n] + d*dt*H[n]*L[n] - c*dt*L[n] return H, L, t # Load in data file data = np.loadtxt('Hudson_Bay.csv', delimiter=',', skiprows=1) # Make arrays containing x-axis and hares and lynx populations t_e = data[:,0] H_e = data[:,1] L_e = data[:,2] # Simulate using the model H, L, t = solver(m=20, H0=34.91, L0=3.857, dt=0.1, a=0.4807, b=0.02482, c=0.9272, d=0.02756, t0=1900) # Visualize simulations and data plt.plot(t_e, H_e, 'b-+', t_e, L_e, 'r-o', t, H, 'm--', t, L, 'k--') plt.xlabel('Year') plt.ylabel('Numbers of hares and lynx') plt.axis([1900, 1920, 0, 140]) plt.title(r'Population of hares and lynx 1900-1920 (x1000)') plt.legend(('H_e', 'L_e', 'H', 'L'), loc='upper left') plt.savefig('Hudson_Bay_sim.pdf') plt.savefig('Hudson_Bay_sim.png') plt.show()