Compound yearly interest with loop

21 visualizzazioni (ultimi 30 giorni)
Ryan  Ellwanger
Ryan Ellwanger il 18 Nov 2016
Commentato: Walter Roberson il 25 Mag 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 Commenti
Eyob Ayalew
Eyob Ayalew il 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 il 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.

Accedi per commentare.

Risposte (2)

sai teja
sai teja il 15 Mag 2020
Modificato: Image Analyst il 15 Mag 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 Commenti
Amber Fraley
Amber Fraley il 25 Mag 2020
Modificato: Amber Fraley il 25 Mag 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.
Walter Roberson
Walter Roberson il 25 Mag 2020
Then you should open a new Question, and in that Question, you should ask something about MATLAB.

Accedi per commentare.


Image Analyst
Image Analyst il 18 Nov 2016
Modificato: Image Analyst il 15 Mag 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 Commenti
Eyob Ayalew
Eyob Ayalew il 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 il 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')

Accedi per commentare.

Categorie

Scopri di più su MATLAB Compiler in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by