update on getting started
This commit is contained in:
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 101 KiB |
@@ -0,0 +1,43 @@
|
||||
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()
|
||||
@@ -0,0 +1,19 @@
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
# Load in data file
|
||||
data = np.loadtxt('src/Hudson_Bay.dat', delimiter=',', skiprows=1)
|
||||
# Make arrays containing x-axis and hares and lynx populations
|
||||
year = data[:,0]
|
||||
hares = data[:,1]
|
||||
lynx = data[:,2]
|
||||
|
||||
plt.plot(year, hares ,'b-+', year, lynx, 'r-o')
|
||||
plt.axis([1900,1920,0, 100.0])
|
||||
plt.xlabel(r'Year')
|
||||
plt.ylabel(r'Numbers of hares and lynx ')
|
||||
plt.legend(('Hares','Lynx'), loc='upper right')
|
||||
plt.title(r'Population of hares and lynx from 1900-1920 (x1000)}')
|
||||
plt.savefig('Hudson_Bay_data.pdf')
|
||||
plt.savefig('Hudson_Bay_data.png')
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user