# [SciPy-user] optimize.leastsq()

Francesc Alted falted at pytables.org
Thu Oct 21 13:20:50 CDT 2004

```Hi,

Sorry for the bunch of questions, but I'm preparing some 'hands-on' on
scipy, and I'm excercising many parts of it.

I've tried to follow the Travis' tutorial for finding the best fit of a
series of data, but my results are different from those stated there:

In [96]: x = arange(0,6e-2,6e-2/30)

In [97]: A,k,theta = 10, 1.0/3e-2, pi/6

In [98]: y_true = A*sin(2*pi*k*x+theta)

In [99]: y_meas = y_true + 2*randn(len(x))

In [100]: def residuals(p, y, x):
.....:     A,k,theta = p
.....:     err = y-A*sin(2*pi*k*x+theta)
.....:     return err
.....:

In [101]: p0 = [8, 1/2.3e-2, pi/3]

In [107]: optimize.leastsq(residuals, p0, args=(y_meas, x))
Out[107]:
(array([ -2.17634575,  57.4116454 ,  -3.03266312]),
'Both actual and predicted relative reductions in the sum of squares\n  are at most 0.000000')

this results are quite different from the 'true' values:
[ 10. 33.3333 0.5236]

Do you have any idea about this error? I suppose that the message should
indicate it, but I can't understand it. I'm using scipy 0.3.2 on Linux.

BTW, thanks for clearifying the question about derivative() parameters. It's
much more clear now.

--
Francesc Alted

```

More information about the SciPy-user mailing list