我希望使用scipy.optimize.leastsq()
scipy 的方法来优化三个参数a,b,c
。我所拥有的是这两个方程。
1*a+2*b+3*c = x1
4*a+5*b+6*c = x2
从分析上讲,这组方程是不确定的,但在数值上,我试图找到a,b,c
最小化测量结果给定结果的误差[2,2]
:
1*a+2*b+3*c - 2 = 0
4*a+5*b+6*c - 2 = 0
因此我写了一些代码。
def function(a,b,c,t):
return np.array([1*a+2*b+3*c+t[1],4*a+5*b+6*c+t[1]])
a0 = 1
b0 = 1
c0 = 1
measdata = np.array([2,2])
t = [1,2]
def residual(x0,measdata,t):
return measdata - function(x0[0],x0[1],x0[2],t)
erg = optimize.leastsq(func=residual,x0=(a0,b0,c0),args=(measdata,t))
它总是导致:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-296-ab0fc90a2253> in <module>()
14 return result - function(x0[0],x0[1],x0[2],t)
15
---> 16 erg = optimize.leastsq(func = residual, x0 = (a0,b0,c0) , args=(result,t), maxfev=10000)
17
18 function(erg[0][0],erg[0][1])
//anaconda/lib/python3.5/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
378 m = shape[0]
379 if n > m:
--> 380 raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))
381 if epsfcn is None:
382 epsfcn = finfo(dtype).eps
TypeError: Improper input: N=3 must not exceed M=2
我如何让它找到最小值?我知道这只是局部最小值,但我会很高兴。