Calculate the number of years the money will last?
显示 更早的评论
my program is supposed to calculate the # of years the money will last for each retiree.. each year 2.5% interest is added onto the balance and the individuals withdraws 4.5%. So i created a While loop but the loop just keeps repeating, it doesn't stop. I think my main problem is that i dont no what to write for the conditional statement of While loop. Here's what i have so far
No_of_elements = 7;% the number of retirees
min_years = 40;
max_years = 50;
*% # of years of employment
r = round(min_years + (max_years-min_years).*rand(No_of_elements,1))
% deposit '
x = (400000-300000 * r-min(r)) ./ max(r) - min(r) + 300000
W = 0; % the initial withdrawal amount
years = 0;
while x >= W
format bank
% the interest
i = x*0.025
% the balance after the interest is added
B= x+i
% the amount of withdrawal
W = B*0.045
% the balance after the withdrawal
x = B-W
years = years+1;
if x<=0
fprintf ('the number of years: %d \n',years)
end
end
3 个评论
Jonathan Campelli
2015-4-8
I'm having difficulty discerning what the variable 'Z' refers to. It has not been initialized anywhere in the above code. Thanks for cleaning it up too, James.
nada Elmoghany
2015-4-8
编辑:nada Elmoghany
2015-4-8
dpb
2015-4-8
Suggest you then edit your code to reflect the changes...
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Construction 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!