我想將洛倫茲峯擬合成一組數據x和y,數據很好。像OriginLab其他程序適合它完美,但我想用python自動化配件,所以我有下面的代碼是基於http://mesa.ac.nz/?page_id=1800scipy.optimize.leastsq返回最佳猜測參數不是新的最佳擬合
我遇到的問題是,scipy.optimize.leastsq返回作爲最合適的我傳遞給它的相同的初始猜測參數,本質上什麼都不做。這是代碼。
#x, y are the arrays with the x,y axes respectively
#defining funcitons
def lorentzian(x,p):
return p[2]*(p[0]**2)/((x - (p[1]))**2 + p[0]**2)
def residuals(p,y,x):
err = y - lorentzian(x,p)
return err
p = [0.055, wv[midIdx], y[midIdx-minIdx]]
pbest = leastsq(residuals, p, args=(y, x), full_output=1)
best_parameters = pbest[0]
print p
print pbest
p的初始猜測和best_parameters是返回的「最佳擬合」從leastsq參數,但他們都是一樣的。
這就是返回由full_output = 1(長數字陣列已經縮短,但仍representitive)
[0.055, 855.50732, 1327.0]
(array([ 5.50000000e-02, 8.55507324e+02, 1.32700000e+03]),
None, {'qtf':array([ 62.05192947, 69.98033905, 57.90628052]),
'nfev': 4,
'fjac': array([[-0., 0., 0., 0., 0., 0., 0.,],
[ 0., -0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0.],
[ 0., 0., -0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0.]]),
'fvec': array([ 62.05192947, 69.98033905,
53.41218567, 45.49879837, 49.58242035, 36.66483688,
34.74443436, 50.82238007, 34.89669037]),
'ipvt': array([1, 2, 3])},
'The cosine of the angle between func(x) and any column of the\n Jacobian
is at most 0.000000 in absolute value', 4)
任何人都可以看到最新錯了嗎?
「full_output = 1」中返回的信息說什麼?可能有一個提示... – seberg
更新添加,並拿出我檢查的代碼冗餘的問題 –