MATLAB: Compound Interest with monthly Contributions, Unable to vectorize.

compoundcontributionfor loopinterest

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.

Best Answer

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]);
Related Question