Compound yearly interest with loop

21 ビュー (過去 30 日間)
Ryan  Ellwanger
Ryan Ellwanger 2016 年 11 月 18 日
コメント済み: Walter Roberson 2020 年 5 月 25 日
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 件のコメント
Eyob Ayalew
Eyob Ayalew 2018 年 11 月 3 日
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 2018 年 11 月 3 日
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.

サインインしてコメントする。

回答 (2 件)

sai teja
sai teja 2020 年 5 月 15 日
編集済み: Image Analyst 2020 年 5 月 15 日
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 件のコメント
Amber Fraley
Amber Fraley 2020 年 5 月 25 日
編集済み: Amber Fraley 2020 年 5 月 25 日
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 2020 年 5 月 25 日
Then you should open a new Question, and in that Question, you should ask something about MATLAB.

サインインしてコメントする。


Image Analyst
Image Analyst 2016 年 11 月 18 日
編集済み: Image Analyst 2020 年 5 月 15 日
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 件のコメント
Eyob Ayalew
Eyob Ayalew 2018 年 11 月 3 日
very cool. but what if you contribute a certain amount every year? how would you add that code?
Image Analyst
Image Analyst 2018 年 11 月 3 日
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')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by