Future Bank Account Balance

I have to create a while loop that will calculate how much money will be in my bank account 18 years from now. I start off with $2000, the interest is 0.4%, and my contribution to the account each month is $150. I have to use a while loop to do this and have a plot to show its increase over time. This is what I have but can't seem to make it work:
oldbalance = 2000;
contribution = 150;
while i <= 216
interest = 0.4/100*oldbalance;
newbalance(i) = oldbalance(i) + interest(i) + contribution(i);
oldbalance = newbalance(i);
end
plot(1:1:216,newbalance(i))
xlabel('time,(months)');
ylabel('amount in the account in dollars');
title('Plot of amount in the account as a function of time');

 Accepted Answer

James Tursa
James Tursa on 30 Apr 2020
Edited: James Tursa on 30 Apr 2020
This line overwrites your old balance vector with a scalar
oldbalance = newbalance(i);
Instead of keeping track of old balance and new balance separately, just have a single variable to keep track of the balance. E.g.,
balance = 2000;
contribution = 150;
i = 1; % you need to initialize i
while i < 216
interest = 0.4/100*balance(i);
balance(i+1) = balance(i) + interest + contribution; % assumes contribution goes in at the end of the period.
i = i + 1; % you need to increment i
end
Also, you might want to double check to see if that interest rate is annual or monthly. If it is annual and you are compounding it monthly, then you would need to divide that rate by 12.
Seems like this problem is more suited to a for-loop than a while-loop.

1 Comment

It's compounded monthly. And yes I figured it was more suited for a for loop as well. I have no idea why my professor is having us use a while loop for this.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!