Need help with Loop coding

6 views (last 30 days)
Joshua
Joshua on 14 Oct 2014
Commented: Mohammad Abouali on 15 Oct 2014
Hi,
So I'm trying to write a program for a school project that calculates the temperatures within a wall cavity and indoor temperature at every hour of the day for 5 days with varying heat generation at different hours. I've asked the professor for help and his replies were "I know nothing about programing, so seek help from someone else" -_-
the first loop is the 5 days and the second loop is the the hourly temperature change. (a loop within a loop)
I've tried many different things with no success. It would be greatly appreciated if someone could help.
So far my program looks like this, The B-Matrix represents the total varying heat generation at each hour and other cooling loads. My current loop isn't working and I've lost hope figuring out how to get this program to work.
Thanks and Greatly appreciate your time (IMMENSELY)
% North Wall Solar Radiation
Qsn = [ 0
0
0
0
32
57
83
87
97
107
114
116
114
107
97
87
83
57
32
0
0
0
0
0 ];
% East Wall Solar Radiation
Qse = [ 0
0
0
0
71
472
651
679
606
457
252
126
114
107
97
82
63
38
5
0
0
0
0
0 ];
% South Wall Solar Radiation
Qss = [ 0
0
0
0
5
38
68
107
209
318
394
420
394
318
209
107
68
38
5
0
0
0
0
0 ];
% West Wall Solar Radiation
Qsw = [ 0
0
0
0
5
38
63
82
97
107
114
126
252
457
606
679
651
472
71
0
0
0
0
0 ];
% Roof Solar Radiation
Qsr = [ 0
0
0
0
9
119
286
454
595
704
772
795
772
704
595
454
286
119
9
0
0
0
0
0 ];
% H-VALUES
Hco = 23; % Outdoor Convection
Hci = 8.3; % Indoor Convection
Hcr = 9.1; % Downward Flow Convection
Hcf = 6.1; % Upwards Flor Convection
% K-WALL
K1 = 0.23; % Plaster
K2 = 0.023; % Insulation (Studded Framing)
K3 = 0.12; % Plywood
K4 = 0.208; % Air Space
K5 = 0.077; % Face Brick
% K-ROOF
K6 = 0.23; % Plaster
K7 = 0.045; % Insulation Loose Fill
K8 = 0.36; % Air Space
K9 = 0.12; % Wood Sheathing
K10 = 0.19; % Plywood
K11 = 0.011; % Membrane
% THICKNESS WALL (METERS)
X1 = 0.0095; % Plaster
X2 = 0.14; % Insulation (Studded Framing)
X3 = 0.015875; % Plywood
X4 = 0.019; % Air Space
X5 = 0.1; % Face Brick
% THICKNESS ROOF (METERS)
X6 = 0.00625; % Plaster
X7 = 0.2032; % Insulation (Loose Fill)
X8 = 0.050; % Air Space
X9 = 0.050; % Wood Sheathing
X10 = 0.015875; % Plywood
X11 = 0.0006; % Membrane
%Area Windows
Awe = 1.5; % East Window
Aww = 1.5; % West Window
Aws = 4; % South Window
%Net Wall Areas
Ae = 20.5; % East Wall
Aw = 20.5; % West Wall
An = 17.6; % North Wall
As = 13.6; % South Wall
Ar = 80; % Roof
Af = 80; % Floor
% Delta X for each node
% Wall
DX0 = X1/2; % Node 1 ( Half Plaster )
DX1 = X2/2 + X1/2; % Node 2 ( Half Plaster + Half Insulation )
DX2 = X2/2 + X3/2; % Node 3 ( Half Insulation + Half Plywood )
DX3 = X3/2 + X4/2; % Node 4 ( Half Plywood + Half Air Space)
DX4 = X4/2 + X5/2; % Node 5 ( Half Air Space + Half Face Brick )
DX5 = X5/2; % Node 6 ( Half Face Brick)
% Roof
DX6 = X6/2; % Node 1 ( Half Plaster )
DX7 = X6/2 + X7/2; % Node 2 ( Half Plaster + Half Insulation )
DX8 = X7/2 + X8/2; % Node 3 ( Half Insulation + Half Air Sapce )
DX9 = X8/2 + X9/2; % Node 4 ( Half Air Space + Half Wood Sheathing )
DX10 = X9/2 + X10/2; % Node 5 ( Half Wood Sheathing + Half Plywood )
DX11 = X10/2 + X11/2; % Node 6 ( Half Plywood + Half Membrane )
DX12 = X11/2; % Node 7 ( Half Membrane )
% Floor
DX13 = 0.0508; % Half thickness of a 4" concrete slab
%Miscellaneous
V = 176; % Volume
Rho = 1.247; % Air Density
CP = 1.005; %
Tmax = 35; % Max Temperature
Tmin = 23; % Minimum Temperature
PI = pi; % Pi
ACH = 0.5; % Air Change Per Hour
Alpha = 0.68; % Absorption Coefficient Walls
Tao = 0.79; % Transmittance Coefficient Windows
Alpha1 = 0.16; % Absorption factor for windows
Dt = 3600; % Conversion to seconds
% Setting Matrices to zeros
A = zeros(33,33); % Heat Transfer Matrix
B = zeros(33,1); % Old Temperature Value & Heat Loads
X = zeros(33,24); % Temperature Matrix, (unknown Values)
T = zeros(33,24); % Temperature Matrix
H = zeros(33,24); % Internal Loads put into a Table
OT = zeros(24,1); % Outside Temp. Matrix
ST = zeros(24,1); % Sky Temp. Matrix
% A-Matrix
% East Wall
% Node 1
A(1,1) = Ae*((K1/X1) + Hci + (Rho*DX0*CP)/Dt);
A(2,1) = (-1)*Ae*(K1/X1);
A(33,1) = (-1)*Hci*Ae;
% Node 2
A(1,2) = (-1)*Ae*(K1/X1);
A(2,2) = Ae*(K1/X1 + K2/X2 + (Rho*DX1*CP)/Dt);
A(3,2) = (-1)*Ae*K2/2;
% Node 3
A(2,3) = (-1)*Ae*(K2/X2);
A(3,3) = Ae*(K2/X2 + K3/X3 + (Rho*DX2*CP)/Dt);
A(4,3) = (-1)*Ae*K3/X3;
% Node 4
A(3,4) = (-1)*Ae*(K3/X3);
A(4,4) = Ae*(K3/X3 + K4/X4 + (Rho*DX3*CP)/Dt);
A(5,4) = (-1)*Ae*(K4/X4);
% Node 5
A(4,5) = (-1)*Ae*(K4/X4);
A(5,5) = Ae*(K4/X4 + K5/X5 + (Rho*DX4*CP)/Dt);
A(6,5) = (-1)*Ae*(K5/X5);
% Node 6
A(5,6) = (-1)*Ae*(K5/X5);
A(6,6) = Ae*(K5/X5 + Hco + (Rho*DX5*CP)/Dt);
% West Wall
% Node 1
A(7,7) = Aw*((K1/X1) + Hci + (Rho*DX0*CP)/Dt);
A(8,7) = (-1)*Aw*(K1/X1);
A(33,7) = (-1)*Hci*Aw;
% Node 2
A(7,8) = (-1)*Aw*(K1/X1);
A(8,8) = Aw*(K1/X1 + K2/X2 + (Rho*DX1*CP)/Dt);
A(9,8) = (-1)*Aw*K2/X2;
% Node 3
A(8,9) = (-1)*Aw*(K2/X2);
A(9,9) = Aw*(K2/X2 + K3/X3 + (Rho*DX2*CP)/Dt);
A(10,9) = (-1)*Ae*K3/X3;
% Node 4
A(9,10) = (-1)*Aw*(K3/X3);
A(10,10) = Aw*(K3/X3 + K4/X4 + (Rho*DX3*CP)/Dt);
A(11,10) = (-1)*Aw*(K4/X4);
% Node 5
A(10,11) = (-1)*Aw*(K4/X4);
A(11,11) = Aw*(K4/X4 + K5/X5 + (Rho*DX4*CP)/Dt);
A(12,11) = (-1)*Aw*(K5/X5);
% Node 6
A(11,12) = (-1)*Aw*(K5/X5);
A(12,12) = Aw*(K5/X5 + Hco + (Rho*DX5*CP)/Dt);
% South Wall
% Node 1
A(13,13) = As*((K1/X1) + Hci + (Rho*DX0*CP)/Dt);
A(14,13) = (-1)*As*(K1/X1);
A(33,13) = (-1)*Hci*As;
% Node 2
A(13,14) = (-1)*As*(K1/X1);
A(14,14) = As*(K1/X1 + K2/X2 + (Rho*DX1*CP)/Dt);
A(15,14) = (-1)*As*K2/X2;
% Node 3
A(14,15) = (-1)*As*(K2/X2);
A(15,15) = As*(K2/X2 + K3/X3 + (Rho*DX2*CP)/Dt);
A(16,15) = (-1)*Ae*K3/X3;
% Node 4
A(15,16) = (-1)*As*(K3/X3);
A(16,16) = As*(K3/X3 + K4/X4 + (Rho*DX3*CP)/Dt);
A(17,16) = (-1)*As*(K4/X4);
% Node 5
A(16,17) = (-1)*As*(K4/X4);
A(17,17) = As*(K4/X4 + K5/X5 + (Rho*DX4*CP)/Dt);
A(18,17) = (-1)*As*(K5/X5);
% Node 6
A(17,18) = (-1)*As*(K5/X5);
A(18,18) = As*(K5/X5 + Hco + (Rho*DX5*CP)/Dt);
% North Wall
% Node 1
A(19,19) = An*((K1/X1) + Hci + (Rho*DX0*CP)/Dt);
A(20,19) = (-1)*An*(K1/X1);
A(33,19) = (-1)*Hci*An;
% Node 2
A(19,20) = (-1)*An*(K1/X1);
A(20,20) = An*(K1/X1 + K2/X2 + (Rho*DX1*CP)/Dt);
A(21,20) = (-1)*An*K2/DX2;
% Node 3
A(20,21) = (-1)*An*(K2/X2);
A(21,21) = An*(K2/X2 + K3/X3 + (Rho*DX2*CP)/Dt);
A(22,21) = (-1)*An*K3/X3;
% Node 4
A(21,22) = (-1)*An*(K3/X3);
A(22,22) = An*(K3/X3 + K4/X4 + (Rho*DX3*CP)/Dt);
A(23,22) = (-1)*An*(K4/X4);
% Node 5
A(22,23) = (-1)*An*(K4/X4);
A(23,23) = An*(K4/X4 + K5/X5 + (Rho*DX4*CP)/Dt);
A(24,23) = (-1)*An*(K5/X5);
% Node 6
A(23,24) = (-1)*An*(K5/X5);
A(24,24) = An*(K5/X5 + Hco + (Rho*DX5*CP)/Dt);
% Roof
% Node 1
A(25,25) = Ar*(K6/X6 + Hcr + (Rho*DX6*CP)/Dt);
A(26,25) = (-1)*Ar*K6/X6;
A(33,25) = (-1)*Hcr*Ar;
% Node 2
A(25,26) = (-1)*Ar*(K6/X7);
A(26,26) = Ar*(K6/X6 + K7/X7 + (Rho*DX7*CP)/Dt);
A(27,26) = (-1)*Ar*(K7/X7);
% Node 3
A(26,27) = (-1)*Ar*(K7/X7);
A(27,27) = Ar*(K7/X7 + K8/X8 + (Rho*DX8*CP)/Dt);
A(28,27) = (-1)*Ar*(K8/X8);
% Node 4
A(27,28) = (-1)*Ar*(K8/X8);
A(28,28) = Ar*(K8/X8 + K9/X9 + (Rho*DX9*CP)/Dt);
A(29,28) = (-1)*Ar*(K9/X9);
% Node 5
A(28,29) = (-1)*Ar*(K9/X9);
A(29,29) = Ar*(K9/X9 + K10/X10 + (Rho*DX10*CP)/Dt);
A(30,29) = (-1)*Ar*(K10/X10);
% Node 6
A(29,30) = (-1)*Ar*(K10/X10);
A(30,30) = Ar*(K10/DX10 + K11/X11 + (Rho*DX11*CP)/Dt);
A(31,30) = (-1)*Ar*(K11/X11);
% Node 7
A(30,31) = (-1)*Ar*(K10/X10);
A(31,31) = Ar*(K11/X11 + Hco + (Rho*DX12*CP)/Dt);
% Indoor Temp, & Floor
% Floor
A(32,32) = Af*Hcf + (Rho*Af*DX13*CP)/Dt;
A(33,32) = Af*Hcf;
% Internal Heat Equation
A(1,33) = (-1)*Ae*Hci;
A(7,33) = (-1)*Aw*Hci;
A(13,33) = (-1)*As*Hci;
A(19,33) = (-1)*An*Hci;
A(25,33) = (-1)*Ar*Hcr;
A(32,33) = (-1)*Af*Hcf;
A(33,33) = Hci*(Aw + Ae + As + An) + Hcr*Ar...
+ Hcf*Af + V*ACH*Rho*CP*1000;
X-Matrix
X = [ T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14...
T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27...
T28, T29, T30, T31, Tf, Tin];
%setup Q-value matrix
% QVALUES
QF = 700; % Heat Gain From Fridge
QS = 2000; % Heat Gain From Stove
QCOM = 40; % Heat Gain From Computer
QL = 60; % Heat Gain From Lights
QTV = 250; % Heat Gain From TV
QCOF = 900; % Heat Gain From Coffee Machine
QAM = 120; % Heat Gain From Awake Man
QSM = 80; % Heat Gain From Sleeping Man
QAW = 116; % Heat Gain From Awake Woman
QSW = 75; % Heat Gain From Sleeping Woman
%Qtotal matrix
Qt = zeros(24,1);
Qt(1,1) = QSM + QSW + QF;
Qt(2,1) = Qt(1,1);
Qt(3,1) = Qt(1,1);
Qt(4,1) = Qt(1,1);
Qt(5,1) = Qt(1,1);
Qt(6,1) = Qt(1,1);
Qt(7,1) = QAM + QAW + QF + QS + QCOF + QL;
Qt(8,1) = QAM + QF + QTV + QL;
Qt(9,1) = Qt(8,1);
Qt(10,1) = Qt(8,1);
Qt(11,1) = Qt(8,1);
Qt(12,1) = QAM + QF + QCOM + QTV + QL;
Qt(13,1) = Qt(12,1);
Qt(14,1) = Qt(12,1);
Qt(15,1) = Qt(12,1);
Qt(16,1) = QAM + QF + QTV + QL;
Qt(17,1) = QAM + QAW + QF + QTV + QL;
Qt(18,1) = QAM + QAW + QF + QS + QL;
Qt(19,1) = QAM + QAW + QF + QTV + QL;
Qt(20,1) = Qt(19,1);
Qt(21,1) = Qt(19,1);
Qt(22,1) = Qt(19,1);
Qt(23,1) = Qt(1,1);
Qt(24,1) = Qt(1,1);
% Initial Temperature Assumption
T1 = -6000;
for t = 1:24
OT(t,1) = (((Tmax + Tmin)/2)+((Tmax - Tmin)/2)*sin(((t - 9)/12)*PI));
ST(t,1) = OT(t)-5;
% B-matrix
B(1,1) = T1*(Rho*Ae*DX0*CP)/Dt;
B(2,1) = T2*(Rho*Ae*DX1*CP)/Dt;
B(3,1) = T3*(Rho*Ae*DX2*CP)/Dt;
B(4,1) = T4*(Rho*Ae*DX3*CP)/Dt;
B(5,1) = T5*(Rho*Ae*DX4*CP)/Dt;
B(6,1) = T6*(Rho*Ae*DX5*CP)/Dt + Alpha*Qse(t,1)*Ae + ST(t,1)*Hco*Ae;
B(7,1) = T7*(Rho*Aw*DX0*CP)/Dt;
B(8,1) = T8*(Rho*Aw*DX1*CP)/Dt;
B(9,1) = T9*(Rho*Aw*DX2*CP)/Dt;
B(10,1) = T10*(Rho*Aw*DX3*CP)/Dt;
B(11,1) = T11*(Rho*Aw*DX4*CP)/Dt;
B(12,1) = T12*(Rho*Aw*DX5*CP)/Dt + Alpha*Qsw(t,1)*Aw + ST(t,1)*Hco*Aw;
B(13,1) = T13*(Rho*As*DX0*CP)/Dt;
B(14,1) = T14*(Rho*As*DX1*CP)/Dt;
B(15,1) = T15*(Rho*As*DX2*CP)/Dt;
B(16,1) = T16*(Rho*As*DX3*CP)/Dt;
B(17,1) = T17*(Rho*As*DX4*CP)/Dt;
B(18,1) = T18*(Rho*As*DX5*CP)/Dt + Alpha*Qss(t,1)*As + ST(t,1)*Hco*As;
B(19,1) = T19*(Rho*An*DX0*CP)/Dt;
B(20,1) = T20*(Rho*An*DX1*CP)/Dt;
B(21,1) = T21*(Rho*An*DX2*CP)/Dt;
B(22,1) = T22*(Rho*An*DX3*CP)/Dt;
B(23,1) = T23*(Rho*An*DX4*CP)/Dt;
B(24,1) = T24*(Rho*An*DX5*CP)/Dt + Alpha*Qsn(t,1)*An + ST(t,1)*Hco*An;
B(25,1) = T25*(Rho*Ar*DX0*CP)/Dt;
B(26,1) = T26*(Rho*Ar*DX1*CP)/Dt;
B(27,1) = T27*(Rho*Ar*DX2*CP)/Dt;
B(28,1) = T28*(Rho*Ar*DX3*CP)/Dt;
B(29,1) = T29*(Rho*Ar*DX4*CP)/Dt;
B(30,1) = T30*(Rho*Ar*DX4*CP)/Dt;
B(31,1) = T31*(Rho*Ar*DX5*CP)/Dt + Alpha*Qsr(t,1)*Ar + ST(t,1)*Hco*Ar;
B(32,1) = Alpha1*Tao*(Qse(t,1)*Awe + Qsw(t,1)*Aww + Qss(t,1)*Aws)...
+ Tf*((Rho*Af*DX13*CP/Dt));
B(33,1) = (V*ACH*Rhi*CP*1000*OT(t,1))-(Qt(t,1));
X(:,t) = A\B;
T = round(X/0.01)*0.01;
H(:,t) = abs(Qt);
end
  4 Comments
