一组微分方程中的条件分段函数

时间:2019-09-05 22:16:35

标签: python-3.x ode piecewise gekko

我正在尝试实现一个模型,并且我将此条件分段功能作为模型的一部分。

This the equation,I'm trying to implement


其中T_zone(= 293)和T_in(= 348)是先前定义的常数,x2和x5是变量,它们取决于依赖于T_in和T_out的其他方程式。

所有参数都有定义的初始值。


temperature_zone=293.0
T_in = m.Var(value=temperature_zone) 
T_zone = m.Var(value=temperature_zone) 
x2 = m.Var(value=temperature_zone)
x5 = m.Var(value=temperature_zone)
Tout = m.Var(value=temperature_zone)
Tp1out=m.Var(value=temperature_zone)

m1out=m.if3(((2.0*(x2))-T_in-T_zone), 0, ((2.0*(x2))-T_in-T_zone))
m.Equation(T_p1out== T_zone+m1out)
m2out=m.if3(((2.0*(x5))-T_p1out-T_zone), 0, ((2.0*(x5))-T_p1out-T_zone))
m.Equation(T_out == T_zone+m2out)

在上面的代码中,我尝试将等式分为两部分,并将if条件的结果作为底数/最小值的一个附加变量添加,但没有解决方案。

1 个答案:

答案 0 :(得分:2)

以下是要考虑的一些事情:

  • 如果T_inT_zone是常量,则使用m.Constm.Param代替m.Var。否则,可以由求解器进行调整。
  • 单个m.if3函数足以解决此问题。您可以定义两个m.if3语句,然后添加输出,但是随后您将创建其他二进制变量,这将使您的问题更难以解决。
  • condition确定是T_out=T_zone (condition<0)还是T_out=2*x5-2*x2+T_in (condition>=0)
  • T_in的值为348.0,而不是temperature_zone (293.0)

这是您提出的问题的完整脚本。

from gekko import GEKKO
m = GEKKO()
temperature_zone=293.0
T_in = m.Const(value=348.0) 
T_zone = m.Const(value=temperature_zone) 
x2 = m.Var(value=temperature_zone)
x5 = m.Var(value=temperature_zone)
condition = 2*x5-2*x2+T_in-T_zone
Tout=m.if3(condition, T_zone, 2*x5-2*x2+T_in)
m.solve()

APOPT solver在两次迭代中找到了解决方案。请注意,在此示例中,x2x5是附加的自由度,并且仍需要方程式来指定其值。

 Number of state variables:              6
 Number of total equations: -            3
 Number of slack variables: -            2
 ---------------------------------------
 Degrees of freedom       :              1

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter:     1 I:  0 Tm:      0.00 NLPi:    2 Dpth:    0 Lvs:    2 Obj:  0.00E+00 Gap:       NaN
--Integer Solution:   0.00E+00 Lowest Leaf:   0.00E+00 Gap:   0.00E+00
Iter:     2 I:  0 Tm:      0.00 NLPi:    1 Dpth:    1 Lvs:    2 Obj:  0.00E+00 Gap:  0.00E+00
 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   1.390000000537839E-002 sec
 Objective      :   0.000000000000000E+000
 Successful solution
 ---------------------------------------------------