現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
while loop statement errors
1 回表示 (過去 30 日間)
古いコメントを表示
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 日
nper is the varible for the number of months untill it is paid of. Sorry if it s confusing the varibles where taking from an exel document I can us to check my answer with.
This is the error that I get and I'm assuming its because theres not enough there. The statement is true though.
Error in untitledBS (line 19)
while nper>=0
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 日
What I showed before was the entire error message. I did move the nper indruductions out of the loop and it seems that those are my problem.
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;
loop = 1 : nper;
while nper>=0
nper = pv*(1+m)-pmt;
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
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 日
I think that might have solver that issue but now the (fprintf) line isnt display what it need to. I get this error.
Error in untitledBS (line 31)
loop, nper, current_balance(loop));
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 日
This is what I have as of now
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;
while current_balance > fv
% do some calculation to pay off one month's worth of the balance?
nper = pv*(1+m)-pmt;
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
Todd Wyzkiewicz
2020 年 4 月 9 日
It dont care for the "corrent_balance(loop)" that I left in the equation. this is the compleat error in the command window that I have now
Error in untitledBS (line 26)
current_balance(loop) = nper;
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 日
Do you know what might be wrong withthe equation? or is it more of a varible issue
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 日
I did notice that. It was taking care of, but now im having troubles getting the fprintf discplay to work correctly.
m = (rate/100)/12;
current_balance = pv;
loop = 0;
while current_balance > fv
% do some calculation to pay off one month's worth of the balance?
pv = pv*(1+m)-pmt;
loop = loop + 1;
current_balance(loop) = pv;
fprintf(['The current balance after %d periods (out of %f) that should be around %.2f\n'], ...
loop, pv, current_balance(loop));
end
and this is what i get ing the cammand window.
I can tell how long you'll have to pay for your house.
What was your present mortgage amount?
150000
What is the yearly interest rate on your mortgage?
3
How much are your mounthly payments?
1200
What is your future disired mortgage value? Hopefully its 0.
0
The current balance after 147 periods (out of 3658.971730) that should be around 3658.97
The current balance after 148 periods (out of 2468.119159) that should be around 2468.12
The current balance after 149 periods (out of 1274.289457) that should be around 1274.29
The current balance after 150 periods (out of 77.475180) that should be around 77.48
The current balance after 151 periods (out of -1122.331132) that should be around -1122.33
>>
the "(out of ____)" lines should ne reading "(out of 151)" all the way down and i dont know what is actualy attatched to the " %f " or whats supposed to be got get the answer im loong for.
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 日
I did. 151 is the right amout of payments fot that intrest rate and the odd numbers I've chosen. I did get it to work though. thanks for the help.
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 日
Your right the but the last payment will just be less that the 1200 in this instance. I acctually dropped the priced and ketp the payment amount.
回答 (0 件)
参考
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)