<table>
 <tr align=left><td>
 <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>
</table>


```python
from __future__ import print_function

%matplotlib inline
import numpy
import matplotlib.pyplot as plt
```

Note to lecturers:  This notebook is designed to work best as a classic Jupyter Notebook with nbextensions 
* hide_input: to hide selected python cells particularly for just plotting
* RISE:  Interactive js slide presentations

## Why should I care?

*Because I have to be here for a requirement...*

*or I might actually need to solve something important...like can I ever retire?*

### An example: The Retirement Problem

$$A = \frac{P}{r} \left((1+r)^n - 1 \right)$$

$A$ is the total amount after n payments

$P$ is the incremental payment

$r$ is the interest rate per payment period

$n$ is the number of payments



Note that these can all be functions of $r$, $n$, and time

What is the minimum interest rate $r$ required to accrue \$1M over 20 years saving \\$1000 per month?

$$
A = \frac{P}{r} \left((1+r)^n - 1 \right)
$$


```python
def A(P, r, n):
    return P / r * ((1 + r)**n - 1)

P = 1000.
years = numpy.linspace(0,20,241)
n = 12*years
target = 1.e6
```


```python
# find the root using scipy's brentq method
from scipy.optimize import brentq
n_max = n[-1]
f = lambda r : target - A(P,r/12., n_max)
r_target = brentq(f,0.1,0.15)
```


```python
fig = plt.figure(figsize=(8,6))
fig.set_figwidth(fig.get_figwidth() * 2)

axes = fig.add_subplot(1, 2, 1)
for r in [0.02, 0.05, 0.08, 0.1, 0.12, 0.15]:
    axes.plot(years, A(P, r/12., n),label='r = {}'.format(r))
axes.plot(years, numpy.ones(years.shape) * target, 'k--',linewidth=2,label="Target")
axes.legend(loc='best')
axes.set_xlabel("Years",fontsize=18)
axes.set_ylabel("Annuity Value (Dollars)",fontsize=18)
axes.set_title("The Forward Problem $A(P,r,n)$",fontsize=20)
axes.grid()

axes = fig.add_subplot(1, 2, 2)
r = numpy.linspace(1.e-6,0.15,100)
target = 1.e6
axes.plot(A(P,r/12.,n_max),r,linewidth=2)
axes.plot(numpy.ones(r.shape) * target, r,'k--',linewidth=2)
axes.scatter(A(P,r_target/12,n_max),r_target,s=100,c='r')
axes.set_xlabel('Annuity Value (Dollars)',fontsize=18)
axes.set_title('The Inverse Problem $r(A,P,n)$, $r\geq$ {:.3f}'.format(r_target),fontsize=20)
axes.grid()
plt.show()
```

This is a rootfinding problem for a function of a single variable

### Another Example:  Boat race (numerical quadrature of arc length)
Given a river with coordinates $(x,f(x))$ find the total length actually rowed over a given interval $x\in [0,X]$

Example:  a sinusoidal river $$f(x) = A \sin x$$


```python
A=.5
fig = plt.figure(figsize=(8,6))
x = numpy.linspace(0,10,100)
plt.plot(x,A*numpy.sin(x),'r',linewidth=2,)
plt.xlabel('distance (km)')
plt.grid()
plt.title('The Boat Race: $f(x)=A\sin(x)')
plt.show()
```

We need to calculate the function $f(x)$'s arc-length from $[0, X]$ e.g.

\begin{align}
    L(X) &= \int_0^{X} \sqrt{1 + |f'(x)|^2} dx\\
         &= \int_0^{X} \sqrt{1 + A^2\cos^2(x)} dx\\
\end{align}

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.

## Why is this exciting?

Computers have revolutionized our ability to explore...

[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)

...and there is more to come!

3258e7545251c45e50940dc29a275d8ac087fd49
ipynb
Jupyter Notebook
extras/00_intro_numerical_methods-extra-examples.ipynb
janice-yang/intro-numerical-methods
bed376b6fe2098caba46e61c1ec53c65dba96b71
CC-BY-4.0
2015-09-08T17:51:42.000Z
2022-03-31T09:29:36.000Z
extras/00_intro_numerical_methods-extra-examples.ipynb
janice-yang/intro-numerical-methods
bed376b6fe2098caba46e61c1ec53c65dba96b71
CC-BY-4.0
2015-09-18T01:15:21.000Z
2020-02-05T21:15:19.000Z
extras/00_intro_numerical_methods-extra-examples.ipynb
janice-yang/intro-numerical-methods
bed376b6fe2098caba46e61c1ec53c65dba96b71
CC-BY-4.0
2015-09-08T18:23:36.000Z
2022-01-16T14:34:42.000Z
Qwen/Qwen-72B
1. YES
2. YES