Search code examples
pythonscipytypeerrornewtons-method

scipy.optimize.newton gives TypeError: 'float' object is not callable


Im new to python and I was writing this simply code for finding the roots of the function:

from scipy import optimize

x = eval(raw_input())                           #Initial guess
f = eval(raw_input())                           # function to be evaluated
F = eval(raw_input())                          #derivative of function f

round(optimize.newton(f, x, F, tol = 1.0e-9), 4)

But the interpreter returns: TypeError: 'float' object is not callable

I'm really not sure what im missing out from this code. Can someone help me out..thank you in advance


Solution

  • optimize.newton expects a reference to a callable object (for example a function). That does not mean that you give a function as a string like 'x*x' but you have to define one first, like:

    def my_func (x):
        return x*x
    

    Then you can plug my_func into optimize.newton (besides the other required parameters).