scipy.optimize.minimize两个不同的输出为相同(?)输入

问题描述 投票:0回答:1

我有一个功能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个回答
0
投票

编辑:我想我理解你的问题是错误的。像在print语句中那样舍入数字显然会导致一些差异(这些差异非常小(围绕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()

功能:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.