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 上搜索了具有相同问题的相关线程:
- input/output error in scipy.optimize.fsolve
- Python fsolve() complains about shape. Why?
- fsolve - mismatch between input and output
- I/O shape mismatch when using scipy.optimize.fsolve on 2-dimensional anonymous function array variable
根据我对这个错误的理解,
raise TypeError(msg)
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'fsolve_function'
问题是输入和输出的形状不相同。
在我下面的代码示例中,我有以下内容:
- 输入,
initialGuess
(在scipy.optimize中的fsolve
函数中使用的起始估计)。输入 initialGuess
对坐标 x、y 和 z 有 3 个起始估计值。因此,我希望我的起始输入估计始终具有三个输入。
- 输出,
out
(非线性联立方程)。在这个例子中,我有 4 个非线性方程。
scipy.optimize.fsolve
引发上面突出显示的错误,因为输入和输出 not 具有相同的形状。在我的特定情况下,我希望我的输入始终具有三个值(以猜测 x、y 和 z 的初始起点)。本例中的输出有 4 个非线性方程,需要使用初始输入估计来求解。
- 旁注:使用相同的输入和输出形状,例如。输入 3 个 [x, y, z] 的形状和 3 个非线性方程的输出,
fsolve
将进行相应的计算。我只是想知道你如何扩展 fsolve
来使用比方说等于或多于 4 个非线性联立方程,只有 3 个输入初始估计?
代码如下:
from scipy.optimize import fsolve
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 = fsolve(fsolve_function, initialGuess)
print result
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]
但我不确定这种情况下的结果有多可靠。
在我进入我的问题之前,我在 Whosebug 上搜索了具有相同问题的相关线程:
- input/output error in scipy.optimize.fsolve
- Python fsolve() complains about shape. Why?
- fsolve - mismatch between input and output
- I/O shape mismatch when using scipy.optimize.fsolve on 2-dimensional anonymous function array variable
根据我对这个错误的理解,
raise TypeError(msg)
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'fsolve_function'
问题是输入和输出的形状不相同。
在我下面的代码示例中,我有以下内容:
- 输入,
initialGuess
(在scipy.optimize中的fsolve
函数中使用的起始估计)。输入initialGuess
对坐标 x、y 和 z 有 3 个起始估计值。因此,我希望我的起始输入估计始终具有三个输入。 - 输出,
out
(非线性联立方程)。在这个例子中,我有 4 个非线性方程。 scipy.optimize.fsolve
引发上面突出显示的错误,因为输入和输出 not 具有相同的形状。在我的特定情况下,我希望我的输入始终具有三个值(以猜测 x、y 和 z 的初始起点)。本例中的输出有 4 个非线性方程,需要使用初始输入估计来求解。- 旁注:使用相同的输入和输出形状,例如。输入 3 个 [x, y, z] 的形状和 3 个非线性方程的输出,
fsolve
将进行相应的计算。我只是想知道你如何扩展fsolve
来使用比方说等于或多于 4 个非线性联立方程,只有 3 个输入初始估计? 代码如下:
from scipy.optimize import fsolve 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 = fsolve(fsolve_function, initialGuess) print result
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]
但我不确定这种情况下的结果有多可靠。