Question gives us the vector increase which has the percentage increase for the fees of upcoming years and asks us to find the tuition fee after 4 years if current fee for a year is 5000
Exact Question:
My code is as below. It runs but final value is not correct.
sum = 5000;
increase = [10 8 10 16 15 4 6 7 8 10 8 12 14 15 8 7 6 5 7 8 9 8];
for x = increase(:,1):increase(:,3)
sum = (sum*(x/100)) + sum;
end
fprintf('%d', sum)

 Réponse acceptée

Your code predicts a 4 year tuition of 5500. Clearly that is not correct. How would I solve this, even if I was not using a loop?
format short g
increase = [10 8 10 16 15 4 6 7 8 10 8 12 14 15 8 7 6 5 7 8 9 8];
tuition = 5000*cumprod(1 + [0,increase]/100)
tuition = 1×23
1.0e+00 * 5000 5500 5940 6534 7579.4 8716.4 9065 9608.9 10282 11104 12214 13192 14775 16843 19370 20919 22383 23726 24913 26657 28789 31380 33891
That is the YEARLY tuition. If you want 4 years, then we could do this:
fouryeartuition = conv(tuition,[1 1 1 1],'valid')
fouryeartuition = 1×20
1.0e+00 * 22974 25553 28770 31895 34970 37672 40060 43209 46792 51285 57024 64179 71906 79515 86398 91942 97679 1.0408e+05 1.1174e+05 1.2072e+05
And my guess is you were being asked to produce the total tuition, as if you started this year, or any year for the next 20 years.
plot(fouryeartuition,'o')
Anyway, 5500 is clearly not correct, although I think the question is far too vague to have a good answer. A good, well-posed question would have been explicit. Why do I think the request was to see a prediction of the total 4 year tuition over the next 20 years? Because they gave you 20 years of yearly tuition hikes as information!
Anway, why did your loop fail? LOOK AT WHAT YOU WROTE!
for x = increase(:,1):increase(:,3)
What is increase(:,1)? increase(:,3)
increase(:,1)
ans =
10
increase(:,3)
ans =
10
So what did your loop do? For example, try this:
for i = 10:10
i
end
i =
10
Do you see the loop only runs for ONE iteration?

1 commentaire

Zain
Zain le 6 Jan 2023
Oh ok I do see what you mean and I also see my mistake now. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by