using for loops to calculate compound interest with yearly contributions

33 ビュー (過去 30 日間)
Kayln Hess
Kayln Hess 2022 年 4 月 26 日
コメント済み: Kayln Hess 2022 年 4 月 28 日
My end goal is to calculate how many years it will take to reach a specified amount, when give an initial balance compounded annually with year contributions
I'd like to be able to specify
original account balance(P)
yearly contributions (C)
desired final amount(N)
rate of yearly interest (r)
I’d like to know what the account holds at each year. I was hoping to use an array and they count that numbers in the array (using numel(y)) to determine the number of years required to reach my final desired amount(N).
N=17804;
P=1000;
C=1000;
r=(10./100);
while P<=N
y(P)=(P*(1+r));
P=y+C;
end
x = numel(y)
The problem I am running into is that I the line “y(P)=(P*(1+r));” seems to create an array in which P always starts as 0 regardless of what value I assign to it.

回答 (3 件)

VBBV
VBBV 2022 年 4 月 27 日
N=17804;
P=1000;
C=1000;
r=(10/100);
I = 1;
while P<=N
y(I)=(P*(1+r)); % use an index for desired amount,
P=y(I)+C;
I = I+1;
end
y(end)
ans = 1.7531e+04
plot(y) % calculated amount thru compounding
% hold on
% bar(N) % desired amount
x = numel(y)
x = 10
  2 件のコメント
VBBV
VBBV 2022 年 4 月 27 日
編集済み: VBBV 2022 年 4 月 27 日
N=17804;
P=1000;
C=1000;
r=(10/100);
I = 1;
format shortG
while P<=N
y(I)=(P*(1+r)); % use an index
P=y(I)+C;
if P >= N
disp(['The desired amount reached in year ', num2str(I-(y(end)-y(end-1))/N)]) % if you want to know which year
end
I = I+1;
end
The desired amount reached in year 9.8543
x = numel(y)
x =
10
if you want to know exactly which year desired amount reached, then you can use a condition to check it
Kayln Hess
Kayln Hess 2022 年 4 月 28 日
Awesome thank you for taking the time to respond!!

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


Prakash S R
Prakash S R 2022 年 4 月 27 日
編集済み: Prakash S R 2022 年 4 月 27 日
The problem is as follows:
You are thinking of y(P) as "y-as-a-function-of-P'.
But when you write y(P), Matlab interprets it as 'vector y, at index number P'. And because P starts as 1000, it initializes a vector y whose first 999 elements are zero!
What you want is a loop counter that is used as the index into the vector y. Then you can count the number of elements in y as before:
N=17804;
P=1000;
C=1000;
r=(10./100);
ind = 1;
while P<=N
y(ind)=(P*(1+r));
P=y(ind)+C;
ind = ind+1;
end
x = numel(y)
  1 件のコメント
Kayln Hess
Kayln Hess 2022 年 4 月 28 日
Awesome thank you for taking the time to respond!!

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


ClementJ
ClementJ 2022 年 4 月 27 日
Hi,
I think you need to add the cmp value in your loop and I think it is :
year_cmp = year_cmp + 1;
y(year_cmp) = (P*(1+r));
In your script, you have:
N=17804; % desired final amount(N)
P=1000; % original account balance(P)
C=1000; % yearly contributions (C)
r=(10./100); % rate of yearly interest (r)
year_cmp = 0;
while P<=N
year_cmp = year_cmp + 1;
y(year_cmp) = (P*(1+r));
P = y(year_cmp) + C;
end
x = numel(y); % You can use year_cmp also
  1 件のコメント
Kayln Hess
Kayln Hess 2022 年 4 月 28 日
Awesome thank you for taking the time to respond!!

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by