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:
- original mortgage value
- yearly interest rate (in % - not decimal)
- their monthly payment
- 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 units—keep everything MONTHLY in your calculations)
2 个评论
James Tursa
2020-4-2
What have you done so far? What specific problems are you having with your code?
回答(2 个)
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.
0 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Financial Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!