MATLAB - 如何创建具有特定要求的条件

时间:2017-02-26 22:09:42

标签: matlab simulation statements

我正在模拟水加热,我需要创造一定的条件,我不知道如何正确地创造它。

所需的水温为55°C。最低温度为50°C。最高温度为70°C。

我有两种类型的加热 - 电加热将水加热到55°C的所需温度,光电加热可以将水加热到最高温度。

我需要创造一个条件,只有当温度低于50°C时才开启电加热,并在达到55°C后停止。如果温度介于50和55之间而没有预先降至50°C以下,则只能进行光伏加热并关闭电加热。

全年每分钟检查一次温度。条件将被置于循环中。

现在,我没有所需的温度条件(55°C),如下所示:

for i = 1:525600
    if (temeprature(i) < 70)
           heating = 1; %heating from photovoltaic
       else
           heating = 0; % heating off
       end
         if (temperature(i) < 50)
          heating = 2; % electric heating when there is not enough power from PV                   
         end
   if heating==0
     calculations
     calling functions
     etc.
     ...
    end
   if heating==1
     calculations
     calling functions
     etc.
     ...
    end 
   if heating==2
     calculations
     calling functions
     etc.
     ...
    end 
 computing temperature with results from conditions
 end

感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

我会使用持久变量进行电加热功能:

function [el, pv] = whatHeating(T)
persistent elHeat
if (isempty(elHeat))
    elHeat = false; % Initialize to false. The only thing where it matters is if you start at 50<T<55.
end

if (T < 50)
    elHeat = true;
elseif (T > 55)
    elHeat = false;
end
el = elHeat; % You can't return persistent variable directly.

if (T > 70)
    pv = false;
else
    pv = true;
end

然后你只需在主要功能中调用此功能。