{ "cells": [ { "cell_type": "markdown", "metadata": { "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html)", "section": "" } }, "source": [ "\n", "*This notebook contains material from [CBE30338](https://jckantor.github.io/CBE30338);\n", "content is available [on Github](https://github.com/jckantor/CBE30338.git).*\n" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html)", "section": "" } }, "source": [ "\n", "< [1.3 Python Conditionals and Libraries](https://jckantor.github.io/CBE30338/01.03-Python-Conditionals-and-Libraries.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [2.0 Process Modeling](https://jckantor.github.io/CBE30338/02.00-Process-Modeling.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[1.4 Python Numeric Integration Revisited](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4-Python-Numeric-Integration-Revisited)",
"section": "1.4 Python Numeric Integration Revisited"
}
},
"source": [
"# 1.4 Python Numeric Integration Revisited"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.4.1 Sidenotes](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1-Sidenotes)",
"section": "1.4.1 Sidenotes"
}
},
"source": [
"## 1.4.1 Sidenotes"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.1.1 Code Academy Sidenote](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1.1-Code-Academy-Sidenote)",
"section": "1.4.1.1 Code Academy Sidenote"
}
},
"source": [
"### 1.4.1.1 Code Academy Sidenote\n",
"\n",
"* https://www.codecademy.com/learn/learn-python"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.1.2 Markdown/Latex Sidenote](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1.2-Markdown/Latex-Sidenote)",
"section": "1.4.1.2 Markdown/Latex Sidenote"
}
},
"source": [
"### 1.4.1.2 Markdown/Latex Sidenote"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.1.2 Markdown/Latex Sidenote](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1.2-Markdown/Latex-Sidenote)",
"section": "1.4.1.2 Markdown/Latex Sidenote"
}
},
"source": [
"Jupyter-notebooks are very convenient because they include these markdown blocks to include discussion of the material.\n",
"\n",
"Programs like Microsoft Word are examples of a \"What you see is what you get\" editor. In Markdown, you use characters and symbols to format your text, and then actually compile them.\n",
"\n",
"For instance, I've been making liberal use of the header feature using the '#' pound/hashtag symbol. **Double click on this cell to see how I'm creating the text below.**"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[1.4 First Header](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4-First-Header)",
"section": "1.4 First Header"
}
},
"source": [
"# 1.4 First Header"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.4.1 Second Header](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1-Second-Header)",
"section": "1.4.1 Second Header"
}
},
"source": [
"## 1.4.1 Second Header"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.1.1 Tertiary Header](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1.1-Tertiary-Header)",
"section": "1.4.1.1 Tertiary Header"
}
},
"source": [
"### 1.4.1.1 Tertiary Header"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.1.1.1 Etc](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1.1.1-Etc)",
"section": "1.4.1.1.1 Etc"
}
},
"source": [
"#### 1.4.1.1.1 Etc\n",
"\n",
"* Bullet Point\n",
"* Bullet Point 2\n",
"* Etc.\n",
"\n",
"**Bolded Text**\n",
"\n",
"_Italicized Text_\n",
"\n",
"Those are a couple examples of some basic formatting. You can see more examples throughout this tutorial. The sidenote above has an example of a link, while there are examples of a chart, and a photo below. Take a look and see if you can reproduce it on your own!\n",
"\n",
"For further reference: [Github's Markdown Guide](https://guides.github.com/features/mastering-markdown/)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.1.2 Latex Side Note](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1.2-Latex-Side-Note)",
"section": "1.4.1.2 Latex Side Note"
}
},
"source": [
"### 1.4.1.2 Latex Side Note"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.1.2 Latex Side Note](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.1.2-Latex-Side-Note)",
"section": "1.4.1.2 Latex Side Note"
}
},
"source": [
"A big included feature in Jupyter-notebook markdown blocks is that you have the ability to include LaTeX formatted text as well. \n",
"\n",
"LaTeX (pronounced \"La-tech\") is similar to a markdown language in and of itself (it is not What-You-See-Is-What-You-Get). It is considerably more feature-full than markdown, but also has a bigger learning curve. I recommend that you use it just for math, as Markdown can't provide Math formatting.\n",
"\n",
"* Start latex formatting with '\\$\\$' and end it with another '\\$\\$'\n",
"$$ math goes here $$\n",
"\n",
"* All alphabetic characters are included in a LateX math statement is intended to be a variable, and is italicized. Basic math is very intuitive due to this.\n",
"\n",
"$$ y = mx + b $$\n",
"\n",
"* As soon as you get to fractions, you have to learn some LaTeX commands. Here we'll use the '\\frac{}{}' command\n",
"\n",
"$$ \\frac{3}{5} $$\n",
"\n",
"* I prefer creating equations in LaTeX to word in large part due to symbols. Most greek letters can be added with the '\\letter' command. For instance '\\lambda'\n",
"\n",
"$$ \\lambda \\leq \\pi + \\Pi $$\n",
"\n",
"* Most common functions are included as operators in LaTeX:\n",
"\n",
"$$ \\log_b(a) = \\frac{\\log(a)}{\\log(a)} $$\n",
"\n",
"Just that should be enough to cover most of the math you'll need in this course. Don't feel like you _have_ to use LaTeX. It is also acceptable to do your work out (neatly) on paper and include a photo."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.4.2 Hare and Lynx Example](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2-Hare-and-Lynx-Example)",
"section": "1.4.2 Hare and Lynx Example"
}
},
"source": [
"## 1.4.2 Hare and Lynx Example"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.2.1 Adapted from [Dr. Kantor's Notes](https://github.com/jckantor/CBE30338/blob/master/notebooks/HareLynx/Hare%20and%20Lynx%20Population%20Dynamics.ipynb) ](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.1-Adapted-from-[Dr.-Kantor's-Notes](https://github.com/jckantor/CBE30338/blob/master/notebooks/HareLynx/Hare%20and%20Lynx%20Population%20Dynamics.ipynb))",
"section": "1.4.2.1 Adapted from [Dr. Kantor's Notes](https://github.com/jckantor/CBE30338/blob/master/notebooks/HareLynx/Hare%20and%20Lynx%20Population%20Dynamics.ipynb) "
}
},
"source": [
"### 1.4.2.1 Adapted from [Dr. Kantor's Notes](https://github.com/jckantor/CBE30338/blob/master/notebooks/HareLynx/Hare%20and%20Lynx%20Population%20Dynamics.ipynb) "
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.2.2 Introduction](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.2-Introduction)",
"section": "1.4.2.2 Introduction"
}
},
"source": [
"### 1.4.2.2 Introduction\n",
"We'd like to model the number of Hares and Lynx in a certain population of animals. \n",
"\n",
"\n",
"\n",
"As cute as that Lynx is, it will prey on the Hare to the exclusion of all other animals if possible. This means the population levels of the Lynx and Hare are intrinsically related, see the pelt trading data for the Hudson's Bay Company:\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.2.3 Modeling](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.3-Modeling)",
"section": "1.4.2.3 Modeling"
}
},
"source": [
"### 1.4.2.3 Modeling\n",
"\n",
"We can start with the basic equation of: change = in - out\n",
"\n",
"$$\n",
"\\frac{dH}{dt} = (Hare Birth Rate) - (Hare Death Rate) \\\\\n",
"\\frac{dL}{dt} = (Lynx Birth Rate) - (Lynx Death Rate)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.3.1 Relevant Parameters](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.3.1-Relevant-Parameters)",
"section": "1.4.2.3.1 Relevant Parameters"
}
},
"source": [
"#### 1.4.2.3.1 Relevant Parameters\n",
"\n",
"| Parameter | Symbol | Value |\n",
"| - | :----: | :---: |\n",
"| Lynx/Hare Predation Rate | $a$ | 3.2 |\n",
"| Lynx/Hare Conversion | $b$ | 0.6 |\n",
"| Lynx/Hare Michaelis Constant| $c$ | 50 |\n",
"| Lynx Death Rate | $d$ | 0.56 |\n",
"| Hare Carrying Capacity| $k$ | 125 |\n",
"| Hare Reproduction Rate | $r$ | 1.6 |"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.3.2 Model Equations](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.3.2-Model-Equations)",
"section": "1.4.2.3.2 Model Equations"
}
},
"source": [
"#### 1.4.2.3.2 Model Equations\n",
"These parameters can be used to form a model:\n",
"\n",
"$$\n",
"\\frac{dH}{dt} = rH(1 - \\frac{H}{k}) - \\frac{aHL}{c + H} \\\\\n",
"\\frac{dL}{dt} = a \\frac{bHL}{c + H} - d*L\n",
"$$\n",
"\n",
"The focus of this tutorial is not on the development of these model equations, but do review the equations and try to make sense of them. It can help you in debugging steps later should you have an error."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.4.2.4 Programming and Plotting](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4-Programming-and-Plotting)",
"section": "1.4.2.4 Programming and Plotting"
}
},
"source": [
"### 1.4.2.4 Programming and Plotting"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.1 Step 1: Initialization](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.1-Step-1:-Initialization)",
"section": "1.4.2.4.1 Step 1: Initialization"
}
},
"source": [
"#### 1.4.2.4.1 Step 1: Initialization"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.1 Step 1: Initialization](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.1-Step-1:-Initialization)",
"section": "1.4.2.4.1 Step 1: Initialization"
}
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy.integrate import solve_ivp"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.2 Step 2: Default Parameter Values](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.2-Step-2:-Default-Parameter-Values)",
"section": "1.4.2.4.2 Step 2: Default Parameter Values"
}
},
"source": [
"#### 1.4.2.4.2 Step 2: Default Parameter Values"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.2 Step 2: Default Parameter Values](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.2-Step-2:-Default-Parameter-Values)",
"section": "1.4.2.4.2 Step 2: Default Parameter Values"
}
},
"outputs": [],
"source": [
"a = 3.2\n",
"b = 0.6\n",
"c = 50\n",
"d = 0.56\n",
"k = 125\n",
"r = 1.6"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.3 Step 3: Define the differential equations](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.3-Step-3:-Define-the-differential-equations)",
"section": "1.4.2.4.3 Step 3: Define the differential equations"
}
},
"source": [
"#### 1.4.2.4.3 Step 3: Define the differential equations"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.3 Step 3: Define the differential equations](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.3-Step-3:-Define-the-differential-equations)",
"section": "1.4.2.4.3 Step 3: Define the differential equations"
}
},
"outputs": [],
"source": [
"def deriv(t, y):\n",
" H, L = y\n",
" dHdt = r*H*(1-H/k) - a*H*L/(c+H)\n",
" dLdt = b*a*H*L/(c+H) - d*L\n",
" return [dHdt, dLdt]"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.4 Step 4: Integrate Differential Equations](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.4-Step-4:-Integrate-Differential-Equations)",
"section": "1.4.2.4.4 Step 4: Integrate Differential Equations"
}
},
"source": [
"#### 1.4.2.4.4 Step 4: Integrate Differential Equations"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.4 Step 4: Integrate Differential Equations](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.4-Step-4:-Integrate-Differential-Equations)",
"section": "1.4.2.4.4 Step 4: Integrate Differential Equations"
}
},
"outputs": [],
"source": [
"t = np.linspace(0, 70, 500) # time grid\n",
"IC = [20, 20] # initial conditions for H and L\n",
"soln = solve_ivp(deriv, (t[0], t[-1]), IC, t_eval=t) # compute solution\n",
"H, L = soln.y # unpack solution "
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.5 Step 5: Plot ](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.5-Step-5:-Plot)",
"section": "1.4.2.4.5 Step 5: Plot "
}
},
"source": [
"#### 1.4.2.4.5 Step 5: Plot "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"nbpages": {
"level": 4,
"link": "[1.4.2.4.5 Step 5: Plot ](https://jckantor.github.io/CBE30338/01.04-Python-Numeric-Integration-Revisited.html#1.4.2.4.5-Step-5:-Plot)",
"section": "1.4.2.4.5 Step 5: Plot "
}
},
"outputs": [
{
"data": {
"text/plain": [
"
"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}