[SciPy-User] is it possible to constrain the scipy.optimize.curve_fit function?

federico vaggi vaggi.federico@gmail....
Thu May 17 04:51:05 CDT 2012

```Afaik,

currently there is no 'automatic' way to do it.  However, in your case,
it's pretty easy.

Simply define:

def func (x, a,b, r):
a = abs(a)
b = abs(b)
r = abs(r)
return r + a*np.power(x,-b)

And that will do the trick.  If you need to more complex boundaries, you
can simply use a combination of period functions with a given amplitude or
what have you.  Alternatively, there are *a lot* of optimization libraries
available for Python that are not a part of scipy that offer the
possibility to specify boundaries.

For example:

http://newville.github.com/lmfit-py/
http://ab-initio.mit.edu/wiki/index.php/NLopt_Python_Reference

Federico

Date: Wed, 16 May 2012 18:20:27 +0200
> From: servant mathieu <servant.mathieu@gmail.com>
> Subject: [SciPy-User] is it possible to constrain the
>        scipy.optimize.curve_fit function?
> To: scipy-user@scipy.org
> Message-ID:
>        <CALnu5bM+c9L7taG_CBHdJhw7xpe5amHSVBRUEX7pa7gnRN+-7Q@mail.gmail.com
> >
> Content-Type: text/plain; charset="iso-8859-1"
>
> Dear scipy users,
>
> I'm trying to fit to data a power law of the form :
>
>
>
>
> def func (x, a,b, r):
>
>      return r + a*np.power(x,-b)
>
>
>
>
> I would like to constrain the curve_fit routine to only allow
> positive parameter values. How is it possible to do so?
>
>
>
> Kind regards,
>
> Mathieu
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120517/e9831cd9/attachment.html
```