MATLAB: I am not sure how to do this coding, can you please help!!

matlab for loop with compound interest

Write a for loop that will compute (as its only output) each of the following finite sum. Run it and give the answer (= the output).
(a) Suppose that Randall sets up a 401(k) retirement annuity that pays r = 6% interest, compounded annually. When he turns 35 years old, Randall deposits $3500, and each subsequent year he plans to deposit 5% more than the previous year (due to planned raises and better money management). He continues this until his 65th birthday (when he makes his last deposit and plans to retire). How much will Randall’s annuity be worth then?
Balance = 3500;
n = 30;
r = .06;
t = 1;
Balance_vec = zeros(1,n);
for i = t:n
Interest_Balance = Balance*(1+r).^i;
New_Balance = Balance+Balance*0.05;
end
disp(New_Balance);

Best Answer

Since this is your assignment, I'll give you hints. I can give more hints if needed but you'll need to leave a comment showing where you're stuck.
Balance = 3500;
n = 30;
r = .06;
t = 1;
% Balance_vec = zeros(1,n); % not used
for i = t:n
Interest_Balance = Balance*(1+r).^i; % Comments [1,2]
New_Balance = Balance+Balance*0.05; % comment [3]
end
disp(New_Balance);
Comments
[1] The exponent in the formula for compound interest is ^(n*t) where n is the number of times interest is compounded per year and t is the number of years (these are different variables than your 'n' and 't'). You're compounding annually so n=1 and you're looping over 1-year intervals so t=1.
[2] The variable name "Interest_Balance" leads me to believe you're misinterpretting the meaning of that variable. That variable contains the current value of the investment (principal + accrued interest). It's the updated balance.
[3] You're not using the New_Balance variable. You need to work that into the loop. Also, a cleaner way to write [B+B*.05] is [B*1.05]
[4] Somthing to think about after working out the other stuff: On his 65th birthday, does Randall make his final deposit before or after the interest is compounded for that year?
Related Question