[SciPy-user] optimize.leastsq()

Travis Oliphant oliphant at ee.byu.edu
Thu Oct 21 14:01:37 CDT 2004


Francesc Alted wrote:

>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.
>  
>
Can you look at the output of peval(x,p) and compare it to y_true?

I just re-ran this experiment and get
array([-10.053690336957587,  33.659758123232756,   3.591816659730455)])

which seems right (except for the fact that some of the phase has been 
used to give -10 instead of 10).

-Travis O.




More information about the SciPy-user mailing list