Compound yearly interest with loop

55 views (last 30 days)
Ryan  Ellwanger
Ryan Ellwanger on 18 Nov 2016
Commented: Walter Roberson on 25 May 2020
I'm trying to compute compound interest with loops. I'm currently using a while loop, but I don't know if that's the easiest solution. Right now, the code is producing the first year, but I can't get it to repeat more than that.
Here's the problem: Imagine that you went to the bank and deposited $15,000 in an account that earns 7% interest every year, with each year’s interest being deposited back into the account. Write a MATLAB program that computes the number of years it would take to accumulate $400,000.
This is the code I have so far
%compute the interest of amount in bank
%initilize variable called 'prod' to 15000
initial=15000
final=0
interest=.07
while initial<40000;
final=(initial*interest)+initial
end
  2 Comments
Eyob Ayalew
Eyob Ayalew on 3 Nov 2018
Good start I am trying to do the same project. In my case, I am trying to account for other factors like federal and state tax applied to only the interest accrued. now I wonder how we can make it build a chart with multiple columns listing the result maybe as a script.
Image Analyst
Image Analyst on 3 Nov 2018
Eyob, you can use App designer or GUIDE to have a "uitable", which is like a spreadsheet/chart/table kind of display. Then put your data into that.

Sign in to comment.

Answers (2)

sai teja
sai teja on 15 May 2020
Edited: Image Analyst on 15 May 2020
We borrowed $1000 at a 10% annual interest rate. If we did not make a payment for two years, and assuming there is no penalty for non-payment, how much do we owe now? Assign the result to a variable called debt.
  3 Comments
Amber Fraley
Amber Fraley on 25 May 2020
Edited: Amber Fraley on 25 May 2020
This is a MATLAB question, I’m on the same question now. We are taking a course through Coursera. It is not related to Ryan’s question.

Sign in to comment.


Image Analyst
Image Analyst on 18 Nov 2016
Edited: Image Analyst on 15 May 2020
Try this:
% Compute the interest of amount in bank
clear all;
close all;
fontSize = 20
principal = 15000
interest=.07
final(1) = principal;
loopCounter = 1;
while final(end) < 40000
loopCounter = loopCounter + 1;
final(loopCounter) = final(loopCounter - 1) * (1 + interest);
numYears = loopCounter - 1;
fprintf('At the end of year #%d, the balance is $%.2f\n', numYears, final(loopCounter));
end
plot(1:loopCounter, final, 'bo-', 'LineWidth', 2);
grid on;
title('Compounded Interest', 'FontSize', fontSize);
xlabel('Year', 'FontSize', fontSize);
ylabel('Balance', 'FontSize', fontSize);
ylim([0, final(end)]);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
  2 Comments
Eyob Ayalew
Eyob Ayalew on 3 Nov 2018
very cool. but what if you contribute a certain amount every year? how would you add that code?
Image Analyst
Image Analyst on 3 Nov 2018
Simply add it in inside the loop:
% Compute the interest of amount in bank
clear all;
close all;
fontSize = 20
principal = 15000
interest=.07
% Specify how much to add at the end of every year.
% We don't add it at the beginning of the year because that would then just be part of the principal.
amountToAddAtEndOfYear = 400;
final(1) = principal;
loopCounter = 1;
while final(end) < 40000
loopCounter = loopCounter + 1;
final(loopCounter) = final(loopCounter - 1) * (1 + interest) + amountToAddAtEndOfYear;
numYears = loopCounter - 1;
fprintf('At the end of year #%d, the balance is $%.2f\n', numYears, final(loopCounter));
end
plot(1:loopCounter, final, 'bo-', 'LineWidth', 2);
grid on;
title('Compounded Interest', 'FontSize', fontSize);
xlabel('Year', 'FontSize', fontSize);
ylabel('Balance', 'FontSize', fontSize);
ylim([0, final(end)]);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Sign in to comment.

Categories

Find more on Startup and Shutdown 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!