[Edu-sig] Re: More on intro to Python (today's 3 hr training)

André Roberge andre.roberge at gmail.com
Tue Mar 29 14:24:24 CEST 2005


Kirby Urner wrote:
> [snip]
> 
> 
>     
> def derivative(f):
>     """
>     Factory function:  accepts a function, returns a closure
>     """
>     def df(x, h=1e-8):
>         return (f(x + h) - f(x))/h
>     return df
> 
Sorry for being picky, but while your derivative factory
function follows the mathematical definition, it is not the
"best" way to do it numerically.  The preferred way is:

  def derivative(f):
      """
      Factory function:  accepts a function, returns a closure
      """
      def df(x, h=1e-8):
          return (f(x + h/2) - f(x - h/2))/h
      return df

This is best seen by doing a graph (say a parabola) and drawing
the derivative with a large "h" using both methods near a local
minimum.

André



More information about the Edu-sig mailing list