while loop statement errors
이전 댓글 표시
disp('I can tell how long you''ll have to pay for your house.')
disp([' '])
pv=input('What was your present mortgage value? ');
rate=input('What is the yearly interest rate on your home? ');
pmt=input('How much are your mounthly payments? ');
fv=input('What is your future disired mortgage value? Hopefully its 0. ');
m = (rate/100)/12;
while nper>0
nper = pv*(1+m)-pmt;
current_balance = 1:nper;
loop = 1 : nper;
current_balance(loop) = nper;
fprintf('It should take %d periods (out of %d) to get your payments to %.2f\n', ...
loop, fv, current_balance(loop));
end
댓글 수: 20
Geoff Hayes
2020년 4월 9일
Todd - if the error is
Undefined function or variable "nper".
then that is telling you that you need to define this variable before you try to use it. In your case, the while loop condition is referencing this but you haven't created it. What does this value represent? I see that you assign it a value on each iteration of the loop but I don't think it is a different value. You may want to review your equations to ensure they are correct, and assign meaningful variable names to these variables to help you understand their purpose.
Todd Wyzkiewicz
2020년 4월 9일
the cyclist
2020년 4월 9일
편집: the cyclist
2020년 4월 9일
What's the complete error message?
Is the first time you define nper inside that while loop? Then, MATLAB is going to give an error when it hits that while statement the first time, because it is not defined yet.
Set it to something less than zero, before the loop, so that the loop is entered.
Todd Wyzkiewicz
2020년 4월 9일
편집: Geoff Hayes
2020년 4월 9일
Geoff Hayes
2020년 4월 9일
Todd - but you still haven't defined nper. Your code is using it before you define it
current_balance = 1:nper;
So how should this be initialized? Should it be initialized to a large value like 25*12 (assuming a <ouch> 25 year mortgage?). Or should it be assigned to zero since this seems like something that you want to solve for.
nper = 0;
current_balance = pv;
while current_balance > fv
% do some calculation to pay off one month's worth of the balance?
nper = nper + 1;
end
You probably don't need a while loop to do this, I've just left it there since it may be a requirement of your homework.
Walter Roberson
2020년 4월 9일
You are not defining nper before you use it in
current_balance = 1:nper;
Todd Wyzkiewicz
2020년 4월 9일
Walter Roberson
2020년 4월 9일
What is the complete error message?
the cyclist
2020년 4월 9일
Can you repost your code, with the corrections you made, so we can see what you are doing?
Also, again, I don't think that is the complete error message. You are posting the part that says where the error occurs; the actually error is displayed a line or two above that in your display.
Todd Wyzkiewicz
2020년 4월 9일
Todd Wyzkiewicz
2020년 4월 9일
Rik
2020년 4월 9일
That is not the complete error. Use clc to clear your command window, then run the code, then copy all the red text.
Walter Roberson
2020년 4월 9일
Dealing only with your most immediate problem:
clear all
clc
disp('I can tell how long you''ll have to pay for your house.')
disp([' '])
pv=input('What was your present mortgage value? ');
rate=input('What is the yearly interest rate on your home? ');
pmt=input('How much are your mounthly payments? ');
fv=input('What is your future disired mortgage value? Hopefully its 0. ');
m = (rate/100)/12;
% fv = (pv*m)-pmt;
% current_balance = 1:nper;
nper = 0;
current_balance = pv;
loop = 0;
while current_balance > fv
% do some calculation to pay off one month's worth of the balance?
nper = pv*(1+m)-pmt;
loop = loop + 1;
current_balance(loop) = nper;
nper = nper + 1;
fprintf('The current balance after %d periods (out of %d) is %.2f\n', ...
loop, nper, current_balance(loop));
end
This does not attempt to fix the other logic problems in your code. This is just a minimum change to get you past the problem of loop not being defined. It does not repair the serious problems in your logic.
Todd Wyzkiewicz
2020년 4월 9일
Walter Roberson
2020년 4월 9일
nper = nper + 1;
What is nper ? That line of code would lead someone to suspect that nper is acting as a counter of the number of periods processed so far. But just above that you have
nper = pv*(1+m)-pmt;
which looks sort of line a balance that one would owe after the very first pay period. And you have
current_balance(loop) = nper;
so it looks even more like you intend for it to be a balance. But why would you add 1 to the balance each cycle? If there is a montly service charge, then make it into a variable instead of hard-coding it, so that readers can understand:
nper = nper + service_charge;
but then you have
fprintf('The current balance after %d periods (out of %d) is %.2f\n', ...
loop, nper, current_balance(loop));
which suggests that nper is to be the total number of periods expected for the repayment, which is something you would have to calculate in advance, somehow.
Todd Wyzkiewicz
2020년 4월 10일
Walter Roberson
2020년 4월 10일
pv = pv*(1+m)-pmt;
Okay, so pv is a balance.
fprintf(['The current balance after %d periods (out of %f) that should be around %.2f\n'], ...
loop, pv, current_balance(loop));
and there you output it as the second output, in the (out of %f) field. Then your third output, current_balance(loop) is going to be the same as pv since current_balance(loop) was done.
(out of 151)
How do you know ahead of time that 151 is the number needed?
Perhaps you should be calculating everything before displaying anything, so that you can examine your loop counter afterwards to find out how many periods you need. Once you have that, you can go ahead to display everything.
Todd Wyzkiewicz
2020년 4월 10일
Walter Roberson
2020년 4월 10일
I repeat my recommendation to run the calculation first to get all of the balances and then display the output once you know how many there are.
(By the way, balance should never go negative. It is acceptable to pay to below the desired future mortage value, but not below 0.)
Todd Wyzkiewicz
2020년 4월 10일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!