# [SciPy-User] help with leastsq and fmin

josef.pktd@gmai... josef.pktd@gmai...
Wed Sep 8 17:42:26 CDT 2010

```On Wed, Sep 8, 2010 at 6:06 PM, Joses Ho <joses.ho@well.ox.ac.uk> wrote:
> Hi,
> I am trying to fit to my qPCR data
> an exponential function
> where each successive y-value depends on the
> previous v-value in the following fashion:
> y[n]  =  y[n-1] + k ln (1 + (y[n-1]/k) + b
> where y[n] = current y-value being evaluated
> and y[n-1] = previous y-value
>
> I therefore have three values to fit - y0 (starting value of y), k, and b.

you might need good starting values because of the recursive non-linearity.

For simpler non-linearities in the recursion, leastsq worked ok for me.

>
> I define a residuals function where I
> compute 20 values of y,
>  given an initial estimate of y0, k and b, and
> then subtract these from my 20
> measured values of y.
>
> I have tried to use scipy.optimize.leastsq on the
> above residuals function, but it doesn't seem to be
> converging on the correct values of y0,k and b.
> I have tried scipy.optimize.fmin on the same function,

same function ? see below

> but I get the following error:
> ...fsim[0] = func(x0)
> ...ValueError: setting an array element with a sequence.
> which seems to me that it refuses to accept the x0 array
> I give to it, consisting of [y0,k,b].

Is your objective function returning a single value?

leastsq wants the array of error terms
fmin wants the final value of the objective

So we cannot use the same function directly in leastsq and fmin.

I usually get this kind of exception when I forget this.

Josef

>
> Basically, I am trying to implement the recent method of Boggy and Woolf
> (PLoS 2010) [doi:10.1371/ journal.pone.0012355] in Python.
> I am using SciPy 0.8.0b1 and NumPy 1.4.0 on
> Python 2.6.5 of the Enthought Python Distribution 6.2-2.
>
> Any help here would be very much appreciated!
>
> Thanks
> Joses
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
```