# [SciPy-User] simplex algorithm and curve fitting

servant mathieu servant.mathieu@gmail....
Tue Feb 21 04:19:49 CST 2012

```Thanks a lot Joseph. I've got a last question which concerns initials guess
for parameters. In fact, in many papers, i read " we fitted both functions
using standard simplex optimization routines. This was repeated 10000 times
with randomized initial values to avoid local minima."  The problemis the
following: which range of values should we use for this randomization?

Best,
Mat

2012/2/21 <josef.pktd@gmail.com>

> 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
> >
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120221/6d872e56/attachment.html
```