Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 318 additions & 0 deletions 00_intro_numerical_methods-extra-examples.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"hide_input": false,
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"<table>\n",
" <tr align=left><td><img align=left src=\"./images/CC-BY.png\">\n",
" <td>Text provided under a Creative Commons Attribution license, CC-BY. All code is made available under the FSF-approved MIT license. (c) Kyle T. Mandli</td>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": false,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"from __future__ import print_function\n",
"\n",
"%matplotlib inline\n",
"import numpy\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Note to lecturers: This notebook is designed to work best as a classic Jupyter Notebook with nbextensions \n",
"* hide_input: to hide selected python cells particularly for just plotting\n",
"* RISE: Interactive js slide presentations"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## Why should I care?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"*Because I have to be here for a requirement...*"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"*or I might actually need to solve something important...like can I ever retire?*"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"### An example: The Retirement Problem\n",
"\n",
"$$A = \\frac{P}{r} \\left((1+r)^n - 1 \\right)$$\n",
"\n",
"$A$ is the total amount after n payments\n",
"\n",
"$P$ is the incremental payment\n",
"\n",
"$r$ is the interest rate per payment period\n",
"\n",
"$n$ is the number of payments\n",
"\n",
"\n",
"\n",
"Note that these can all be functions of $r$, $n$, and time"
]
},
{
"cell_type": "markdown",
"metadata": {
"hide_input": true,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"What is the minimum interest rate $r$ required to accrue \\$1M over 20 years saving \\\\$1000 per month?\n",
"\n",
"$$\n",
"A = \\frac{P}{r} \\left((1+r)^n - 1 \\right)\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": false,
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"def A(P, r, n):\n",
" return P / r * ((1 + r)**n - 1)\n",
"\n",
"P = 1000.\n",
"years = numpy.linspace(0,20,241)\n",
"n = 12*years\n",
"target = 1.e6"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": false,
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"# find the root using scipy's brentq method\n",
"from scipy.optimize import brentq\n",
"n_max = n[-1]\n",
"f = lambda r : target - A(P,r/12., n_max)\n",
"r_target = brentq(f,0.1,0.15)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": true,
"jupyter": {
"source_hidden": true
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"fig = plt.figure(figsize=(8,6))\n",
"fig.set_figwidth(fig.get_figwidth() * 2)\n",
"\n",
"axes = fig.add_subplot(1, 2, 1)\n",
"for r in [0.02, 0.05, 0.08, 0.1, 0.12, 0.15]:\n",
" axes.plot(years, A(P, r/12., n),label='r = {}'.format(r))\n",
"axes.plot(years, numpy.ones(years.shape) * target, 'k--',linewidth=2,label=\"Target\")\n",
"axes.legend(loc='best')\n",
"axes.set_xlabel(\"Years\",fontsize=18)\n",
"axes.set_ylabel(\"Annuity Value (Dollars)\",fontsize=18)\n",
"axes.set_title(\"The Forward Problem $A(P,r,n)$\",fontsize=20)\n",
"axes.grid()\n",
"\n",
"axes = fig.add_subplot(1, 2, 2)\n",
"r = numpy.linspace(1.e-6,0.15,100)\n",
"target = 1.e6\n",
"axes.plot(A(P,r/12.,n_max),r,linewidth=2)\n",
"axes.plot(numpy.ones(r.shape) * target, r,'k--',linewidth=2)\n",
"axes.scatter(A(P,r_target/12,n_max),r_target,s=100,c='r')\n",
"axes.set_xlabel('Annuity Value (Dollars)',fontsize=18)\n",
"axes.set_title('The Inverse Problem $r(A,P,n)$, $r\\geq$ {:.3f}'.format(r_target),fontsize=20)\n",
"axes.grid()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"This is a rootfinding problem for a function of a single variable"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"### Another Example: Boat race (numerical quadrature of arc length)\n",
"Given a river with coordinates $(x,f(x))$ find the total length actually rowed over a given interval $x\\in [0,X]$\n",
"\n",
"Example: a sinusoidal river $$f(x) = A \\sin x$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide_input": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"A=.5\n",
"fig = plt.figure(figsize=(8,6))\n",
"x = numpy.linspace(0,10,100)\n",
"plt.plot(x,A*numpy.sin(x),'r',linewidth=2,)\n",
"plt.xlabel('distance (km)')\n",
"plt.grid()\n",
"plt.title('The Boat Race: $f(x)=A\\sin(x)')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"We need to calculate the function $f(x)$'s arc-length from $[0, X]$ e.g.\n",
"\n",
"\\begin{align}\n",
" L(X) &= \\int_0^{X} \\sqrt{1 + |f'(x)|^2} dx\\\\\n",
" &= \\int_0^{X} \\sqrt{1 + A^2\\cos^2(x)} dx\\\\\n",
"\\end{align}\n",
"\n",
"In general the value of this integral cannot be solved analytically (the answer is actually an elliptic integral). We usually need to approximate the integral using a numerical quadrature."
]
},
{
"cell_type": "markdown",
"metadata": {
"hide_input": true,
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## Why is this exciting?\n",
"\n",
"Computers have revolutionized our ability to explore...\n",
"\n",
"[Top 10 Algorithms of the 20th Century](http://www.siam.org/pdf/news/637.pdf?t=1&cn=ZmxleGlibGVfcmVjcw%3D%3D&refsrc=email&iid=658bdab6af614c83a8df1f8e02035eae&uid=755271476&nid=244+285282312)\n",
"\n",
"...and there is more to come!"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.5"
},
"latex_envs": {
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 0
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading