[SciPy-user] optimize.leastsq() question

Frank Palazzolo palazzol at comcast.net
Mon Dec 4 21:18:44 CST 2006


Thanks for looking.  The only reason I passed in the err variable was 
because in my actual program - which is much more complicated - it was 
more convenient to create and allocate err once, outside of the callback 
function.  I know this is probably not significant for a Python program, 
but I did it that was because this is a prototype for a C++ implementation.

Thanks for letting me know about numpy.random.  I am trying to get all 
of the non-numpy stuff out of my app :)

It is wierd, because it doesn't seem like a "Python" error, the solver 
just stops iterating with that message about the Jacobian.  And the 
values it calculates up to that point are the same as the configuration 
that succeeds.

I'm curious enough that I'd love to set up a debug environment so that I 
could trace through the Python/C interface, but I don't have time at the 

Thanks again,

Christian Kristukat wrote:
> palazzol at comcast.net wrote:
>>First, I want to thank you all for making SciPy so great.  I am prototyping an application using VPython and SciPy together, and I can't believe how well everything is working after <2 weeks of coding.
>>Now, on to my question.  I've boiled it down to a small test program which shows the situation, and attached it here.  I am passing an array into my minimization function, intended to hold the return values.  Apparently, this causes some kind of issue, when the array is updated elementwise.  If you run it as-is, you will see the iteration stop after a few tries, with an error.  If you uncomment either of the #err=xxx lines, it works fine.
> I do not see what you intend using the err variable, nor do I understand why it
> should not work like this. This is really weird.
> Anyway, I send attached a modified version.
> Christian
> ------------------------------------------------------------------------
> from numpy import *
> from scipy.optimize.minpack import *
> #from random import *
> # this statement imports the python random module
> # you rather want to use numpy.random
> def f(x,y,z):
>     temp = z - (x[0] + x[1]*y + x[2]*y*y)
>     # to make err a copy of temp, just write err[:] = temp[:]
>     # or err = temp.copy()
>     # or even err = temp+0
>     return temp
> y = arange(0,10)
> # consider using linspace() instead of arange, it is more convenient
> #z = 1. + 5*y + 9*y*y + random()
>                         # this returns a random scalar
> z = 1. + 5*y + 9*y*y + random.rand(10)*100.0
>                        # this returns an array with 10 random numbers
> x0 = array([1.,1.,1.])
> #err = array([0.,0.,0.,0.,0.,0.,0.,0.,0.,0.])
> #args = (y,z,err) 
> args = (y,z)
> # I don't see why you pass err and what it is used for nor do I understand
> # why the minimization does not work anyway.
> sol = leastsq(f,x0,args)
> print sol
> #import pylab 
> #pylab.plot(y,z)
> #pylab.plot(y,-f(sol[0],y,z)+z)
> #pylab.show()
> ------------------------------------------------------------------------
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-user mailing list