Compound Interest with monthly Contributions, Unable to vectorize.

조회 수: 6 (최근 30일)
Hello, I would like help with vectorizing this code so that I can plot the answer. I have been able to get individual answers for each month, however, I cannot write this function in terms of months, so thats why I can not get my answer into a vector. Here's what I have so far.
%
New_balance=1000
for month=[0:18*12]
New_balance=New_balance*1.005+100
end
It is monthly interest of .5% for 18 years with 100 every month and an initial $1000. I want to be able to plot(months,New_balance), including the 0th month of $1000. Thank You.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 11월 13일
n = 18*12+1;
New_balance([1,n],1)=[1000;0];
for j1=2:n
New_balance(j1)=New_balance(j1-1)*1.005+100;
end
or
n = 18*12;
New_balance = filter(1,[1 -1.005],[1000;ones(n,1)*100]);
  댓글 수: 2
Billy
Billy 2012년 11월 13일
편집: Billy 2012년 11월 13일
You have the part of the answer that I want, but my directions tell me after the loop, I should be able to show the final amount at the end of 18 years. Your answer fits what I described, but I just noticed this detail. Do you know how I would be able to extract the final value by placing something in or after the for loop? Is there a way to define the final value as a variable? Thanks.
Oleg Komarov
Oleg Komarov 2012년 11월 13일
New_balance(end)
@Billy: invest some time into the getting started guide (the chapter on matrix manipulations).

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

C.J. Harris
C.J. Harris 2012년 11월 13일
You don't even need to vectorise, just calculate the answer directly. The answer below is slightly lower than your answer. But in your calculation you appear to accrue interest in month zero (time of deposit).
P = 100;
i = 0.005;
A = 1000;
n = 18*12;
FV1 = ( (1 + i)^ n ) * A;
FV2 = P * ( ( (1 + i)^n - 1) / i );
FV = FV1 + FV2;
  댓글 수: 1
Billy
Billy 2012년 11월 13일
I should have mentioned, that I need to use a for loop for this problem and so I cannot just find the answer at the end of 18 years. I have to have a matrix of 216 values so that I will be able to plot.

댓글을 달려면 로그인하십시오.


Alex
Alex 2012년 11월 13일
You could try a 1-pole IIR filter.

Oleg Komarov
Oleg Komarov 2012년 11월 13일
n = 18*12; % number of periods
i = 0.005; % per period interest
I = 1000; % initial investment
CF = 100; % recurrent cash flow
s = (1 + i).^(0:n); % compounding factor
capital = I*s + [0 cumsum(CF*s(1:end-1))];
plot(0:n,capital)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by