我正在尝试改变牛顿冷却问题(链接:https://mbe.modelica.university/behavior/equations/physical/#physical-types),以便:
1)前5秒T_inf为300K
2)在T = 5时,我将其切换为正弦波,T_inf的平均值为400 K,峰峰值幅度为50 K,周期为10秒
3)在T = 85s时,我想将正弦波的周期更改为0.01秒,其他都保持不变。仿真必须在100秒内结束
我在第1部分和第2部分中都取得了成功,但第3部分却没有为我服务。 我的代码在下面。
model MAE5833_Saleem_NewtonCooling_HW2_default
// Types
type Temperature = Real (unit="K", min=0);
type ConvectionCoefficient = Real (unit="W/(m2.K)", min=0);
type Area = Real (unit="m2", min=0);
type Mass = Real (unit="kg", min=0);
type SpecificHeat = Real (unit="J/(K.kg)", min=0);
// Parameters
parameter Temperature T0=400 "Initial temperature";
parameter ConvectionCoefficient h=0.7 "Convective cooling coefficient";
parameter Area A=1.0 "Surface area";
parameter Mass m=0.1 "Mass of thermal capacitance";
parameter SpecificHeat c_p=1.2 "Specific heat";
parameter Real freqHz=0.1 "Frequency of sine wave in from 5 to 85 seconds";
parameter Real freq2=100 "Time period of 0.01s after 85 seconds";
parameter Real amplitude=25 "Peak to peak of 50K";
parameter Real starttime=5;
parameter Real T_init=300;
parameter Real T_new=400;
Temperature T "Temperature";
Temperature T_inf;
initial equation
T = T0 "Specify initial value for T";
equation
m*c_p*der(T) = h*A*(T_inf - T) "Newton's law of cooling";
algorithm
when {time > starttime,time < 85} then
T_inf := (T_new - T_init) + amplitude*Modelica.Math.sin(2*3.14*freqHz*(time - starttime));
elsewhen time > 85 then
T_inf := (T_new - T_init) + amplitude*Modelica.Math.sin(2*3.14*freq2*(time - starttime));
elsewhen time < starttime then
T_inf := T_init;
end when;
annotation (experiment(
StopTime=100,
Interval=0.001,
__Dymola_Algorithm="Rkfix2"));
end MAE5833_Saleem_NewtonCooling_HW2_default;
答案 0 :(得分:3)
在这种情况下,您必须使用if
语句,而不是when
。
这是更新的公式部分,下面有一些其他建议:
equation
m*c_p*der(T) = h*A*(T_inf - T) "Newton's law of cooling";
if time >= starttime and time < 85 then
T_inf = (T_new - T_init) + amplitude*sin(2*Modelica.Constants.pi*freqHz*(time - starttime));
elseif time >= 85 then
T_inf = (T_new - T_init) + amplitude*sin(2*Modelica.Constants.pi*freq2*(time - starttime));
else
T_inf = T_init;
end if;
sin
代替Modelica.Math.sin
,因为该函数是内置的Modelica.Constants.pi
而不是自己定义pi algorithm
合并到equation
部分中。除非有充分的理由,否则不要使用算法部分。