【问题标题】:scipy.optimize.minimize two different outputs for the same(?) inputscipy.optimize.minimize 相同(?)输入的两个不同输出
【发布时间】:2019-03-09 14:35:07
【问题描述】:

我有一个函数func(x)。我想知道xfunc(x)-7=0。因为没有确切的真实答案,我认为minimize 是个好主意。

from scipy.optimize import minimize

def func(x): # testing function which leads to the same behaviour
    return (x * 5 + x * (1 - x) * (6-3*x) * 5)

def comp(x): #  comparison function which should get zero
    return abs(((func(x)) - 7))

x0 = 0.
x_real = minimize(comp, x0) # minimize comparison function to get x

print(x_real.x)

最后一个print 给了我[ 0.7851167]。以下print...

print(comp(x_real.x))
print(comp(0.7851167))

...导致不同的输出:

[  1.31290960e-08]
6.151420706146382e-09

谁能解释一下这种行为?

【问题讨论】:

    标签: python scipy minimize


    【解决方案1】:

    编辑:我想我理解你的问题是错误的。像您在打印语句中所做的那样四舍五入显然会导致一些差异(这些差异非常小(大约1e-9)。

    要找到所有 x,您应该应用一些全局求解方法或从不同的初始点开始找到所有局部最小值(如图所示)。

    但是这个方程有两个解。

    def func(x): # testing function which leads to the same behaviour
        return (x * 5 + x * (1 - x) * (6-3*x) * 5)
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.linspace(-0.1,1,100)
    plt.plot(x, func(x)-7)
    plt.plot(x, np.zeros(100))
    plt.show()
    

    功能:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2016-06-17
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      相关资源
      最近更新 更多