MATLAB: Write a function with the header: function [table] = myWhenCanIRetire(go
ID: 3861808 • Letter: M
Question
MATLAB: Write a function with the header: function [table] = myWhenCanIRetire(goal, PV, A, int) which generates a table of savings vs year [year, savings] it will take you to reach your retirement savings goal (goal) given a present sum of money (PV), an annual interest rate (int) and an annual contribution (A) added at the end of each year.
Test Cases: >> nYears = myWhenCanIRetire(200000, 1000,7000,.08) nYears = 1.00 8080.00 2.00 15726.40 3.00 23984.51 4.00 32903.27 5.00 42535.53 6.00 52938.38 7.00 64173.45 8.00 76307.32 9.00 89411.91 10.00 103564.86 11.00 118850.05 12.00 135358.06 13.00 153186.70 14.00 172441.64 15.00 193236.97 16.00 215695.92
Explanation / Answer
% matlab code
function [table] = myWhenCanIRetire(goal, PV, A, int)
year = 1;
while true
saving = PV*(1+int) + A;
table(year,1) = year;
table(year,2) = saving;
year = year + 1;
if saving >= goal
break;
end
PV = saving;
end
end
nYears = myWhenCanIRetire(200000, 1000,7000,.08);
for i=1:length(nYears)
fprintf("%0.2f %0.2f ",nYears(i,1),nYears(i,2))
end
%{
output:
nYears =
1.00 8080.00
2.00 15726.40
3.00 23984.51
4.00 32903.27
5.00 42535.53
6.00 52938.38
7.00 64173.45
8.00 76307.32
9.00 89411.91
10.00 103564.86
11.00 118850.05
12.00 135358.06
13.00 153186.70
14.00 172441.64
15.00 193236.97
16.00 215695.92
%}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.