函数调用的结果不是使用 scipy.fsolve 的正确浮点数组

Result from function call is not a proper array of floats using scipy.fsolve

我正在尝试使用 scipy 的 fsolve 函数求解这个简单的联立方程:

x + 2 = 10 & x^2 = 64。

我期待 8 作为解决方案。但是我收到一条错误消息 "minpack.error: Result from function call is not a proper array of floats."

我对 python 科学图书馆还很陌生。有人可以解释如何解决这个错误吗?谢谢!

from scipy.optimize import fsolve

def equations(p):
    x = p
    return (x-8, x**2 - 64)

x =  fsolve(equations, 1)

print(x)

当您查看 scipy 模块中如何定义 fsolve 时,我们看到:

def fsolve(func, x0, args=(), fprime=None, full_output=0,
           col_deriv=0, xtol=1.49012e-8, maxfev=0, band=None,
           epsfcn=None, factor=100, diag=None):
    """
    Find the roots of a function.

    Return the roots of the (non-linear) equations defined by
    ``func(x) = 0`` given a starting estimate.

    Parameters
    ----------
    func : callable ``f(x, *args)``
        A function that takes at least one (possibly vector) argument,
        and returns a value of the same length.
    '''

因此,p 的输入值应包含与函数返回的元素一样多的元素。尝试例如:

from scipy.optimize import fsolve
import numpy as np


def equations(p):
    x1 = p[0]
    x2 = p[1]
    return x1-8, x2**2 - 64

x = fsolve(equations, np.array([1, 2]))

print(x)

给出 8, 8 作为答案。