MATLAB - 如何根据特定要求创建条件
MATLAB - how to create condition with specific requirements
我正在模拟水加热,我需要创建特定条件,但我不知道如何正确创建它。
所需水温为55°C。最低温度为 50 °C。最高温度为 70 °C。
我有两种供暖方式——将水加热到所需温度 55 °C 的电加热和可以将水加热到最高温度的光伏加热。
我需要创造条件,只有当温度低于 50 °C 时才打开电加热,并在达到 55 °C 后停止。如果温度在 50 和 55 之间,而之前没有降到 50 °C 以下,则只能使用光伏加热,并且电加热关闭。
全年每分钟测一次体温。条件会放在for循环中。
现在,我没有条件要求的温度(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
感谢任何建议。
我会为电加热创建一个带有持久变量的函数:
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
然后你只需在你的主函数中调用这个函数。
我正在模拟水加热,我需要创建特定条件,但我不知道如何正确创建它。
所需水温为55°C。最低温度为 50 °C。最高温度为 70 °C。
我有两种供暖方式——将水加热到所需温度 55 °C 的电加热和可以将水加热到最高温度的光伏加热。
我需要创造条件,只有当温度低于 50 °C 时才打开电加热,并在达到 55 °C 后停止。如果温度在 50 和 55 之间,而之前没有降到 50 °C 以下,则只能使用光伏加热,并且电加热关闭。
全年每分钟测一次体温。条件会放在for循环中。
现在,我没有条件要求的温度(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
感谢任何建议。
我会为电加热创建一个带有持久变量的函数:
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
然后你只需在你的主函数中调用这个函数。