"""
Newton-Raphson which works with lambda functions.
 
EXAMPLES:
    sage: attach 'newton_raphson.sage'
    sage: f = lambda x : -49*exp(x/2) + 251*exp(-x/2) - 1
    sage: newton_raphson(f,1.6,0.000001,0.00001)
    1.6246136830923981
"""
def newton_raphson(f, x, dx, eps):
    df = lambda x : (f(x+dx)-f(x))/dx
    i = ZZ(0)
    while i<ZZ(1000):
        i=i+ZZ(1)
        x1 = x-f(x)/df(x)
        t = abs(x1-x)
        if t < eps: break
        x = x1
    return x
