Simulink or for-loop?

I am trying to model a solar water heating system monitoring every second in one day. The problem is that these calculations (using for-loops) are quite slow, and two simulated hours can take as much as 15 minutes. My question for you is if it's perhaps better to use Simulink, since its specifically designed to handle a signal (solar irradiation, in this case). Is it faster, or slower?
I'm not using any add-ons but I'm open to suggestions.

6 comentarios

Mathieu NOE
Mathieu NOE el 13 de Nov. de 2020
hello Christopher
it's difficult to answer your question without getting a bit more in the details of your computation
if you have lot of for loops , can you vectorize your code instead ?
IMHO, also Simulink is not the best choice if you still need lots of for loops
Christopher Norling
Christopher Norling el 13 de Nov. de 2020
The system consists of different components, and each component has losses (the solar collector has a gain). The efficiency of the collector depends on the in-and-out temperatures of the fluid, and if the fluid is in fact flowing. That means that we need to know the temperature of the fluid coming in in order to calculate the temperature coming out. This temperature depends on the boiler, which absorbs heat from the fluid. To know the exit temperature of the fluid from the boiler, one needs to know the inlet temperature, since there is a matter of alternate heating (high temp produces a thermocline with high-grade hot water, and low only heats low grade). The inlet flow has to pass through pipes though, and there is a loss of energy along the way (due to the heat).
Needless to say, there are a lot of unknown variables, and they are dependent on values from the second earlier. I'm not an expert, but I don't think vectorization can help in this case since there is so much interdependence.
Thanks for the feedback on Simulink.
Mathieu NOE
Mathieu NOE el 15 de Nov. de 2020
Ok
I understand the complexity of the problem , but there is maybe a way to solve this without needing tons of for loops
I believe we need to find the equilibrium point of the entire installation
that should leave to solving a set of linear equations maybe with polynomial functions
do you have already a code that works ?
Christopher Norling
Christopher Norling el 15 de Nov. de 2020
I've made attempts at it.
As it is now, it only simulates 60 seconds and it's barely a skeleton. Something that will need thermodynamical/mathematical revision is the heat in the collector (since it doesn't handle on-off signal from the pump properly).
syms t
Panel.Area=8;
T.c=15; T.w=80; T.start=15;
TST.Coil=15; TST.Volume=100/1000;
rho=1000; cp=4175;
Pipe.Radius=9/1000; Pipe.Area=pi*Pipe.Radius^2;
loopstart=3600*14; %14:00:00
loopend=loopstart+60; %14:01:00
SunSpline.y=ones(loopend-loopstart,1)*500; %500W/m2, example value
interval=loopend-loopstart;
loopstart=1; %Initial simulation time is defined as iteration 1
T_ex=zeros(interval,1); %Temperature of fluid exiting collector
T_in=zeros(interval,1)+T.c; %Temperature of fluid entering collector
T_ex(loopstart)=T.c; %Initial temperature out is 15C
T_in(loopstart)=T.c; %Initial temperature in is 15C
T_TST_bottom=zeros(interval,1)+T.c; %Initial temperature in thermal storage tank is 15C
for i=1:interval
%=========================== COLLECTOR ===========================%
Q_panel=SunSpline.y(i)*Panel.Area; %Incoming energy to system from sun
if i==1
SensorDiff=0;
else
SensorDiff=T_ex(i-1)-T_in(i-1);
end
if SensorDiff<5
if i==loopstart
T_ex(i)=T_ex(i)+Q_panel/((1/1000)*XTyfocor('cp',T_in(i))*XTyfocor('rho',T_in(i)));
else
T_ex(i)=T_ex(i-1)+Q_panel/((1/1000)*XTyfocor('cp',T_in(i))*XTyfocor('rho',T_in(i)));
%Equation for exit temperature with absorption of energy
%without in- or outflow of fluid
end
else
if i==loopstart
T_ex(i)=T_in(i)+Q_panel/((1/1000)*XTyfocor('cp',T_in(i))*XTyfocor('rho',T_in(i)));
else
T_ex(i)=T_in(i-1)+Q_panel/((1/1000)*XTyfocor('cp',T_in(i-1))*XTyfocor('rho',T_in(i-1)));
%Equation for exit temperature with absorption of energy
%with in- or outflow of fluid. NEEDS REVISION
end
end
%========================== FLOW TO TST ==========================%
%This is where losses should be taken into account
T_TST(i)=T_ex(i);
Q_TST=Q_panel; %The heat coming out of this system equals the heat
%coming into the system
%========================= FLOW THROUGH TST =======================%
Q_Tyfo=Pipe.Area*TST.Coil*XTyfocor('cp',T_TST(i))*(T_TST(i)-t);
%Equation for heat exchange for the fluid
if i==1
Q_TST=TST.Volume*rho*cp*(t-T_TST_bottom(i));
else
Q_TST=TST.Volume*rho*cp*(t-T_TST_bottom(i-1));
end
%Equation for heat exchange for the thermal storage tank
temp=double(solve(Q_Tyfo-Q_TST)); %Find equilibrium temperature
T_TST_bottom(i)=temp; %Set temp
T_return(i)=temp; %Set temp
%========================== FLOW TO SC ==========================%
%This is where losses should be taken into account
end
Mathieu NOE
Mathieu NOE el 16 de Nov. de 2020
OK
to be honest with you , I will not try to work on your symbolic code (simply I do not have the Symbolic Math Toolbox)
I would prefer to have first a document that describes the system and what are the governing equations , then I would like to see if I can find the equilibrium point with simple methods
are you interested to find the equilibrium point for a given condition only or are you triying to do a time simulation that covers one complete night / day cycle ?
Christopher Norling
Christopher Norling el 19 de Nov. de 2020
A typo is that the cp and rho used in the equations with Q_TST_Warm and Q_TST_Cold are supposed to separate from the cp and rho shown in Functions, but with similar behaviour. The reason is that the other equations is for the propylene glycol, whereas the Q_TST-equations is for water.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre General Applications en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Preguntada:

el 11 de Nov. de 2020

Comentada:

el 19 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by