# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <font color=blue>NOTE: Videos and graphs do not display immediately on all devices.  If this is the case for you, click the "Cell" menu option, and select "Run All".  If you still do not see the videos/graphs, try a different browser.  If that also does not work, please let your instructor know by email.</font>
#
# # The Logistic Equation
#
# In this notebook, we analyze the Logistic Equation.  The Logistic Equation is a separable differential equation that represents <i>limited</i> growth.  The Logistic Equation perhaps is the second-most important differential equaion (after the World's Most Important Differential Equation, which represents <i>exponential</i> growth [and decay]), at least in the context of modelling in the life sciences.
#
# > Visit [the Wikepedia page on the Logistic Equation](https://en.wikipedia.org/wiki/Logistic_function) to learn more about the applicability of the Logistic Equation in a wide variety of scientific and social disciplines.
#
# We solve the Logistic Equation, and compare the solution to the outcome of qualitative analysis (phase plot analysis, covered in MATH 134).  For readers who did not take MATH 134, we review phase plot analysis.
#
# ### 1. About the Logistic Equation
#
# While exponential growth (or decay) is a reasonable model for some sitations, such as the initial growth of a population, or the initial growth of the number of infected individuals in a population in the context of an infectious disease spreading through the population, eventually the exponential model no longer applies.  For example, at a certain size, a growing population will reach a limit where the habitat will not support any more individuals.
#
# Suppose that the per-capita growth rate of a population of size $N$ declines linearly from a value of $r$ when $N=0$ to a value of 0 when $N=K$, where $K$ is the <i>carrying capacity</i> of the environment, then
# $$\frac{dN/dt}{N} = r \left( 1 - \frac{N}{K} \right),$$
# where $N(t)$ is the size of the population at time $t$.  The left-hand side of the equation expresses the <i>per-capita</i> growth rate (growth rate $dN/dt$ divided by the number of individuals in the population), and the right-hand side of the equation expresses the linear decrease in the per-capital growth rate as a function of $N$.  When $N=0$, the per-capita growth rate is $r$; when $N=K$, the per-capita growth rate is $K$.
#
# Rewriting the equation, we obtain the Logistic Equation, namely
# $$\frac{dN}{dt} = r N \left( 1 - \frac{N}{K} \right)$$
# or 
# $$\frac{dN}{dt} = \frac{r}{K} N \left( K - N \right).$$
# We recognize the Logistic Equation as a differential equation, where $t$ is the independent variable, $N$ is the dependent variable, and $r$ and $K$ are parameters (constants that may vary from one population or environment to another).
#
# We seek a function $N(t)$ that satisfies the differential equation.  We will do so in Section 5 below.  
#
# ### 2. Review of Phase Plot Analysis
#
# Before we find the exact form of the solution, we will determine the qualitative nature of the solutions using <i>phase plot analysis</i> of the differential equation.  For readers who have taken MATH 134, this is a review; for other readers, this is an introduction.
#
# Consider a generic differential equation $\displaystyle \frac{dy}{dt} = f(y)$.  Here, we seek a function $y(t)$ that satisfies the differential equation.
#
# Phase plot analysis is facilitated by the graph of $\displaystyle \frac{dy}{dt} = f(y)$ versus $y$, as summarized in the following graphic:
#
# ![Fig-PhaselineAnalysis.jpg](attachment:Fig-PhaselineAnalysis.jpg)
#
# Given the graph of $\displaystyle \frac{dy}{dt} = f(y)$ versus $y$ on the left, we aim to <i>infer</i> qualitative sketches of the family of solutions, that is, we aim to <i>infer</i> the graph of $y(t)$ versus $t$ on the right.
#
# > This is a challenging concept.  Do take a moment to understand the difference between the two graphs.
#
# Phase plot analysis brings together the following to infer the graph of $y(t)$ versus $t$:
# 1. Whenever $f(y) > 0$, $\displaystyle \frac{dy}{dt} > 0$, and so $y(t)$ increases.
# 2. Whenever $f(y) < 0$, $\displaystyle \frac{dy}{dt} < 0$, and so $y(t)$ decreases.
# 3. Whenever $f(y) = 0$, $\displaystyle \frac{dy}{dt} = 0$, and so $y(t)$ does not change.
#
# In the following video, we give an overview of phase plot analysis.

from IPython.display import YouTubeVideo
YouTubeVideo('KKiFCTJPYkE')

# ### 3. Phase Plot Analysis of the Logistic Equation
#
# We now return to the Logistic Equation, 
# $$\frac{dN}{dt} = r N \left( 1 - \frac{N}{K} \right).$$
# We let $\displaystyle r = \frac{3}{2}$ and $K = 4000$ to obtain
# $$\frac{dN}{dt} = \frac{3}{2} N \left( 1 - \frac{N}{4000} \right).$$
#
# In the following video, we use phase plot analysis to infer the qualitative nature of the solutions of this Logistic Equation.

from IPython.display import YouTubeVideo
YouTubeVideo('-hQGBGUlr3Y')

