#!/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 numerical derivative of a function $f$ is a formula of the type # \\[ # f'(x_0) \simeq \frac{f(x_0+h/2) - f(x_0-h/2)}{h} # \\] # Implement a function `derivator` which returns the derivative *function*. # In[ ]: def derivator(f, h): pass # In[ ]: msin_ = derivator(cos, 1e-10) # In[ ]: assert(allclose(msin_(0), 0)) assert(allclose(msin_(pi), 0)) assert(allclose(msin_(pi/2), -1.))