High order functions - receive lambda func for derivative calculation

Viewed 41

Im new to python and ive been trying to implement a numerical derivative high order function but I can't modify my code to have the desired result:

def deriv(f):
    return lambda x: ((f(x + h) - f(x)) / h)

But when calling the function im getting an error i cant iterpret.

1 Answers

Welcome to StackOverflow! Next time I suggest adding the error description in order to help us help you better.

In this case, I suppose you are probably missing the definition of the variable h. try adding this line to your code:

def derivative(f):
    h=0.000001
    return lambda x: ((f(x + h) - f(x)) / h)

or:

def deriv(f, h=0.000001):
    return lambda x: ((f(x + h) - f(x)) / h)
Related