Created
December 11, 2015 11:33
-
-
Save olivierverdier/f91c3c0c9f85ba4b53c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Exercise for the course [Python for MATLAB users](http://sese.nu/python-for-matlab-users-ht15/), by Olivier Verdier" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Using matplotlib backend: MacOSX\n", | |
"Populating the interactive namespace from numpy and matplotlib\n" | |
] | |
} | |
], | |
"source": [ | |
"%pylab\n", | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"A continuous functions $f$ which changes its sign in an interval $[a,b]$, i.e. $f(a)f(b)< 0$, has at least one root in this interval. Such a root can be found by the *bisection method*. \n", | |
"\n", | |
"This method starts from the given interval. Then it investigates the sign changes in the subintervals $[a,\\frac{a+b}{2}]$ \n", | |
"and $[\\frac{a+b}{2},b]$. If the sign changes in the first subinterval $ b$ is redefined to be \n", | |
"$b:=\\frac{a+b}{2}$\n", | |
"otherwise $a$ is redefined in the same manner to \n", | |
"$a:=\\frac{a+b}{2}$,\n", | |
"and the process is repeated until the $b-a$ is less than a given tolerance.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def bisect(f, a, b):\n", | |
" tol = 1e-9\n", | |
" for i in range(100):\n", | |
" if (b-a) < tol:\n", | |
" break\n", | |
" c = (a+b)/2\n", | |
" if f(a)*f(c) <= 0:\n", | |
" b = c\n", | |
" else:\n", | |
" a = c\n", | |
" return c" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Implement the function `bisect` above until the following does not complain:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def lin(x):\n", | |
" return x\n", | |
"assert allclose(bisect(sin, 3., 4.), pi)\n", | |
"assert allclose(bisect(lin, -1,1), 0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment