与你的第二个f:
In [596]: def f(x):
...: u=x[0]
...: v=x[1]
...: return [u+v-4, u**2+v**2-8]
...:
我给它一个 2 元素列表,然后返回一个 2 元素列表
In [597]: f([0,0])
Out[597]: [-4, -8]
实际上fsolve 会将你的x0 变成一个2元素数组,并将返回也视为一个数组
In [598]: f(np.array([0,0]))
Out[598]: [-4, -8]
In [599]: np.array(f(np.array([0,0])))
Out[599]: array([-4, -8])
In [600]: _.shape
Out[600]: (2,)
由于所有未定义的变量(Qu、Pu 等),我无法演示您的第一个 f。我想我可以猜出合理的形状,但我不喜欢那样做。
但是错误消息表明它生成了类似的东西:
In [601]: np.array([[1],[2]])
Out[601]:
array([[1],
[2]])
In [602]: _.shape
Out[602]: (2, 1)
我可以通过对您的第二个f 稍作改动来模仿:
In [606]: def f1(x):
...: u=x[0]
...: v=x[1]
...: return [[u+v-4], [u**2+v**2-8]]
...:
In [607]: optimize.fsolve(f1,[0,0])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-607-a21691ae31f8> in <module>()
----> 1 optimize.fsolve(f1,[0,0])
/usr/lib/python3/dist-packages/scipy/optimize/minpack.py in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag)
144 'diag': diag}
145
--> 146 res = _root_hybr(func, x0, args, jac=fprime, **options)
147 if full_output:
148 x = res['x']
/usr/lib/python3/dist-packages/scipy/optimize/minpack.py in _root_hybr(func, x0, args, jac, col_deriv, xtol, maxfev, band, eps, factor, diag, **unknown_options)
210 if not isinstance(args, tuple):
211 args = (args,)
--> 212 shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
213 if epsfcn is None:
214 epsfcn = finfo(dtype).eps
/usr/lib/python3/dist-packages/scipy/optimize/minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
38 msg += "."
39 msg += 'Shape should be %s but it is %s.' % (output_shape, shape(res))
---> 40 raise TypeError(msg)
41 if issubdtype(res.dtype, inexact):
42 dt = res.dtype
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'f1'.Shape should be (2,) but it is (2, 1).
所以它会执行一个测试计算,比如f(x0),并检查返回内容的维度。并在与预期不符时抱怨。
请记住,numpy 中的数组可以有 0、1、2 或更多维度。相比之下,MATLAB 至少有 2d。所以在 numpy 中,像 (2,) 这样的形状不同于 (2,1) 或 (1,2)。它们都有 2 个元素,并且可以相互重塑,但对于许多操作而言,维度的数量很重要。
[1,2]、[[1],[2]] 和 [[1,2]] 是等效的列表表达式。