[SciPy-user] parameter bounds using leastsq

Stephen Walton stephen.walton at csun.edu
Tue Nov 8 10:53:12 CST 2005

Eric Zollars wrote:

>	Could you flesh out this answer some more?  I've had to do this in the 
>past and was sure I was missing something.  In the simplest case a set of:
>y1 = (b0 + b1*x1 + b2*x2)1
>yn = (b0 + b1*x1 + b2*x2)n
>In fortran I would pass x as a 2d matrix to the function misfit.  What 
>do you do in scipy if x is a vector at each point y?
You would write your function f accordingly.  Because Scipy is vector 
oriented,  a simple


would work, assuming x is a Scipy/Numeric array of shape (2,n).

By the way, I humbly suggest there was a typo in Robert's original post:

>>def f(beta, x):
>>    # compute values y given parameters beta at points x
>>def misfit(beta, x, y):
>>    diff = y - f(beta, x)
>>    return scipy.sum(diff*diff)
>>beta_opt = scipy.optimize.fmin_cobyla(f, beta0, constraints, (x, y))
I think the "f" argument in the call to fmin_cobyla needs to be "misfit" 
else you're not fitting to the data.

Steve Walton

More information about the SciPy-user mailing list