How to commit statement only under certain conditions?

Hello, I'm simulating water heating and I need to create certain condition and I dont know how to create it properly.
Required temperature of water is 55 °C. Minimal temperature is 50 °C. Maximum temperature is 70 °C.
I have 2 types of heating - electrical heating which heats water to required temperature 55 °C and photovoltaic heating which can heat water to maximum temperature.
I need to create condition which turn on electrical heating only if temperature drops below 50 °C and stops after reaching 55 °C. If the temperature is between 50 and 55 without dropping under 50 °C only photovoltaic heating is possible and electrical heating is off.
Temperature is checked every minute for whole year. Conditions will be placed in for cycle. I dont know how to create condition for starting electrical heating under 50 and stopping at 55.
Something like that:
if temperature < 70
photovoltaic on
else
everything off
if temperature < 50
electrical heating on and stops at 55°C
Thanks for advice.

 Accepted Answer

Jan
Jan on 26 Feb 2017
Edited: Jan on 26 Feb 2017
electric = true;
photovoltaic = false;
for iMinute = 1:365*24*60
if temperature(iMinute) < 50
electric = true;
elseif temperature(iMinute) > 55
electric = false;
end
photovoltaic = (temperature(iMinute) < 70);
...
end

7 Comments

Thank you for reaction. Maybe I need to write more details becouse I dont get how to get it work on my code. It still stops at minimal temperature.
I will show you how I am doing it right now, without condition for required temperature.
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
end
If I try try to add this:
if (temperature(i) < 50)
heating = 2; % electric heating when there is not enough power from PV
elseif (temperature(i) > 55)
heating = 0;
end
It still stops at 50 °C.
Thank you for your time.
What does "it stops" exactly mean? Where is the temperature changed? Perhaps the problem is there.
It means, that it ignores 55 °C condition and heats the water just to 50 °C. If temperature drops under 50 due to heat loss or water consuption, water is heated again just to 50.
Temperature is changed according to condition "0", "1" or "2". In each condition I'am calculating how much energy I put into water from water heater with some power output (e.g. 2000 W). Obviously if condition heater == 0 is on (temperature is > 70 °C), no energy flows into water and temperature is just dropping.
To prevent from too many on/off cycles of water heater I need to set up some delay by required temperature at 55 °C so the heater will turn on again after temperature drops by 5 °C and not for example just 0,5 °C (from 50 to 49,5 and turning on heater do 50 again).
EDIT: If I make simplification by setting power of photovoltaic heater to 0, it keeps temperature just at 50 °C (oscilating around it). I want it to rise at 55, drops to 50 (due to losses) and after that switch heater on.
With photovoltaic on it looks like this:
Higher power in summer, higher possible temperature.
If I add condition as in the previous post, then in looks like first graph.
Maybe it looks difficult, but it cant be :D. Just one condition to switch on heater under 50 and stop it at 55. After that, ignore temperature between 50 and 55 with electrical heating. Photovoltaic can run as it please if there is enough power from sun.
Jan
Jan on 26 Feb 2017
Edited: Jan on 27 Feb 2017
Sorry, without seeing the code, which changes the temperature, I cannot guess, if there is a problem. I assume the solution will be easy, but the problem is hidden in the parts of your code abbreviated by "calculations, calling functions, etc., ...".
I suggest to start with a simplified version, which contains the temperature control only. Then add a trivial change of the temprature like: "if any heating is on, T = T + 1" and observe, what happens.
I dont think that problem is in "calculations, calling functions, etc., ...". There are just formulas calculating power, energy and losses according to condition. Temperature is calculated out of this conditions usig their results. I cant control temperature without heating.
Jan
Jan on 27 Feb 2017
Edited: Jan on 27 Feb 2017
@LamaObencna: The code you have shown here, does not contain the code for changing the temperature. The part of detecting, which heating is enabled, was discussed exhaustively already, and it cannot cause the observed behavior alone. You can replace it for testing: Keep the photovoltaic and/or electric heating enabled manually and examine, what happens.
Something like "it ignores 55 °C condition" cannot happen. Matlab is not in a bad mood and ignores any line. I really want to help you and I'm convinced the problem is not included in the part of the code, which has been posted here. So please start testing the code I've suggested if it enables the heat sources exactly as you want it. Then we can proceed and find out, why the correct values do not yield the expected result in the change of the temperature.
Done. I figured it out with help of community of stackoverflow, basicly very similar to your idea. I should show whole code, it would be easier to get it work, my mistake. Anyway, thank you for your time and advice. I have marked your answer as accepted becouse it was right direction.

Sign in to comment.

More Answers (0)

Asked:

on 26 Feb 2017

Edited:

on 27 Feb 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!