Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
614 views
ubuntu2004
Kernel: Python 3 (system-wide)
import numpy as np import matplotlib.pyplot as plt r_X = 1 n_hours = 8 initial_population = 1000 pop_X = np.zeros(n_hours + 1) pop_X[0] = initial_population for i in range(n_hours): pop_X[i + 1] = pop_X[i] + pop_X[i] * r_X print("Population of species X:", pop_X)
Population of species X: [ 1000. 2000. 4000. 8000. 16000. 32000. 64000. 128000. 256000.]
plt.figure(figsize=(6,3)) plt.plot(pop_X) plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species X")
Text(0.5, 1.0, 'Species X')
Image in a Jupyter notebook
r_Y = 0.1 n_hours = 8 initial_population = 1000 pop_Y = np.zeros(n_hours + 1) pop_Y[0] = initial_population for i in range(n_hours): pop_Y[i + 1] = pop_Y[i] + pop_Y[i] * r_Y print("Population of species Y:", pop_Y)
Population of species Y: [1000. 1100. 1210. 1331. 1464.1 1610.51 1771.561 1948.7171 2143.58881]
plt.figure(figsize=(6,3)) plt.plot(pop_Y) plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species Y")
Text(0.5, 1.0, 'Species Y')
Image in a Jupyter notebook
data_X = np.array([ 1. , 2.18, 4.45, 8.91, 16.1 , 31.49, 60.89, 117.58, 214.4 ]) * 1000 plt.figure(figsize=(6,3)) plt.plot(pop_X, label="model") plt.plot(data_X, label="experiment") plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species X") plt.legend()
<matplotlib.legend.Legend at 0x7fd6a84e8b80>
Image in a Jupyter notebook
data_X = np.array([ 1. , 2.18, 4.45, 8.91, 16.1 , 31.49, 60.89, 117.58, 214.4 ]) * 1000 plt.figure(figsize=(6,3)) plt.plot(pop_Y, label="model") plt.plot(data_X, label="experiment") plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species Y") plt.legend()
<matplotlib.legend.Legend at 0x7fd6a8546b80>
Image in a Jupyter notebook
data_X = np.loadtxt("data_exp_X.txt") print(data_X) r_X = 1 n_hours = 24 initial_population = 1000 pop_X = np.zeros(n_hours + 1) pop_X[0] = initial_population for i in range(n_hours): pop_X[i + 1] = pop_X[i] + pop_X[i] * r_X plt.figure(figsize=(6,3)) plt.plot(pop_X, label="model") plt.plot(data_X, label="experiment") plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species X") plt.legend()
[1.00000000e+03 2.17777342e+03 4.44576258e+03 8.91456547e+03 1.60958432e+04 3.14897238e+04 6.08883077e+04 1.17580768e+05 2.14397399e+05 3.81989811e+05 6.03315856e+05 7.87285829e+05 9.96982461e+05 1.04405640e+06 1.08337178e+06 9.95533440e+05 9.79746583e+05 9.72332987e+05 9.21969859e+05 1.04273060e+06 9.43221236e+05 9.37714865e+05 9.85877957e+05 1.07409317e+06]
<matplotlib.legend.Legend at 0x7fd6a844f6a0>
Image in a Jupyter notebook
plt.figure(figsize=(6,3)) plt.plot(data_X, color='#ff7f0e')
[<matplotlib.lines.Line2D at 0x7fd6a81ef1f0>]
Image in a Jupyter notebook
data_X = np.loadtxt("data_exp_X.txt") print(data_X) r_X = 1 K_X = 1e6 n_hours = 24 initial_population = 1000 pop_X = np.zeros(n_hours + 1) pop_X[0] = initial_population for i in range(n_hours): pop_X[i + 1] = pop_X[i] + (1 - pop_X[i]/K_X) * r_X * pop_X[i] plt.figure(figsize=(6,3)) plt.plot(pop_X, label="model") plt.plot(data_X, label="experiment") plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species X") plt.legend()
[1.00000000e+03 2.17777342e+03 4.44576258e+03 8.91456547e+03 1.60958432e+04 3.14897238e+04 6.08883077e+04 1.17580768e+05 2.14397399e+05 3.81989811e+05 6.03315856e+05 7.87285829e+05 9.96982461e+05 1.04405640e+06 1.08337178e+06 9.95533440e+05 9.79746583e+05 9.72332987e+05 9.21969859e+05 1.04273060e+06 9.43221236e+05 9.37714865e+05 9.85877957e+05 1.07409317e+06]
<matplotlib.legend.Legend at 0x7fd6a81ab700>
Image in a Jupyter notebook
data_Y = np.loadtxt("data_exp_Y.txt") print(data_Y) r_Y = 0.4 K_Y = 1e5 n_hours = 24 initial_population = 1000 pop_Y = np.zeros(n_hours + 1) pop_Y[0] = initial_population for i in range(n_hours): pop_Y[i + 1] = pop_Y[i] + (1 - pop_Y[i]/K_Y) * r_Y * pop_Y[i] plt.figure(figsize=(6,3)) plt.plot(pop_Y, label="model") plt.plot(data_Y, label="experiment") plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species Y") plt.legend()
[ 1000. 1468.8176607 2017.57949899 2807.82253178 4159.57884669 5880.69829402 7977.64738035 10987.23896838 15591.68925539 22778.96386341 27118.703459 32395.04050862 36971.5970265 37400.13541081 40923.61000019 43399.85608048 50286.14014915 51923.92262397 48936.98814589 53081.08029097 52972.24903328 49175.42023105 50032.49399002 50577.54776097]
<matplotlib.legend.Legend at 0x7fd6a806fa90>
Image in a Jupyter notebook
data_A = np.loadtxt("data_exp_A.txt") print(data_A) r_A = 0.9 K_A = 1e6 n_hours = 24 initial_population = 1000 pop_A = np.zeros(n_hours + 1) pop_A[0] = initial_population for i in range(n_hours): pop_A[i + 1] = pop_A[i] + (1 - pop_A[i]/K_A) * r_A * pop_A[i] plt.figure(figsize=(6,3)) plt.plot(pop_A, label="model") plt.plot(data_A, label="experiment") plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species A") plt.legend() print("data from experiment A fits the model when r (the growth rate) = 0.9")
[ 1000. 1815.38733857 3448.85956215 5798.61439409 10814.57619309 21383.8241311 41567.08370915 80314.28400864 149785.58154461 265585.75112717 365411.1780143 491689.65445915 481846.55931036 512615.59044084 484702.74364789 557117.06357226 577315.88799858 536066.67437239 587459.10894856 522740.85774677 541827.5414437 544308.07661042 511645.32546393 547557.6806335 ] data from experiment A fits the model when r (the growth rate) = 0.9
Image in a Jupyter notebook
data_B = np.loadtxt("data_exp_B.txt") print(data_B) r_B = 0.4 K_B = .5e5 n_hours = 24 initial_population = 10000 pop_B = np.zeros(n_hours + 1) pop_B[0] = initial_population for i in range(n_hours): pop_B[i + 1] = pop_B[i] + (1 - pop_B[i]/K_B) * r_B * pop_B[i] plt.figure(figsize=(6,3)) plt.plot(pop_B, label="model") plt.plot(data_B, label="experiment") plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species B") plt.legend() print("data from experiment B fits the model when r (the growth rate) = 0.4")
[10000. 14459.72616873 19131.64692025 24238.72992793 26897.68997643 31945.75133898 36640.34691545 41360.91604431 44038.35660568 47444.96450721 48857.57934742 47687.15960361 52444.80325624 55773.3720636 60200.29157554 58068.40452503 55623.84038794 53861.60465174 50108.26898469 54439.73015449 51652.25171659 49774.81127443 51135.06372078 56478.91948071] data from experiment B fits the model when r (the growth rate) = 0.4
Image in a Jupyter notebook
data_C = np.loadtxt("data_exp_C.txt") print(data_C) r_C = 0.7 K_C = 1e5 n_hours = 24 initial_population = 1000 pop_C = np.zeros(n_hours + 1) pop_C[0] = initial_population for i in range(n_hours): pop_C[i + 1] = pop_C[i] + (1 - pop_C[i]/K_C) * r_C * pop_C[i] plt.figure(figsize=(6,3)) plt.plot(pop_C, label="model") plt.plot(data_C, label="experiment") plt.xlabel("time (hours)") plt.ylabel("population") plt.title("Species C") plt.legend() print("data from experiment C fits the model when r (the growth rate) = 0.7")
[ 1000. 1713.24432864 2744.49898211 4449.95419272 7662.63408538 12526.09381011 19450.42606159 30143.888785 46748.25020832 71004.59399994 81066.7935574 90233.10312421 94121.25320283 87729.32306957 92361.26302922 93968.98534754 105341.48590974 102981.44000993 94634.96172829 103248.79824022 101238.10227826 93521.06457228 96575.99191112 97826.04612173] data from experiment C fits the model when r (the growth rate) = 0.7
Image in a Jupyter notebook