Python - scipy fmin, giving the arguments to fmin
Question: I'm a bit of a newbie in Python. I'm writing a little piece of code in order
to find the minimum of a function:

    
    
    import os,sys,matplotlib,pylab
    import numpy as np
    from scipy.optimize import fmin
    
    par = [2., 0.5, 0.008]
    x1 = 0.4
    f2_2 = lambda x, param: param[0] * x**2 + param[1] * x + param[2]
    xmin = fmin(f2_2,x1,args = (par))
    
    print xmin
    

it should be very simple, however I am getting this error:

    
    
    "Traceback (most recent call last):
    
      File "prova.fmin.py", line 9, in <module>
    
     xmin = fmin(f2_2,x1,args = (par))
    
      File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 257, in fmin
    
     fsim[0] = func(x0)
    
      File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 176, in function_wrapper
    
     return function(x, *args)
    
    TypeError: <lambda>() takes exactly 2 arguments (4 given)"
    

Could someone help me in understanding this please?


Answer: I just tried this out. Looks like you need to say `(par,)` and not just
`(par)`. Note that `(par,)` is a tuple, with the variable `par` as a single
element, whereas `(par)` just evaluates to `par`: no tuple. The "`args`"
keyword of `fmin` expects to find a tuple, not `par`, which in this case is a
list.

Edit: Well, actually, it would seem that `args` doesn't mind receiving a list
either. But then, inside of `fmin`, when the function `f2_2` is called, `args`
is [unpacked](http://docs.python.org/2/tutorial/controlflow.html#arbitrary-
argument-lists), meaning its contents are now passed as arguments to `f2_2`.
This means that `f2_2` ends up getting four arguments, viz. `x`, `2`, `0.5`
and `0.008` in this case, as opposed to getting just the two arguments `x` and
`[2, 0.5, 0.008]`.

