trouble understanding for loops

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