gpt4 book ai didi

variables - Modelica 中时间变量的平均值

转载 作者:行者123 更新时间:2023-12-02 16:08:45 25 4
gpt4 key购买 nike

我有一个简单的模型如下,我打算计算 x 导数 wrt time(der(x)) 的平均值。

model Average
Real x;
initial equation
x = 2.0;
equation
der(x) = x + 5;
annotation (experiment(StopTime=10, __Dymola_Algorithm="Dassl"));
end Average;

重点是它是原始代码的简化代码,其中“x”由 CombiTimeTable 给出。我需要执行几个模拟(通过几个文本文件作为 CombiTimeTable 的输入)它们没有相同的行数(不同的持续时间)。换句话说,StopTime 因情况而异,例如对于这种特定情况,此处的 StopTime 为 10s。有什么方法可以使用一般方程来计算 der(x) 的平均值。如果有人可以提供帮助,我将不胜感激。

最佳答案

基于sjoelund.se的回答,我创建了一个模型,该模型通过 2 种方法计算 der(x) 的平均值:

  • 使用 MSL 中的 block Modelica.Blocks.Math.ContinuousMean(而不是 Mean,因为它不采样)
  • 在模拟终止时用(x2-x1)/(time2-time1)计算平均值
model Average
import Modelica.Constants.pi;
import Modelica.Utilities.Streams.print;

Real x;
Real time1, time2, x1, x2;
Modelica.Blocks.Math.ContinuousMean continuousMean;

equation

der(x) = sqrt(1+cos(2*pi*2*time))+sin(time)^2;

// Method 1: Continuous mean from MSL
continuousMean.u = der(x);

when terminal() then
print("Continuous mean: "+String( continuousMean.y));
end when;

// Method 2: Mean at end time
when initial() then
x1 = x;
time1 = time;
end when;

when terminal() then
x2 = x;
time2 = time;
print("Computed mean: "+String( (x2-x1)/(time2-time1)));
end when;

annotation (experiment(StopTime=1.0, __Dymola_Algorithm="Dassl"));
end Average;

这两个值都在模拟结束时打印出来。它们给出的值与您可以在 Dymola 中对绘制信号应用的平均后处理命令相似:

simulation output and plot of mean of der(x)

另一种可能性是在模拟结束后调用 Dymola 中的均值信号运算符(可能在运行模拟的脚本中):

DymolaCommands.Plot.signalOperatorValue("der(x)", SignalOperator.ArithmeticMean, 0, 1);

关于variables - Modelica 中时间变量的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68625956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com