By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

derivative of Newton-raphson

P: 2
I can't get this code to work, what is wrong?

If I type:
Expand|Select|Wrap|Line Numbers
  1. >>>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: ->
Expand|Select|Wrap|Line Numbers
  1. def derivative (f,x,h):
  2.     import math
  3.     return float(1/(2*h)) * (f(x+h) - f(x-h))
Sep 25 '10 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 2,851
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.
Expand|Select|Wrap|Line Numbers
  1. import math
  3. def derivative (f,x,h):
  4.     return 1.0/(2*h) * (f(x+h) - f(x-h))
  6. print derivative(math.sin, math.pi, 0.0001)
Sep 25 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.