我尝试使用 Leastsq 来拟合一条非常简单的曲线。但是,其解决方案并未优化。谁能给我一些建议?下面是我的代码:
from scipy import optimize
import numpy as np
hl_obs = np.array([10.0, 23.0, 20.0])
ph=np.array([5.0,7.0,9.0])
tp=60
def residuals(k_abn, ph, tp, hl_obs):
hr=np.log(2)/(hl_obs*24.0)
ph_adj=6013.79/(tp+273.15) + 23.6521*np.log10(tp+273.15)-64.7013
err = peval(k_abn, ph, ph_adj)-hr
return err
def peval(k_abn, ph, ph_adj):
temp= k_abn[0]*np.power(10,-ph) + k_abn[1] + k_abn[2]*np.power(10,(-ph_adj + ph))
return temp
k_abn =np.array([1, 0, 0])
from scipy.optimize import leastsq
p,ier = leastsq(residuals, k_abn, args=(hl_obs, ph, tp), maxfev=2000000)
print p, ier
从 EXCEL 的求解器中,我知道解应该是k_abn=[165, 0.001237578, 2.14]
. 一旦我将 Excel 的解决方案提供给function peval
,它就会生成正确的答案......
peval([165,0.001238,2.14], 5.0, 13.01573)=0.002888113
peval([165,0.001238,2.14], 7.0, 13.01573)=0.001255701
peval([165,0.001238,2.14], 9.0, 13.01573)=0.001444057
另外,我尝试使用epsfcn=np.finfo(np.float32).eps
Can有人给我一些建议来提高精度吗?谢谢!