2

I am new to scipy, and the following code doesn't seem to work:

from scipy import optimize

def f(x, y):
    return x * x - 3 + y


def main():
   x0 = 0.1
    y = 1
    res = optimize.newton(f(x0,y), x0)
    print (res)

The error I receive is:

 File "C:\Python27\lib\site-packages\scipy\optimize\zeros.py", line 144, in newton
    q0 = func(*((p0,) + args))
TypeError: 'float' object is not callable
K DawG
  • 13,287
  • 9
  • 35
  • 66
insomniac
  • 192
  • 1
  • 3
  • 16

1 Answers1

8

You need to supply a function as the first argument of optimize.newton. The guess x0 for the independent parameter is supplied as the second argument and you can use args to supply constant parameters:

def f(x, y):
    return x * x - 3 + y

def main():
    x0 = .1
    y = 1
    res = optimize.newton(f, x0, args=(y,))
David Zwicker
  • 23,581
  • 6
  • 62
  • 77
  • I have a follow-up question. args can be declared as a tuple, so if I presume I can simple declare: args0 = y, and then declare res = optimize.newton(f, x0, args = args0)? – insomniac Dec 13 '13 at 13:56
  • It has to be a tuple, so you have to declare `args0 = (y,)`. If you would have an additional parameter `z`, you could of course also add it as `args0 = (y, z)`. – David Zwicker Dec 13 '13 at 14:00