【问题标题】:Python scipy minimization - Simple example not producing expected returnPython scipy 最小化 - 不产生预期回报的简单示例
【发布时间】:2021-11-04 14:04:12
【问题描述】:

我正在尝试使用 scipy 来查找满足简单体积 = 长度 * 宽度 * 高度示例中条件的变量的最小值。我试图找到在给定长度为 10 和宽度为 10 的情况下产生 1300 体积的高度。答案应该是 13,但 scipy 告诉我 x: 1.0000044152960563

from scipy.optimize import minimize_scalar

def objective_function(x):
    target = 1300
    length = 10
    width = 10
    (length * width * x) - target
    return x

res = minimize_scalar(objective_function, method='bounded', bounds=(1, 100))
res
print(x)

我可以使用函数外产生的 x 值吗?

【问题讨论】:

    标签: python scipy-optimize-minimize


    【解决方案1】:

    我发现我做错了什么。我需要对结果进行平方以获得正数,然后取平方根。

    from scipy.optimize import minimize_scalar
    import math
    
    def objective_function(x):
        target = 1300
        length = 10
        width = 10
        return (math.sqrt(((length * width * x) - target) ** 2))
    
    res = minimize_scalar(objective_function, method='bounded', bounds=(1, 100))
    res.x
    

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 1970-01-01
      • 2019-02-26
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多