Compound yearly interest with loop
조회 수: 26 (최근 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
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
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
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
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
2020년 5월 25일
Then you should open a new Question, and in that Question, you should ask something about MATLAB.
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
2018년 11월 3일
very cool. but what if you contribute a certain amount every year? how would you add that code?
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 Exchange에서 MATLAB Compiler에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!