# [SciPy-user] scipy.optimize.leastsq failure

Steve Schmerler elcorto@gmx....
Mon Aug 27 04:25:52 CDT 2007

```fdu.xiaojf@gmail.com wrote:
> Hi,
>
> I get an ier flag of 2 when running the following code:
>
> ----------------------------------------------------------
>>from scipy.optimize import leastsq
>>from numpy import array
>
> x = array([1.2, 3.4, 5.6, 7.8])
> y = (x-1.234)**2+3.456
>
> def obj_f(pp, xx, yy):
>      return pp[0] * (xx-pp[1])**2 + pp[2] - yy
>
> b = leastsq(obj_f, (1., 1., 2.), args=(x,y), full_output=2)
> print b
> print obj_f(b[0], x, y)
> ----------------------------------------------------------
>
> The output is:
> ----------------------------------------------------------
> (array([ 1.   ,  1.234,  3.456]), array([[ 0.01067209,  0.03485503,  0.04927042],
>         [ 0.03485503,  0.12416711,  0.22839652],
>         [ 0.04927042,  0.22839652,  0.91824454]]), {'qtf': array([
> -5.72325346e-10,  -3.96833152e-10,  -2.98440470e-11]), 'nfev': 13, 'fjac':
> array([[-47.37134237,   0.09903785,   0.40239425,   0.9100936 ],
>         [ 15.89408601,   3.85306656,   0.61349297,  -0.32983294],
>         [ -1.41155008,  -0.95837977,   1.04356817,   0.82836952]]), 'fvec':
> array([ -8.88178420e-16,   0.00000000e+00,   0.00000000e+00,
>          -7.10542736e-15]), 'ipvt': array([1, 2, 3])}, 'The relative error
> between two consecutive iterates is at most 0.000000', 2)
> [ -8.88178420e-16   0.00000000e+00   0.00000000e+00  -7.10542736e-15]
> ----------------------------------------------------------
>
> Tt seems that the result is rather accurate, but while I get an ier flag of 2?
> I don't quite understand the error message.
>

leastq() uses the underlying Fortran routines from MINPACK: LMDIF if you don't
provide a Jacobain (Dfun=None) or LMDER otherwise. The message tells you that
the routine met one of it's convergence criteria. See the source of leastsq()
in optimize/minpack.py and the docstrings of the Fortran routines ('info' flag):
http://www.netlib.org/minpack/lmdif.f
http://www.netlib.org/minpack/lmder.f

--
cheers,
steve

I love deadlines. I like the whooshing sound they make as they fly by. --