Having trouble with Complicated for loop

I have created this program, but having issues with my loop. My program is suppose to do calculation 24 times within a loop of 5. so intotal i have 120 iterations, and at each iteration the new values become the old values for the following iteration. my problem is that I can't get my 24th value to become my first value of the next loop.
for example my loop looks like this:
To = 20; %initial value
for h = 1:5 % days
for t = 1:24 %hours
MatrixB (1,2) = To* "Values"
X(:,t) = A\B;
Tn = X; % New Temp
To(:,t+1) = Tn(:,t); %new temp becoming old temp for next iteration
end
end
now my issue is trying to get the value on the 24th hour on day 1 to become my initial value of To on hour 1 of day 2 so that I can continue the iteration for another 4 days. so far my values are the same for each day which cannot be.

 Réponse acceptée

Star Strider
Star Strider le 20 Oct 2014
I couldn’t run your code, so I did the best I could to simulate it. Here, ‘X’ updates and carries over. See if it works with your calculations:
X = zeros(2, 5, 24); % Preallocate
To = [0; 0]; % Initial Carryover Matrix
for h = 1:5
if h > 1
To = X(:,(h-1),24); % Last Matrix (Days 2-5)
end
for t = 1:24
X(:,h,t) = [h; t] + To; % Calculations
end
end
Q = X(:,:,24); % Selected Output (Discard)
Update the preallocation step with the number of rows in ‘X’ in your calculations. (I used 2 here.)

7 commentaires

Joshua
Joshua le 20 Oct 2014
Modifié(e) : Joshua le 20 Oct 2014
I simplified my code to a basic version. ( just to show the structure of my coding) but I attached the actual code to this comment if you want to see the actual program
if you don't quite understand the program, I have a matrix and im trying to solve X matrix from Ax=b, but the values in the B matrix vary with the hours (solar heat gain). so I need the 24th hour to become my first hour of day 2 for the next series of iteration and so forth for 5 days. Thanks for your help, I appreciate it a lot.
Joshua
Joshua le 20 Oct 2014
I'm currently trying to implement your coding structure into mine, but I'm still having trouble. My first 24hr iteration works fine. but but it's when i want to use the values on the 24th hour for the next iteration (h=2, t=1)is what T'm having issues with. My X-matrix are the values I'm trying to solve for, at each iteration, and then I need to take my X-values of hour 4 (for example, which is t=4) into To, so that I can calculate the temperature change at the next hour (hour 5, t=5) based on the previous hour. it's just carrying the 24th hour (t=24, h = 1) to the following day hour (t=1,h=2)
I was getting:
Subscripted assignment dimension mismatch.
Error in ThermalPartB (line 403)
To(:,h) = To;
so after some experimentation, I put:
To = zeros(33,1,24);
and replaced the ‘To’ assignment with:
To(:,h,t+1) = Tn(:,t);
It runs, but I can’t tell if it’s doing what you want. The ‘h’ subscript is the day, but I’m not sure how you want to incorporate it into your code.
Joshua
Joshua le 21 Oct 2014
After analyzing the whole output, it's not doing what I want it to do. I've tried modifying the loop coding with what you've given and still no luck.
This is so complicated, I'm losing hope with this coding. I've been at it for over a week now -_-..
like if you run the program with h = 1:1 and t = 1:24, and you click on the Calculated X-varibles, in the workspace, in columns 24 of table X to become my To for loop h=2, t=1..
I having so much trouble with this, it's starting to get frustrating.
But thank you, I really appreciate your time and help with this, it means a lot.
Star Strider
Star Strider le 21 Oct 2014
Modifié(e) : Star Strider le 21 Oct 2014
My pleasure!
I believe I’ve figured out how to incorporate the logic of my original Answer into your actual code.
Before the ‘h’ loop, add this necessary preallocation:
Td = zeros(33,1);
then between the initial statements of the for loops, add this if block:
for h = 1:2 % Loop for the 5 days
if h > 1
To = Td;
end
for t = 1:24
then add the ‘Td’ assignment here:
Tn = X;
To(:,t+1) = Tn(:,t);
Td = To;
This takes advantage of the default behaviour in loops of a variable that is not otherwise indexed taking on the last value in the loop. If all goes well (and I honestly don’t understand your code well enough to decide this myself), ‘Td’ will take on the last value for the day and become the first value for the next day, using the logic in the if block. (This is a version of my original Answer.)
Your code with these changes runs. Please run it and see if it works as you want it to. I need you to determine if it is now giving the correct results.
EDIT — You might want to assign ‘Td’ as equaling the last value of ‘Tn’ instead, since it is actually hour 24:
Td = Tn(:,t);
Just thought of that. Experiment with it to see which one gives you the results you want.
Ahhhh Finally!!! I was able to figure out how to do the loop the way I wanted to. I did what you suggested, it didn't quite function properly but I just added a little tweek with the "if' statement and it worked magically.
my loop resembles this now
for h = 1:5 % Loop for the 5 days
if h > 1
To(:,1) = To(:,t+1);
end
for t = 1:24 %Hours
"B(m,n) = Values...."
X(:,t) = A\B;
X = round(X/0.01)*0.01;
Tn = X;
To(:,t+1) = Tn(:,t);
end
end
I can't Thank you enough for helping me. I sincerely appreciate your time in helping me. After many days of working on it and asking around for help, it finally works. Thank you once again.
Star Strider
Star Strider le 21 Oct 2014
My pleasure!
I wasn’t quite sure how to set the carry-over value of ‘To’, but I knew the solution was to define it at the start of the ‘h’ loop. My contribution was in part just having seen it for the first time! I’m happy you got it to work.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by