José-Luis
José-Luis on 14 Oct 2014
Edited: José-Luis on 14 Oct 2014
What is it exactly you want people to do? If you just say "Please look at my code and tell me what's wrong" you might not elicit that many answers. Plus, it could be equalled to someone doing your homework for you.
Please try to ask specific Matlab questions.
"It isn't working" isn't a very helpful description of your problem.
Joshua
Joshua on 14 Oct 2014
Edited: Joshua on 14 Oct 2014
I don't want people doing the coding, (I supplied the program to let others understand what i have done so far) as I mentioned before, what I'm trying to create is a loop that generates the temperatures at every hour of the day for 5 days. Loop 1: is for the 5 days, Loop 2: (subloop) is to determine the X matrix with matrix B varying in values at every hour.
I don't necessarily want the exact answer, I'm asking for the structure of the loop so that I can input it into my coding. like how to create the loop.
I have an initial temperature and have to solve for the new temperature. We never had a matlab course, so this project was given to us while they expect that we already know how to code in matlab.
the answer can be answered in a basic expression with simple varibles so that i can implement the structure of the loop coding into my program

Sign in to comment.

Accepted Answer

Mohammad Abouali
Mohammad Abouali on 15 Oct 2014
So your code to calculate temperature is too long to follow:
Here is the general idea.
Let's say you can calculate T at day n hour h using function TempCalc(inputs list). Your general loop could be something like this
Temperature=zeros(24,nDays)
for d=1:nDays
for h=1:24
Temperature(h,d)=TempCalc(input variable List)
end
end
You can later simply plot the time variation of Temperature by just typing plot(Temperature(:)).
  2 Comments
Joshua
Joshua on 15 Oct 2014
Thank you, This is exactly what i was looking for, jus the structure on how to create the loop.
I appreciate your time in helping me. thank you once again.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!