首页 > 解决方案 > 如何优化一组方程的变量?

问题描述

我有 6 个变量,我想根据 20 个方程估计它们的值(这些方程作为列表存储在 f0 中)。有人知道我可以使用 python 中的哪个库来估计这些变量吗?优化完成后,这就是我对 6 个变量的期望:

# expected value for x[0] = 2,
# expected value for x[1] = 0.002,
# expected value for x[2] = 4000,
# expected value for x[3] = 2,
# expected value for x[4] = 0.002,
# expected value for x[5] = 0.002

def func(x):
    rl = [1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 150, 200, 250, 300, 350, 400]
    vin = 20*[480]
    ian = [22.381, 6.209, 3.4182, 2.0862, 1.7003, 1.5329, 1.4446, 1.3918, 1.3574, 1.3337, 1.3165,
           1.3036, 1.2857, 1.2793, 1.2695, 1.2552, 1.2475, 1.2429, 1.2398, 1.2373]
    f0 = []
    for i in range(len(rl)):
        f0.append((x[3]*x[2]*(376.99*1j*x[5] + x[4] + rl[i])/((x[3] + x[2])*(x[3]*x[2]/(x[3] + x[2]) + 376.99*1j*x[5] + x[4] + rl[i])) + 376.99*1j*x[1] + x[0])* ian[i] - vin[i])
    return f0

我尝试使用 minimum_squares 如下所示,但结果有点偏离。谢谢!

res = least_squares(func, (0.1, 0.01, 350, 1, 1, 0.01), bounds = ((0,0,0,0,0,0), (2,0.5,5000,5,2,0.5)))

标签: pythonoptimizationscipyleast-squares

解决方案


推荐阅读