1

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

user277746
  • 13
  • 4

2 Answers2

1

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).

a_guest
  • 34,165
  • 12
  • 64
  • 118
0

This will depend on what you are inputting for f. If you enter something like

lambda x: x ** 2

then it will be interpreted as a function, for example

In [83]: f = eval('lambda x: x ** 2')

In [84]: f(5)
Out[84]: 25
user2085282
  • 1,077
  • 1
  • 8
  • 16