Compound yearly interest with loop

51 次查看(过去 30 天)
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.

请先登录,再进行评论。


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 CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by