Skip to content

Instantly share code, notes, and snippets.

@olivierverdier
Created November 20, 2014 11:48
Show Gist options
  • Save olivierverdier/54d99f91df6ae9e969ed to your computer and use it in GitHub Desktop.
Save olivierverdier/54d99f91df6ae9e969ed to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"worksheets": [
{
"cells": [
{
"metadata": {},
"cell_type": "heading",
"source": "Exercise for the course [Python for MATLAB users](http://sese.nu/python-for-matlab-users/), by Olivier Verdier",
"level": 2
},
{
"metadata": {},
"cell_type": "code",
"input": "%pylab\n%matplotlib inline\nfrom __future__ import division",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "from __future__ import division",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"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\nThis method starts from the given interval. Then it investigates the sign changes in the subintervals $[a,\\frac{a+b}{2}]$ \nand $[\\frac{a+b}{2},b]$. If the sign changes in the first subinterval $ b$ is redefined to be \n$b:=\\frac{a+b}{2}$\notherwise $a$ is redefined in the same manner to \n$a:=\\frac{a+b}{2}$,\nand the process is repeated until the $b-a$ is less than a given tolerance.\n"
},
{
"metadata": {},
"cell_type": "code",
"input": "def bisect(f, a, b):\n return 0\n # implement this!",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Implement the function `bisect` above until the following does not complain:"
},
{
"metadata": {},
"cell_type": "code",
"input": "assert allclose(bisect(sin, 3., 4.), pi)",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
}
],
"metadata": {}
}
],
"metadata": {
"name": "",
"signature": "sha256:b45cf2ca7041b6c208b8a13df5a0d0ab83d201a54dacc8bf5afe6f33ca0c6452"
},
"nbformat": 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment