[SciPy-user] leastsq not converging with tutorial example

Alan G Isaac aisaac at american.edu
Thu Oct 27 14:11:27 CDT 2005


On Wed, 26 Oct 2005, Kael Fischer apparently wrote: 
> I get back the initial parameters and the "The cosine of 
> the angle between func(x) and any column of the\n  
> Jacobian is at most 0.000000 in absolute value" status 
> message.  Anybody know what is going on. 

> # Based on Scipy leastsq section of the tutorial 
> from scipy import * 
> from scipy.optimize import leastsq 
> x = arange(0,6e-2,6e-2/30) 
> A,k,theta = 10, 1.0/3e-2, pi/6 
> y_true = A*sin(2*pi*k*x+theta) 
> y_meas = y_true + 2*randn(len(x)) 

> def residuals(p, y, x): 
>     A,k,theta = p 
>     err = y-A*sin(2*pi*k*x+theta) 
>     return err 

> p0 = [8, 1/2.3e-2, pi/3] 
> print array(p0) 

> plsq = leastsq(residuals, p0, args=(y_meas, x),full_output=True ) 
> print plsq 


I've lost track of this conversation:
are you working with the old SciPy?
If so, I get a fine fit.
(There is no unique solution.)
Output below.  
Cheers,
Alan Isaac
 
([-10.08803863, 33.35407647,  3.66224999,], {'qtf': 
[-1.42364335e-006,-7.54678390e-006, 8.60238959e-007,], 
'nfev': 67, 'fjac': [[ 3.90809167e+001, 1.52285596e-001, 
5.42796568e-002,-5.31232430e-002,
       -1.51329383e-001,-2.23337196e-001,-2.56680604e-001,-2.45587151e-001,
       -1.91977358e-001,-1.05132214e-001,-8.64509291e-005, 1.04974277e-001,
        1.91861732e-001, 2.45533859e-001, 2.56698861e-001, 2.23423839e-001,
        1.51469423e-001, 5.32924326e-002,-5.41106080e-002,-1.52145958e-001,
       -2.23841613e-001,-2.56785538e-001,-2.45274439e-001,-1.91301134e-001,
       -1.04209544e-001, 9.22927844e-004, 1.05895621e-001, 1.92535537e-001,
        2.45843467e-001, 2.56590679e-001,]
 [ 7.47608840e+000,-4.30094645e+000, 6.46929896e-002,-5.72488268e-002,
       -1.45802107e-001,-1.89678003e-001,-1.88687038e-001,-1.52489724e-001,
       -9.72813755e-002,-4.12694610e-002,-2.40662882e-005, 1.72343990e-002,
        9.59154323e-003,-1.57616526e-002,-4.57896244e-002,-6.53658172e-002,
       -6.16101164e-002,-2.77618802e-002, 3.43667671e-002, 1.14003855e-001,
        1.93285265e-001, 2.51053205e-001, 2.67805817e-001, 2.30718203e-001,
        1.37580822e-001,-1.32390172e-003,-1.63990371e-001,-3.20145990e-001,
       -4.36857539e-001,-4.85253949e-001,]
 [-1.46106598e-003,-4.38759204e-001,-3.84704098e+000, 2.74702726e-001,
        2.64053433e-001, 2.02700946e-001, 1.03168178e-001,-1.48774649e-002,
       -1.28450677e-001,-2.15668814e-001,-2.59924688e-001,-2.53022660e-001,
       -1.96689943e-001,-1.02185423e-001, 1.19103083e-002, 1.23295947e-001,
        2.10251560e-001, 2.55820643e-001, 2.51075021e-001, 1.96840417e-001,
        1.03552770e-001,-1.07296399e-002,-1.23782838e-001,-2.13486879e-001,
       -2.62096394e-001,-2.59696278e-001,-2.06177840e-001,-1.11348387e-001,
        6.86050421e-003, 1.25759521e-001,]], 'fvec': [-0.12272512,-0.06597184,-0.12726937,-0.37158314, 0.37928372, 0.16812751,
      -0.15770147,-0.36697812, 0.12214352,-0.23236439,-0.18302132, 0.00113977,
       0.17116167, 0.03007847,-0.42073783, 0.25313819, 0.05608965,-0.03051878,
      -0.18438834,-0.06210419, 0.25409787,-0.14341716,-0.32881238, 0.10358692,
       0.35464075,-0.20294016,-0.10304636, 0.13489069,-0.12251931,-0.06129922,], 'ipvt': [3,2,1,]}, 1, 'Both actual and predicted relative reductions in the sum of squares\n  are at most 0.000000')
True:   (10, 33.333333333333336, 0.52359877559829882)
Start:  [  8.        , 43.47826087,  1.04719755,]
End:    [-10.08803863, 33.35407647,  3.66224999,]





More information about the SciPy-user mailing list