import numpy as np
import matplotlib.pyplot as plt
# solution
def y(t, N0, r=0.5, K=10.):
'''
Args
t : time
N0: initial population (unit: # of animals)
r : growth rate (unit 1/time)
K : carrying capacity (unit: # of animals)
'''
N = K / (1. + ((K - N0)/N0) * np.exp(-r * t))
return N
# plot solution for different initial population choices
t = np.linspace(0, 10, 101) # 101 points in time spanning [0, 10]
arr_N0 = 2. * np.arange(1, 11) # array of initial conditions
r = 0.5
K = 10.
# loop over initial conditions and plot solutions
plt.figure(figsize = (9, 7))
for n0 in arr_N0:
plt.plot(t, y(t, n0, r=r, K=K))
plt.plot([t.min(), t.max()], K * np.ones(2), color = 'k', linewidth = 2.5, label='Carrying Capacity')
plt.xlabel('time')
plt.ylabel('N')
plt.title(f'K = {K}, r = {r}')
plt.legend()
# plot solution for different carrying capacity choices
t = np.linspace(0, 10, 101) # 101 points in time spanning [0, 10]
N0 = 10
r = 0.5
arr_K = 2 * np.arange(1, 11)
plt.figure(figsize = (9, 7))
for _ in arr_K:
plt.plot(t, y(t, N0, r=r, K=_))
# plt.plot([t.min(), t.max()], K * np.ones(2), color = 'k', linewidth = 2.5, label='Carrying Capacity')
plt.xlabel('time')
plt.ylabel('N')
plt.title(f'Varrying Carrying Capacity (K) with Fixed Params: N0 = {N0}, r = {r}')
plt.legend()
No handles with labels found to put in legend.
# plot solution for different growth rates choices
t = np.linspace(0, 10, 101) # 101 points in time spanning [0, 10]
N0 = 10
arr_r = np.linspace(0, 1, 11)
K = 16
plt.figure(figsize = (9, 7))
for _ in arr_r:
plt.plot(t, y(t, N0, r=_, K=K))
plt.plot([t.min(), t.max()], K * np.ones(2), color = 'k', linewidth = 2.5, label='Carrying Capacity')
plt.xlabel('time')
plt.ylabel('N')
plt.title(f'Varrying Growth Rates (r) with Fixed Params: N0 = {N0}, K = {K}')
plt.legend()