derivative of Newton-raphson

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Viktor Sundelin
    New Member
    • Sep 2010
    • 2

    derivative of Newton-raphson

    Hello!
    I can't get this code to work, what is wrong?


    If I type:
    Code:
    >>>derivative(math.sin, math.pi, 0.0001)
    Shouldn't I get a value in return?
    This is the error massage:

    TypeError: 'float' object is not callable

    This is the function definition: ->
    Code:
    def derivative (f,x,h):
        import math
        return float(1/(2*h)) * (f(x+h) - f(x-h))
    Last edited by bvdet; Sep 25 '10, 04:31 PM. Reason: Fix code tags, indentation
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I think you assigned the identifier float to a real number, effectively masking built-in function float(). There is no need to type cast your calculation to float as long as one of the values is a float.
    Code:
    import math
    
    def derivative (f,x,h):
        return 1.0/(2*h) * (f(x+h) - f(x-h))
    
    print derivative(math.sin, math.pi, 0.0001)

    Comment

    Working...