MATLAB: Storing values in a for loop when input vector is not used in calculation

for loopoverwriting variables

I'm working through examples in "Matlab for Engineers and Scientists" and am having trouble with this problem:
=============================
Suppose you deposit $50 in a bank account every month for a year. Every month, after the deposit has been made, interest at the rate of 1% is added to the balance: After one month the balance is $50.50, and after two months it is $101.51. Write a program to compute and print the balance each month for a year. Arrange the output to look something like this:
MONTH MONTH-END BALANCE
1 50.50
2 101.51
3 153.02
12 640.47
=============================
Obviously the vector of months shows up fine, but I can't get the balance vector figured out…if I used the months vector in the calculation of balance I know I'd get a vector, but my current code overwrites the previous balance from last month. I don't use the months vector for anything but a counter/index:
=============================
Monthly=50;
Months=[1:12];
Balance=0;
for k=1:length(Months)
Balance=(Balance+(Monthly))*1.01;
end
disp('MONTH MONTH-END BALANCE')
disp([Months' Balance'])
=============================
I know I can do this using the month as an exponent on the rate but just want to know if there's a way to get a vector of balances by modifying the code above.

Best Answer

Monthly=50;
Months=[1:12];
Balance=0;
disp('MONTH MONTH-END BALANCE')
for k=1:length(Months)
Balance=(Balance+(Monthly))*1.01;
disp([Months(k) Balance])
end
Related Question