开发者

SciPy LeastSq Dfun Usage

开发者 https://www.devze.com 2023-03-25 04:42 出处:网络
I\'m trying to get my Jacobian to work with SciPy\'s Optimize library\'s leastsq function. I have the following code:

I'm trying to get my Jacobian to work with SciPy's Optimize library's leastsq function.

I have the following code:

#!/usr/bin/python
import scipy
import numpy
from scipy.optimize import leastsq

#Define real coefficients
p_real=[3,5,1]

#Define functions
def func(p, x):         #Function
    return p[0]*numpy.exp(-p[1]*x)+p[2]

开发者_如何学运维def dfunc(p, x, y):     #Derivative
    return [numpy.exp(-p[1]*x),-x*p[0]*numpy.exp(-p[1]*x), numpy.ones(len(x))]

def residuals(p, x, y):
    return y-func(p, x)

#Generate messy data
x_vals=numpy.linspace(0,10,30)
y_vals=func(p_real,x_vals)
y_messy=y_vals+numpy.random.normal(size=len(y_vals))

#Fit
plsq,cov,infodict,mesg,ier=leastsq(residuals, [10,10,10], args=(x_vals, y_vals), Dfun=dfunc, col_deriv=1, full_output=True)

print plsq

Now, when I run this, I get plsq=[10,10,10] as my return. When I take out Dfun=dfunc, col_deriv=1, then I get something close to p_real.

Can anyone tell me what gives? Or point out a better source of documentation than what SciPy provides?

Incidentally, I'm using the Jacobian because I have the (perhaps misguided) belief that it will lead to faster convergence.


Change residuals to its negative:

def residuals(p, x, y):
    return func(p, x)-y

and you get

[ 3.  5.  1.]

Hope this helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号