<jupyter_start><jupyter_text>Teaching notebook, Math 152, Feb 4, 2021A few ideas for analyzing "dynamical systems"The logistic equation is:$$P_{t+1} = c \cdot \frac{P_t (K - P_t)}{K}.$$Here $P_t$ represents the population at time $t$.  The constant $K$ is related to the "carrying capacity" -- the maximum population that the environment can support.  The constant $c$ represents a rate of population growth.An equilibrium will be reached if $P_{t+1} = P_t$, which occurs at $$P = K \cdot \frac{c-1}{c}.$$That is the population at which the dynamics will be "stable" -- as many new creatures are born as die, in each time step.Let's begin by implementing some formulas as Python functions, with some default values of $K$ and $c$.<jupyter_code>def logistic(P, c=1.5, K = 1000):
  return c * P * (K-P) / K

def equilibrium(c=1.5, K=1000):
  return K * (c-1) / c
equilibrium()
equilibrium(c=2.0, K=10000)
def log_steps(P_start = 5.0, num_steps=10, c=1.5, K=1000):
  P = P_start
  P_list = [P]
  for t in range(num_steps):
    P = logistic(P, c=c, K=K)
    P_list.append(P)
  return P_list
log_steps(P_start=500, num_steps=30)
equilibrium(c=2.5)
log_steps(c=2.5)
import matplotlib.pyplot as plt
import seaborn as sns
def plot_logistic(P_start, num_steps=10, c=1.5, K=1000):
  P_list = log_steps(P_start, num_steps, c, K)
  x_list = range(len(P_list))
  sns.set_style('whitegrid')
  fig,ax = plt.subplots(1,1,figsize=(10,4)) # Create a figure with 1 row and 1 column, 6 inches by 4 inches.
  plt.plot(x_list, P_list, marker='o') # Line plot with dots at time points.
  ax.axhline(equilibrium(c,K), color='red', alpha=0.5)
  ax.set_xlabel('Time')
  ax.set_ylabel('Population')
  plt.show()

plot_logistic(5, c=4.0, num_steps=100)
import numpy as np
np.arange(2.0,4.0,0.1)
def verhulst(P_start=5.0, num_steps=200, K=1000):
  sns.set_style('whitegrid')
  fig,ax = plt.subplots(1,1,figsize=(8,6))
  for c in np.arange(2.0, 4.0, 0.003):
    eq = equilibrium(c,K)
    plt.scatter(c, eq, color='red', s=3.0)
    P_list = log_steps(P_start, num_steps, c, K)
    y_list = P_list[50:]
    x_list = [c]*len(y_list)
    plt.scatter(x_list, y_list, color='k', s=0.5)
  ax.set_xlabel('Growth rate c')
  ax.axvline(3.5)
  plt.show()
verhulst()
plot_logistic(5, c=3.5, num_steps=100)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1513301351940689, 'fraction_numerical': 0.05058874836458788, 'mean_word_length': 4.488038277511961, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '641987', 'license': 'MIT', 'n_tokens_mistral': 993, 'n_tokens_neox': 929, 'n_words': 252, 'path': 'Math152_Feb4_2021.ipynb', 'repo_name': 'kelngu/PythonForMathematics', 'size': '2293'}