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é

_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to