# [SciPy-User] simplex algorithm and curve fitting

josef.pktd@gmai... josef.pktd@gmai...
Tue Feb 21 04:05:58 CST 2012

```On Tue, Feb 21, 2012 at 4:38 AM, servant mathieu
<servant.mathieu@gmail.com> wrote:
> Dear Scipy users,
>
> I've got some troubles with the scipy.optimize.curve_fit function. This
> function is based on the Levenburg-Maquardt algorithm, which is extremely
> rapid but usually finds a local minimum, not a global one (and thus often
> returns anormal parameter values). In my research field, we usually use
> Nelder Mead's simplex routines to avoid this problem. However, I don't know
> if it is possible to perform curve fitting in scipy using simplex; the fmin
> function  doesn't seem to perform adjustments to data.
>
> Here is my code for fitting a three parameters hyperbolic cotangent function
> using curve_fit:
>
> from scipy.optimize import curve_fit
>
> import numpy as np
>
>
>
> def func (x, A,k, r ):
>
> return r + (A /(k*x)) * np.tanh (A*k*x)
>
>
>
> xdata = np.array ([0.15,0.25,0.35,0.45, 0.55, 0.75])
>
>
>
> datacomp = np.array ([344.3276300, 324.0051063, 314.2693475,
> 309.9906375,309.9251162, 307.3955800])
>
> dataincomp = np.array ([363.3839888, 343.5735787, 334.6013375, 327.7868238,
> 329.4642550, 328.0667050])
>
>
>
> poptcomp, pcovcomp = curve_fit (func, xdata, datacomp, maxfev = 10000)
>
> poptincomp, pcovincomp = curve_fit (func, xdata, dataincomp, maxfev = 10000)
>
>
>
>
>
> How could I proceed to perform the fitting using simplex?

You need to define your own loss function for the optimizers like
fmin, or in future version minimize

something like this

def loss(params, args)
A, k, r = params
y, x = args
return ((y - func (x, A,k, r ))**2).sum()

and use loss in the call to fmin

Josef

>
>
>
> Best,
>
> Mat
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```