Skip to content

Commit

Permalink
Extended notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
rwest committed Sep 28, 2016
1 parent 0a743bc commit 98c1acf
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions rabbits-and-foxes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,69 @@
"*This problem is based on one from Chapter 1 of H. Scott Fogler's textbook \"Essentials of Chemical Reaction Engineering\".*\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Solving ODEs\n",
"\n",
"*Much of the following content reused under Creative Commons Attribution license CC-BY 4.0, code under MIT license (c)2014 L.A. Barba, G.F. Forsyth. Partly based on David Ketcheson's pendulum lesson, also under CC-BY. https://github.com/numerical-mooc/numerical-mooc*\n",
"\n",
"Let's step back for a moment. Suppose we have a first-order ODE $u'=f(u)$. You know that if we were to integrate this, there would be an arbitrary constant of integration. To find its value, we do need to know one point on the curve $(t, u)$. When the derivative in the ODE is with respect to time, we call that point the _initial value_ and write something like this:\n",
"\n",
"$$u(t=0)=u_0$$\n",
"\n",
"In the case of a second-order ODE, we already saw how to write it as a system of first-order ODEs, and we would need an initial value for each equation: two conditions are needed to determine our constants of integration. The same applies for higher-order ODEs: if it is of order $n$, we can write it as $n$ first-order equations, and we need $n$ known values. If we have that data, we call the problem an _initial value problem_.\n",
"\n",
"Remember the definition of a derivative? The derivative represents the slope of the tangent at a point of the curve $u=u(t)$, and the definition of the derivative $u'$ for a function is:\n",
"\n",
"$$u'(t) = \\lim_{\\Delta t\\rightarrow 0} \\frac{u(t+\\Delta t)-u(t)}{\\Delta t}$$\n",
"\n",
"If the step $\\Delta t$ is already very small, we can _approximate_ the derivative by dropping the limit. We can write:\n",
"\n",
"$$\\begin{equation}\n",
"u(t+\\Delta t) \\approx u(t) + u'(t) \\Delta t\n",
"\\end{equation}$$\n",
"\n",
"With this equation, and because we know $u'(t)=f(u)$, if we have an initial value, we can step by $\\Delta t$ and find the value of $u(t+\\Delta t)$, then we can take this value, and find $u(t+2\\Delta t)$, and so on: we say that we _step in time_, numerically finding the solution $u(t)$ for a range of values: $t_1, t_2, t_3 \\cdots$, each separated by $\\Delta t$. The numerical solution of the ODE is simply the table of values $t_i, u_i$ that results from this process.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler's method\n",
"*Also known as \"Simple Euler\" or sometimes \"Simple Error\".*\n",
"\n",
"The approximate solution at time $t_n$ is $u_n$, and the numerical solution of the differential equation consists of computing a sequence of approximate solutions by the following formula, based on Equation (10):\n",
"\n",
"$$u_{n+1} = u_n + \\Delta t \\,f(u_n).$$\n",
"\n",
"This formula is called **Euler's method**.\n",
"\n",
"For the equations of the rabbits and foxes, Euler's method gives the following algorithm that we need to implement in code:\n",
"\n",
"\\begin{align}\n",
"R_{n+1} & = R_n + \\Delta t \\left(k_1 R_n - k_2 R_n F_n \\right) \\\\\n",
"F_{n+1} & = F_n + \\Delta t \\left( k_3 R_n F-n - k_4 F_n \\right).\n",
"\\end{align}\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 98c1acf

Please sign in to comment.