Python 用函数a.cos(b-psi)+c导出雅可比矩阵,结果为0
我有一个具有余弦形状的数据集。我尝试使用以下函数来拟合此数据集:Python 用函数a.cos(b-psi)+c导出雅可比矩阵,结果为0,python,scipy,mathematical-optimization,Python,Scipy,Mathematical Optimization,我有一个具有余弦形状的数据集。我尝试使用以下函数来拟合此数据集: a.cos(b-psi)+c 我的目标是估计最符合我的数据的参数a、b和c。所以我尝试使用scipy.leastsq来最小化这个:data-a.cosb-psi+c psi与数据一起被发现 我的首字母参数是ndarray x=np。数组[a0,b0,c0],我的数据存储在元组args=psi中,data,我有我的函数: def func(x, *args): psi = args[0].ravel() data = args[1
a.cos(b-psi)+c
我的目标是估计最符合我的数据的参数a、b和c。所以我尝试使用scipy.leastsq来最小化这个:data-a.cosb-psi+c
psi与数据一起被发现
我的首字母参数是ndarray x=np。数组[a0,b0,c0],我的数据存储在元组args=psi中,data,我有我的函数:
def func(x, *args):
psi = args[0].ravel()
data = args[1].ravel()
return np.array(data - (x[0]*np.cos(x[1]-psi) + x[2]))
然后,我使用以下行启动scipy.leastsq:
xopt = leastsq(coreg.func,x0,args,full_output=1)
对于初始参数:
Out[30]: array([ 3.8, 1.3, 0. ])
但结果是:
(array([ 3.8, 1.3, 0. ]),
None,
{'fjac': array([[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan]]),
'fvec': array([-3.17913524, -2.19610415, -2.06748506, ..., 1.76355583,
2.32077375, 2.89394884]),
'ipvt': array([1, 2, 3], dtype=int32),
'nfev': 4,
'qtf': array([ nan, nan, nan])},
'The cosine of the angle between func(x) and any column of the\n Jacobian is at most 0.000000 in absolute value',
4)
我不知道为什么它不能计算雅可比矩阵,我想这就是为什么它会返回和我最初输入的相同的参数
如果有帮助,以下是每个对象的值:
Variable Type Data/Info
--------------------------------
args tuple n=2
np module <module 'numpy' from '/us<...>ages/numpy/__init__.pyc'>
psi ndarray 1201x1201: 1442401 elems, type `float64`, 11539208 bytes (11 Mb)
data ndarray 1201x1201: 1442401 elems, type `float64`, 11539208 bytes (11 Mb)
x ndarray 3: 3 elems, type `float64`, 24 bytes
函数的雅可比矩阵很容易用解析法写出。提供给LEATTSQ电话。谢谢您的回答 我这样输入雅可比矩阵?对不起,我是python的初学者
def jacobi(x,*args):
psi = args[0].ravel()
target = args[1].ravel()
return -x[0]*np.sin(x[1]-psi)
但接下来会发生什么:
/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.pyc in leastsq(func,
x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev,
epsfcn,factor,diag)
367 _check_func('leastsq', 'Dfun', Dfun, x0, args, n, (n,m))
368 else:
--> 369 _check_func('leastsq', 'Dfun', Dfun, x0, args, n, (m,n))
370 if (maxfev == 0):
371 maxfev = 100*(n + 1)
/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.pyc in _check_func
(checker, argname, thefunc, x0, args, numinputs,
output_shape)
28 else:
29 msg += "."
---> 30 raise TypeError(msg)
31 if issubdtype(res.dtype, inexact):
32 dt = res.dtype
TypeError: leastsq: there is a mismatch between the input and output shape of the
'Dfun' argument 'jacob'.