For Loop not outputting what i want
1 次查看(过去 30 天)
显示 更早的评论
The question is about calculating University enrolment for 15 years using the formula for each years enrolment. In year 1 10% of people retake, so to the previous years value i need to add 10%.
75% of year 1 students continue to year two, 5% of year two students retake and 300 come in at year two from other Unis.
90% of year 2 students proceed to year 3, and 5% retake the year.
This explains where the equasions below come from.
At minimum you would expect 150 to be added to the year 1 students as 10% of the previous year are added and the first year consists of 150 people, and then only grows. In my output, though, this isn't the case.
function Question_3
x1(1) = 1500;
x2(1) = 1400;
x3(1) = 1300;
x4(1) = 1500 + 1400 + 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);
x4(k+1) = 0.1 * x1(k) + 3000 + 0.75 * x1(k) + 0.05 * x2(k) + 300 + 0.9 * x2(k) + 0.05 * x3(k);
end
This code has x1(k) plateau at 3.3333e+03 and the total enrollment plateus at 1.0e+04
I don't know how to make this loop incement the way it is supposed to.
Any and all help is, as always, appreciated.
Cheers
2 个评论
Guillaume
2018-1-11
I don't know how to make this loop incement the way it is supposed to.
What makes you think that it is not doing what it is supposed to?
回答(1 个)
Harish Ramachandran
2018-1-17
1. Let us just take the case of x1 vector. Once the 'for' loop is done, the resultant x1 vector is a 1x15 double which means that the 'for' loop did complete the entirety of its iterations.
2. The observation is that the graph plateaus as seen in the graph below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181092/image.png)
You stated that "In year 1 10% of people retake, so to the previous years value i need to add 10%". However, in your code, you have failed to take into account the previous value.
x1(k+1) = 0.1 * x1(k) + 3000;
Once you include the previous value to the code,
x1(k+1) = x1(k) + 0.1 * x1(k) + 3000;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181094/image.png)
2 个评论
Guillaume
2018-1-17
I believe the original code was actually correct. Each year, in year 1 you keep 10% of last year 1 intake, so x1(k)*0.1, and you have a new intake of 3000. The rest has moved onto year 2.
The fact that the intake actually plateau is correct.
Harish Ramachandran
2018-1-17
Thank you for commenting Guillaume.
I was under the impression that 10% of the students retake in addition to same number of existing student + an additional 3000 student, which led me to posting the snippet above. We shall wait to hear back from Liam.
If what you mentioned is correct, then the question becomes invalid due to expected behavior.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!