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

2 Ansichten (letzte 30 Tage)
John Hershberger
John Hershberger am 3 Apr. 2020
Bearbeitet: Adam Danz am 3 Apr. 2020
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);

Akzeptierte Antwort

Adam Danz
Adam Danz am 3 Apr. 2020
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?

Weitere Antworten (1)

David Hill
David Hill am 3 Apr. 2020
b = 3500;
Balance=3500;
r = 1.06;
rr=1.05;
for i = 1:30
Balance = Balance*r+b*rr^i;
end
  4 Kommentare
David Hill
David Hill am 3 Apr. 2020
Except, when rr=1 the annual contribution is 3500. To get no annual contribution, rr would have to be 0.
Adam Danz
Adam Danz am 3 Apr. 2020
Bearbeitet: Adam Danz am 3 Apr. 2020
Ahh... I see what you did now. +1
Just to note, without annual contributions, the basic formula for interest compounded annually is
cifcn = @(P, I, nT, nY) P*(1+I/nT)^(nT * nY);
% P is the principal (3500)
% I is the interest rate (0.06)
% nT is number of times compounded anually (1)
% nY is number of years (30)
I don't want to give away the annual contribution part of the eq since it's the OP's assignment.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Birthdays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by