If loop: What's going wrong?

4 visualizzazioni (ultimi 30 giorni)
Liam Wiltshire
Liam Wiltshire il 10 Gen 2018
Commentato: Liam Wiltshire il 11 Gen 2018
Here is my if loop:
loopcount = 0;
TotalEnrolledNew = 0;
if TotalEnrolledNew < 10000;
x11(1) = 1500;
x22(1) = 1400;
x33(1) = 1300;
for k = 1:1:14
x11(k+1) = 0.1 * x11(k) + 2900 + 100 * k;
x22(k+1) = 0.75 * x11(k) + 0.05 * x22(k) + 250 + 50 * k;
x33(k+1) = 0.9 * x22(k) + 0.05 * x33(k);
end
TotalEnrolledNew = x11(k) + x22(k) + x33(k)
loopcount = loopcount + 1
else
x111(1) = x11(loopcount);
x222(1) = x22(loopcount);
x333(1) = x33(loopcount);
for k = 1:1:14
x111(k+1) = 0.1 * x111(k) + TotalEnrolledNew;
x222(k+1) = 0.75 * x111(k) + 0.05 * x222(k) + 300;
x333(k+1) = 0.9 * x222(k) + 0.05 * x333(k);
end
TotlEnrolledNew2 = x111(15) + x222(15) + x333(15)
end
The idea is quite blatant, to follow the first set of rules until TotalEnrollmentNew is greater than 10000, then follow the new set of rules using TotalEnrolledNew in its first iteration. My issue is that loopcount stays at 1 in the ouput, and i think this is because of the use of the first for loop meaning the program loops within the if loop, so doesn't loop correctly. I have tried doing the following without the for loop but can't make any headway.
Any and all help appreciated as always
  1 Commento
Stephen23
Stephen23 il 10 Gen 2018
Modificato: Stephen23 il 10 Gen 2018
Your code alignment is very poor, which makes following the logic of the code much harder. Poor code alignment is how beginners hide bugs in their code. You should use the default alignment of the MATLAB editor. You can also align the code in the Editor by selecting the code and then pressing ctrl + i. Correctly aligned code makes identifying your mistake much simpler:
loopcount = 0;
TotalEnrolledNew = 0;
if TotalEnrolledNew < 10000;
x11(1) = 1500;
x22(1) = 1400;
x33(1) = 1300;
for k = 1:1:14
x11(k + 1) = 0.1 * x11(k) + 2900 + 100 * k;
x22(k + 1) = 0.75 * x11(k) + 0.05 * x22(k) + 250 + 50 * k;
x33(k + 1) = 0.9 * x22(k) + 0.05 * x33(k);
end
TotalEnrolledNew = x11(k) + x22(k) + x33(k)
loopcount = loopcount + 1
else
x111(1) = x11(loopcount);
x222(1) = x22(loopcount);
x333(1) = x33(loopcount);
for k = 1:1:14
x111(k + 1) = 0.1 * x111(k) + TotalEnrolledNew;
x222(k + 1) = 0.75 * x111(k) + 0.05 * x222(k) + 300;
x333(k + 1) = 0.9 * x222(k) + 0.05 * x333(k);
end
TotlEnrolledNew2 = x111(15) + x222(15) + x333(15)
end

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 10 Gen 2018
Modificato: Stephen23 il 10 Gen 2018
These are the relevant parts of the code:
loopcount = 0;
...
if TotalEnrolledNew < 10000;
...
... irrelevant loop here
...
loopcount = loopcount + 1
else
...
... another irrelevant loop here
...
end
As you can see all loops are totally irrelevant to your loopcount variable: it is not incremented in any loop because it is not inside any loop.
It is not clear what you expect loopcount to do, or why you think it has anything to do with loops.
"The idea is quite blatant, to follow the first set of rules until TotalEnrollmentNew is greater than 10000, then follow the new set of rules using TotalEnrolledNew in its first iteration."
You don't change TotalEnrolledNew inside any loop, so it is not clear how this description corresponds to your code. If TotalEnrollmentNew should be changing "until" some condition then you need to be defining/updating its value inside some loop of some kind, perhaps something like this:
TotalEnrollmentNew = ...
...
while TotalEnrolledNew < 10000;
...
TotalEnrollmentNew = ...
end
...
... move on to the next "set of rules":
while ...
...
end
  1 Commento
Liam Wiltshire
Liam Wiltshire il 11 Gen 2018
Thank you for your help, I've opted to switch to using a while loop, and then adding an arbitrary if statement to forfil the question requirements.
Thank you

Accedi per commentare.

Più risposte (1)

Jette
Jette il 10 Gen 2018
I think there is missing a surrounding loop which loop over TotalEnrolledNew (?). Or do you have another loop around the code shown here?
  1 Commento
Liam Wiltshire
Liam Wiltshire il 10 Gen 2018
Nope no other loop, the full code is:
function Question_3
x1(1) = 1500;
x2(1) = 1400;
x3(1) = 1300;
for k = 1:1:14
x1(k+1) = 0.1 * x1(k) + 3000;
x2(k+1) = 0.75 * x1(k) + 0.05 * x2(k) + 300;
x3(k+1) = 0.9 * x2(k) + 0.05 * x3(k);
end
format short
Year_1_Students = x1(15)
Year_2_Students = x2(15)
Year_3_Students = x3(15)
Total_Enrolment = Year_1_Students + Year_2_Students + Year_3_Students
x = [1:1:15];
figure
plot(x,x1)
hold on
plot(x,x2)
hold on
plot(x,x3)
hold off
legend ('Year One Students', 'Year Two Students', 'Year Three Students')
clear
%b
loopcount = 0;
TotalEnrolledNew = 0;
k = 0;
if TotalEnrolledNew < 10000;
x11(1) = 1500;
x22(1) = 1400;
x33(1) = 1300;
for k = 1:1:14
x11(k + 1) = 0.1 * x11(k) + 2900 + 100 * k;
x22(k + 1) = 0.75 * x11(k) + 0.05 * x22(k) + 250 + 50 * k;
x33(k + 1) = 0.9 * x22(k) + 0.05 * x33(k);
end
TotalEnrolledNew = x11(k) + x22(k) + x33(k)
loopcount = loopcount + 1
else
x111(1) = x11(k);
x222(1) = x22(k);
x333(1) = x33(k);
for k = 1:1:14
x111(k+1) = 0.1 * x111(k) + TotalEnrolledNew;
x222(k+1) = 0.75 * x111(k) + 0.05 * x222(k) + 300;
x333(k+1) = 0.9 * x222(k) + 0.05 * x333(k);
end
TotlEnrolledNew2 = x111(15) + x222(15) + x333(15)
end
end
As you can see there is no other loop, and in looking a bit further the code will only ever do 1 iteration of the if loop, as the counter only ever goes to 1. I'm so damn confused, if you can shed some light it would be appreciated.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by