【问题标题】:Python solving multiple nonlinear equations where one equation has different forms depending on the range of one of the variablesPython求解多个非线性方程,其中一个方程具有不同的形式,具体取决于变量之一的范围
【发布时间】:2019-12-01 20:35:16
【问题描述】:

我需要找到满足多个非线性方程的多个变量的值。这些方程之一在特定范围内应具有零值,否则应具有函数。有没有办法在 scipy 中使用 fsolve 来解决这个问题?

示例: Q,T,K,W,H,L 是已知参数(常数) 我正在求解 a、b、c 和 d。 我的功能是

  • 第一:

    c = math.pi * T * (b - a) / math.log((b - 3.3)/ 0.059)

    • 第二:

    c = K / (2.0 * W) * (b ** 2 - H ** 2)

    • 第三:

    d= 0 , when (a - 102.5) <=0

    d = 0.02 * (31.26 - 0.74 * ((a-102.5) * 100.0)) * (((a-102.5)*100.0)**1.48) , when (a - 102.5) > 0

    • 第四:

    Q + c * L + d * 3.6 = 0

我想我的问题的改写可能是:(是否可以表示 fsolve 中的第三个函数)?

提前非常感谢

【问题讨论】:

    标签: python


    【解决方案1】:

    也许sympy,Python 的符号数学库,可以提供帮助吗? 有一个函数Piecewise 来表示条件函数。 除了符号求解器 (solve),sympy 还有一个数字求解器 nsolve

    代码可能如下所示:

    from sympy import pi, log, Eq, Piecewise, nsolve
    from sympy.abc import Q, T, K, W, H, L, a, b, c, d
    
    eq1 = Eq(c, pi * T * (b - a) / log((b - 3.3) / 0.059))
    eq2 = Eq(c, K / (2 * W) * (b ** 2 - H ** 2))
    eq3 = Eq(d, Piecewise((0, a - 102.5 < 0),
                          (0.02 * (31.26 - 0.74 * ((a - 102.5) * 100.0)) * (((a - 102.5) * 100.0) ** 1.48), True)))
    eq4 = Eq(Q + c * L + d * 3.6, 0)
    
    all_eq = [e.subs({Q: 20, T: 10, K: 1, W: 1, H: 1, L: 1}) # fill in specific values for the parameters
              for e in (eq1, eq2, eq3, eq4)]
    
    sol = nsolve(all_eq, (a, b, c, d),  # numerically solve for a,b,c,d
                         (1, 10, 1, 1)) # give initial values for a,b,c,d (here b should be larger than 3.3)
    

    【讨论】:

    • 这个答案有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多