trouble understanding for loops

조회 수: 5 (최근 30일)
Todd Wyzkiewicz
Todd Wyzkiewicz 2020년 4월 2일
댓글: Todd Wyzkiewicz 2020년 4월 2일
Create a script file that calculates the present balance on a mortgage loan. This program must allow the user to input the following:
  1. original mortgage value
  2. yearly interest rate (in % - not decimal)
  3. their monthly payment
  4. the number of months payments have been made
The program must then output the present balance to the user. Use this math to find mortgage values:
(New) Balance = (Old) Balance * (1+monthly_interest_rate) Monthly_Payment
You must use a FOR loop to calculate a balance for each month and arrive at the current balance.
Make sure you are consistent with unitskeep everything MONTHLY in your calculations)
  댓글 수: 2
James Tursa
James Tursa 2020년 4월 2일
What have you done so far? What specific problems are you having with your code?
Todd Wyzkiewicz
Todd Wyzkiewicz 2020년 4월 2일
편집: Image Analyst 2020년 4월 2일
pv=('What was your original mortgage value? ');
disp(' ')
r=('What is the yearly interest rate percent on your home? ');
disp(' ')
nper=('How many months have you been paying your morgage? ');
disp(' ')
pmt=('How many payments have you made so far? ');
rate=0.1/12;
nper=10*12;
current_bal=1:nper;
for loop=1:nper
pv=pv*(1+rate);
current_bal(loop)=pv;
end
disp(' ')
%the error that comes up is for the "current_ball(loop)=pv' line, but I'm not even sure how to set up for the variables

댓글을 달려면 로그인하십시오.

답변 (2개)

James Tursa
James Tursa 2020년 4월 2일
편집: James Tursa 2020년 4월 2일
The first problem is getting the input from the user.
This assigns a char string to pv:
pv=('What was your original mortgage value? ');
This gets a value from the user and assigns that value to pv:
pv=input('What was your original mortgage value? ');
The line using the input( ) function is what you want.
Second problem is your pv calculations. The instructions say to use this formula:
(New) Balance = (Old) Balance * (1+monthly_interest_rate) Monthly_Payment
But your code looks like this:
pv=pv*(1+rate);
You code doesn't look like it matches the formula, it is missing the monthly payment part.

Image Analyst
Image Analyst 2020년 4월 2일
You forgot to use input() before the parentheses, among other things. Here is improved code:
pv = input('What was your original mortgage value? ');
disp(' ')
r = input('What is the yearly interest rate percent on your home? ');
disp(' ')
numPeriods = input('How many months have you been paying your morgage? ');
disp(' ')
pmt = input('How many payments have you made so far? ');
rate = 0.1/12;
% numPeriods = 10*12;
current_balance = 1:numPeriods;
for loop = 1 : numPeriods
pv = pv*(1+rate);
current_balance(loop) = pv;
fprintf('The current balance after %d periods (out of %d) is %.2f\n', ...
loop, numPeriods, current_balance(loop));
end
There are still some things you need to fix though.
  댓글 수: 1
Todd Wyzkiewicz
Todd Wyzkiewicz 2020년 4월 2일
I was just tring to run it for myself, but adding the 'input()' did help a lot. Thank you for that, and I am looking for as much critiques as I can get.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Financial Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by