#!/usr/bin/env python # coding: utf-8 # ## Exercise for the course [Python for MATLAB users](http://sese.nu/python-for-matlab-users-ht15/), by Olivier Verdier # In[1]: get_ipython().run_line_magic('pylab', '') get_ipython().run_line_magic('matplotlib', 'inline') # 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*. # # This method starts from the given interval. Then it investigates the sign changes in the subintervals $[a,\frac{a+b}{2}]$ # and $[\frac{a+b}{2},b]$. If the sign changes in the first subinterval $ b$ is redefined to be # $b:=\frac{a+b}{2}$ # otherwise $a$ is redefined in the same manner to # $a:=\frac{a+b}{2}$, # and the process is repeated until the $b-a$ is less than a given tolerance. # # In[ ]: def bisect(f, a, b): return 0 # implement this! # Implement the function `bisect` above until the following does not complain: # In[ ]: assert allclose(bisect(sin, 3., 4.), pi) # In[ ]: