Compound interest in a for loop

I need to create a loop to calculate the compound interest of a Roth IRA each year. The function has to input p(principal amount), c(yearly contribution), r(return rate), and n(number of years). It has to output a v vector of values in the account the first day of each year. I have written a loop that should calculate the yearly compound interest through the y=equation, and then add it to the vector v. However this loop only prints out the initial values p. How do I make this create a vector of compount interests. I also have to plot the vector that comes out versus the year, but I have no idea what syntax to use to do that.
v=p;
for N=2:length(n)
y=(v*(1+r).^N+c)
v=v+y(N);
end

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 12 日
編集済み: Sulaymon Eshkabilov 2021 年 6 月 12 日

0 投票

v=p;
for N=2:length(n)
y(N)=(v(N-1)*(1+r).^N+c)
v(N)=v(N-1)+y(N); % specify the index and you will get all the values
end

3 件のコメント

Petch Anuwutthinawin
Petch Anuwutthinawin 2021 年 6 月 12 日
It still just reprints the input p value and does not output a vector.
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 12 日
編集済み: Sulaymon Eshkabilov 2021 年 6 月 12 日
v=p;
for N=2:length(n)
y(N)=(v(N-1)*(1+r).^N+c);
v(N)=v(N-1)+y(N); % specify the index and you will get all the values
end
disp(y)
disp(v)
% or
% fprintf(...)
Petch Anuwutthinawin
Petch Anuwutthinawin 2021 年 6 月 12 日
編集済み: Petch Anuwutthinawin 2021 年 6 月 12 日
all the code does is print out the input values of p, and not a vector and not the correct calculations

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2021 年 6 月 12 日

編集済み:

2021 年 6 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by