【问题标题】:Result from function call is not a proper array of floats using scipy.fsolve函数调用的结果不是使用 scipy.fsolve 的正确浮点数组
【发布时间】:2019-10-15 16:50:19
【问题描述】:

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

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

我期待 8 作为解决方案。但是我收到一条错误消息 "minpack.error: 函数调用的结果不是正确的浮点数组。"

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

from scipy.optimize import fsolve

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

x =  fsolve(equations, 1)

print(x)

【问题讨论】:

    标签: python scipy-optimize


    【解决方案1】:

    当您查看在 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 作为答案。

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      相关资源
      最近更新 更多