【问题标题】:How do I model a single time event in Modelica occurring at a predetermined time?如何在 Modelica 中对预定时间发生的单个时间事件进行建模?
【发布时间】:2017-11-04 09:42:13
【问题描述】:

我想模拟一个连续时间系统,它会在预先知道的某个时刻改变其行为。一个小例子如下。

model time_event
  Real x(start = 0)  "state variable for this example";
  parameter T_ch = 5 "time at which the system dynamics undergoes a change";
equation
  if time <= T_ch then 
    der(x) = x + 1;
  end if;
  if time > T_ch then
    der(x) = -x;
  end if;

end time_event;

【问题讨论】:

    标签: modelica openmodelica


    【解决方案1】:

    您的解决方案几乎没问题。以下是您的代码,经过一些修改。

    • 用过if then else也可以做if then elseif then elseif then ... else
    • 添加了平衡变量xb 以具有公共导数方程(不必只是编码样式)。

    代码:

    model time_event      
        Real x(start = 0)  "state variable for this example";
        parameter Real T_ch = 5 "time at which the system dynamics undergoes a change";
        Real xb "Balance variable for derivative";
    equation
        der(x) = xb; 
        if time <= T_ch then 
            xb = x + 1;
        else
            xb = -x;
        end if;
    end time_event;
    

    结果图:

    红色 = x

    蓝色 = der(x)

    【讨论】:

    • 非常感谢。您能否解释一下编译器在我的代码中出现错误的原因是什么?据我所知,这两个代码在编程上是相同的。
    • 一个 if 方程需要在每个分支中具有相同数量的方程。因为它需要知道 der(x) 在哪个方程中求解。 der(x) 有 2 个不同的方程,应该会出错。这个答案只是将中间 xb 移出等式,除非您想绘制该变量,否则这是不必要的。请注意,如果您的条件只涉及参数(而不是时间),编译器可能已经能够评估分支;在这些情况下,不需要平衡 if 方程,因为其中一些将被删除。
    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多