# For Loop not outputting what i want

1 回表示 (過去 30 日間)
Liam Wiltshire 2018 年 1 月 11 日
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 件のコメント表示 1 件の古いコメント非表示 1 件の古いコメント
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: 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 件のコメント表示 1 件の古いコメント非表示 1 件の古いコメント
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.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeProgramming Utilities についてさらに検索

### Community Treasure Hunt

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

Start Hunting!