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
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
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:
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;
  2 个评论
Guillaume
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
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Licensing on Cloud Platforms 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by