# ### 4. Solving the Logistic Equation
#
# We now proceed with determining the solution of the Logistic Equation
# $$\frac{dN}{dt} = \frac{3}{2} N \left( 1 - \frac{N}{4000} \right)$$
# or 
# $$\frac{dN}{dt} = \frac{3}{2 \cdot 4000} N \left( 4000 - N \right).$$
#
# Note that the Logistic Equation is separable.  In differential form, we have
# $$ \frac{4000 \cdot dN}{N ( 4000 - N ) } = \frac{3}{2} dt$$
# provided that $N \ne 0$ or $N \ne 4000$.  The latter is important; we will return to these conditions later.
#
# We now integrate both sides:
# $$\int \frac{4000 \cdot dN}{N ( 4000 - N ) } = \frac{3}{2} \int dt.$$
# The integral on the left-hand side can be evaluated using the method of partial fractions. It is left as an exercise to the reader to show that 
# $$\frac{4000}{N ( 4000 - N ) } = \frac{1}{N} + \frac{1}{4000-N}.$$
# We thus obtain
# $$\ln|N| - \ln|4000-N| = \frac{3}{2} t + C,$$
# where $C$ is the constant of integration.
# Combining the terms on the left-hand side into one term gives 
# $$\ln \left| \frac{4000-N}{N} \right| = - \frac{3}{2} t - C.$$
# Exponentiating both sides gives 
# $$\left| \frac{4000-N}{N} \right| = e^{ - \frac{3}{2} t - C } = e^{-\frac{3}{2} t} e^{-C} = \hat{C}e^{ - \frac{3}{2} t},$$
# where $\hat{C} = e^{-C}$.
# Note that $\hat{C}$ is positive.  Allowing $\hat{C}$ to be non-positive as well means that we can drop the absolute value on the left-hand side, that is 
# $$\frac{4000-N}{N} = C e^{ - \frac{3}{2} t },$$
# where we have dropped the hat from the constant $C$ for convenience.
# It is left as an exercise for the reader to work through the algebra
# to write $N$ explicitly as as function of $N$, resulting in 
# $$N(t) = \frac{ 4000 }{ 1 + C e^{-\frac{3}{2} t } }.$$
# The latter is the <b>general</b> solution of the Logistic Equation, defining a family of solutions.  
#
# If we are given an initial condition, $N(0) = N_0$, then we can determine $C$.  It is left as an exercise for the reader to show that $\displaystyle C = \frac{4000 - N_0}{N_0}$.
#
# Recall that rewriting the Logistic Equation in differential form was valid only when $N \ne 0, 4000$.  It turns out that $N(t) = 0$ and $N(t) = 4000$ also satisfy the Logistic Equation (check it!), and therefore also are solutions.  These solutions correspond to initial conditions $N_0 = 0$ and $N_0 = 4000$, respectively.  
#
# Putting everything together, the <b>particular</b> solution of the Logistic Equation is 
# $$N(t) = 
# \begin{cases}
# \frac{ 4000 }{ 1 + \frac{4000 - N_0}{N_0} e^{-\frac{3}{2} t } } & \text{if } N_0 > 0 \\ 
# 0 & \text{if } N_0 = 0. 
# \end{cases}$$
#
# The graph of $N(t)$ for several values of $N_0$ is shown below. This is precisely the graph that we found earlier from phase plot analysis.  
#
# ![Fig-Solution.jpg](attachment:Fig-Solution.jpg)
#
# Note that $N(t)$ has 4000 as the horizontal asymptote for all $N_0 > 0$.  If $0 < N_0 < 4000$, the population eventually reaches carrying capacity, but does not exceed the carrying capacity.  If $N_0 > 4000$, the population self-regulates and decreases towards the carrying capacity. 
#
# The advantage of knowing $N(t)$ explicitly is that we can determine/predict the exact size of the population at any given time $t$.
#
# ### 5. Summary
#
# <b>The Logistic Equation</b>
#
# - The Logistic Equation is $$\frac{dN}{dt} = r N \left( 1 - \frac{N}{K} \right),$$ where $r$ is a per-capita growth rate, and $K$ is the carrying capacity.
#
#
# - The Logistic Equation is a separable differential equation that can be solved explicitly for $N(t)$.
#
# <b>Phase Plot Analysis</b> 
#
# - Phase plot analysis facilitates the visualization of the qualitative nature of solutions to autonomous differential equations, to locate their equilibria, and to determine properties of these equilibria.
#
#
# - A phase plot shows the graph of $\displaystyle \frac{dy}{dt} = g(y)$ versus $y$.  
#     1. Whenever $g(y)>0$, then $y$ is increasing.
#     2. Whenever $g(y)<0$, then $y$ is decreasing.
#     3. When $g(\hat{y})=0$, then $y=\hat{y}$ is an equilibrium.
#
# ![07p431_f01.jpg](attachment:07p431_f01.jpg)
#
# <font size=0>Credit: Biocalculus: Calculus for the Life Sciences, <NAME> and <NAME>, Cengage Learning, 2015.</font>
#
# ### 6. Further Study
#
# Please refer to Sections 7.2 and 7.4 in the textbook for additional treatment of these topics.
#
# ### 7. Don't Forget
#
# Don't forget to return to eClass to complete the pre-class quiz.

StarcoderdataJupyterScriptsDedupFiltered