trouble understanding for loops

10 views (last 30 days)
Todd Wyzkiewicz
Todd Wyzkiewicz on 2 Apr 2020
Commented: Todd Wyzkiewicz on 2 Apr 2020
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 Comments
James Tursa
James Tursa on 2 Apr 2020
What have you done so far? What specific problems are you having with your code?
Todd Wyzkiewicz
Todd Wyzkiewicz on 2 Apr 2020
Edited: Image Analyst on 2 Apr 2020
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

Sign in to comment.

Answers (2)

James Tursa
James Tursa on 2 Apr 2020
Edited: James Tursa on 2 Apr 2020
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 on 2 Apr 2020
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 Comment
Todd Wyzkiewicz
Todd Wyzkiewicz on 2 Apr 2020
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.

Sign in to comment.

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!