Changing the parameters of a for loops if condition is met

4 views (last 30 days)
Hi All,
was wondering if you could shed some light on an issue, i have read other breaking of for loops but got lost with the explanation or size of the code in the example. I have a simplified version of my code below which is for running total temperature rise.
NOC =2; % number of cycles
RT=zeros(100,NOC); % running temp total
RT(1,1)=20 % starting ambient temp
for Y = 1:NOC
for x = 1:100
if x==1 && Y>1
RT(x,Y)=RT(end,Y-1);
end
value(x) = 2*x;
if x ~=1
RT(x,Y) = value(x) +RT(x-1,Y)
end
end
end
I want to add in a temperature parameter which if met, changes to a different line of code and continues, so if the RT value is greater than 1000, stop multiplying it by 2 and start multiplying it by 5 etc. I've seen as much as its not as easy to break a for loop than others but when i try, if,break and continue i am doing something wrong and it is not seeing the condition i have set as met to execute the different line of code.
Thank you in advance for any help.
  3 Comments
Jo Betteley
Jo Betteley on 1 Apr 2018
Thank you for your response, this is what i am currently trying. A better representation of my code is this:
NOC =2; % number of cycles
RT=zeros(100,NOC); % running temp total
RT(1,1)=20 % starting ambient temp
SHW =4.5; % specific heat capcaity of water
LPM = 5; % litres per minute flow rate
FR= LPM/60; % litres per minute into kg/s
Q=1 % heating of 1kW
for Y = 1:NOC
for x = 1:100
if x ==1 &&Y>1
RT(x,Y) =RT(end,Y-1);
end
if x ~=1
TR(x,Y) = Q/(SHW*FR)
RT(x,Y) = TR(x,Y) +RT(x-1,Y)
if RT(x,Y) > 50
TR(x,Y) = (Q/(SHW*FR))*1000
RT(x,Y) = TR(x,Y) +RT(x-1,Y)
end
end
end
end
So for temperature under 50 degrees i have the code running:
TR(x,Y) = Q/(SHW*FR)
RT(x,Y) = TR(x,Y) +RT(x-1,Y)
Then for temp above 50 i have it switch to:
TR(x,Y) = (Q/(SHW*FR))*1000
RT(x,Y) = TR(x,Y) +RT(x-1,Y)
The * 1000 is just so i could see the value change. I think this is now working.....I think
dpb
dpb on 1 Apr 2018
Actually, for RT<=50 you have both branches being taken. Superficial read indicates that doesn't really matter for this specific code; it just overwrites the previous answer, but is dangerous if something else were to change.

Sign in to comment.

Accepted Answer

dpb
dpb on 1 Apr 2018
...
Q=1 % heating of 1kW
QMult=[2 5]; % choices from original posting
for Y = 1:NOC
for x = 1:100
if x ==1 &&Y>1
RT(x,Y) =RT(end,Y-1);
end
if x ~=1
QFact=Qmult(RT(x,Y)>50 +1); % lookup desired factor from previous temp
TR(x,Y) = Q/(SHW*FR)*QFact;
end
end
end
...
  1 Comment
Jo Betteley
Jo Betteley on 1 Apr 2018
Ah, great stuff, i have just been having a play with that and it works, just got to make sense of the rest of the code ive got.
thank you very much for the help!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!