# [SciPy-User] problem with the fmin function

josef.pktd@gmai... josef.pktd@gmai...
Mon Apr 30 06:08:30 CDT 2012

```On Mon, Apr 30, 2012 at 5:15 AM, servant mathieu
<servant.mathieu@gmail.com> wrote:
> Dear scipy users,
>
> I'm trying to minimize a chi-square value by a simplex routine. My code
> contains two functions: the first one, called spotdiffusion(a, ter , v , sda
> , rd) generates some simulated data for a given set of parameter values. The
> second function, called chis ( a, ter , v , sda , rd), computes a chi-square
> value from the comparison of observed and simulated data, for a given set of
> parameter values. To do so, the chis function calls the spotdiffusion one.
> Here is a simplified structure of my code:
>
> def spotdiffusion (a, ter , v , sda , rd):
>              do simulation computations
>              returns an array of simulated data
>
> def chis (a, ter , v , sda , rd):

the parameter is just one list or array, and you need to unpack, for example

def chis (x):
a, ter , v , sda , rd = x

Josef

>               observed data
>               simulated_data = spotdiffusion ( a, ter , v , sda , rd)
>               do chi-square computations
>               returns the chi-square value (float)
>
> Now I want now to find a, ter, v, sda and rda values which minimize the
> chi-square. Here is my attempt:
>
> x0 = np.array ([0.11,0.25,0.35, 1.7, 0.017]) ####initial guess
> xopt = fmin (chis, x0, maxiter=300)
>
> However, python returns an error:
> Traceback (most recent call last):
>   File "<ipython console>", line 1, in <module>
>   File
> "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\startup.py",
> line 128, in runfile
>     execfile(filename, glbs)
>   File "C:\Users\mathieu\Desktop\modeling\spotlight diffusion
> model\fitting_spotlight.py", line 246, in <module>
>     xopt = fmin (chis, x0, maxiter=300)
>   File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 257,
> in fmin
>     fsim[0] = func(x0)
>   File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 176,
> in function_wrapper
>     return function(x, *args)
> TypeError: chis() takes exactly 5 arguments (1 given)
>
> I don't understand where is my mistake. Any help would be appreciated!
>
> Cheers,
> Mathieu
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```