trouble understanding for loops
1 回表示 (過去 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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!