Python scipy fsolve "mismatch between the input and output shape of the 'func' argument"

Python scipy fsolve "mismatch between the input and output shape of the 'func' argument"

在我进入我的问题之前,我在 Whosebug 上搜索了具有相同问题的相关线程:

根据我对这个错误的理解,

raise TypeError(msg)
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'fsolve_function'

问题是输入和输出的形状相同。

在我下面的代码示例中,我有以下内容:

fsolve 是 MINPACK 的 hybrd, which requires the function's argument and output have the same number of elements. You can try other algorithms from the more general scipy.optimize.root 的包装,没有此限制(例如 lm):

from scipy.optimize import fsolve, root

def fsolve_function(arguments):
    x = arguments[0]
    y = arguments[1]
    z = arguments[2]

    out = [(35.85 - x)**2 + (93.23 - y)**2 + (-39.50 - z)**2 - 15**2]
    out.append((42.1 - x)**2 + (81.68 - y)**2 + (-14.64 - z)**2 - 27**2)
    out.append((-70.90 - x)**2 + (-55.94 - y)**2 + (-68.62 - z)**2 - 170**2)
    out.append((-118.69 - x)**2 + (-159.80 - y)**2 + (-39.29 - z)**2 - 277**2)

    return out

initialGuess = [35, 93, -39]
result = root(fsolve_function, initialGuess, method='lm')
print(result.x)

顺便说一句,它找不到实际的零 --- 应该有一个吗?

您还可以强制 fsolve 使用您的函数,如果您为它提供带有 "bogus" 第四个变量的初始猜测:

initialGuess = [35, 93, -39, 0]

但我不确定这种情况下的结果有多可靠。