counting number of times commadn executed in while loop
2 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am working on a problem where I need to know how many years it would take for interest being compounded annually to be twice the intial investment. for Initial investment of $1000, here is my code at a rate of 5%
FinalValue = Investment;
while FinalValue < 2*Investment;
FinalValue = FinalValue + Rate*FinalValue;
end
but I dont know how to assign numYears to the number of years (number of executions) of this loop.
0 件のコメント
回答 (1 件)
Roger Stafford
2014 年 6 月 21 日
FinalValue = Investment;
Count = 0;
while FinalValue < 2*Investment;
FinalValue = FinalValue + Rate*FinalValue;
Count = Count+1;
end
2 件のコメント
Roger Stafford
2014 年 6 月 21 日
'Count' is a count of the number of times you have executed the line
FinalValue = FinalValue + Rate*FinalValue;
which is presumably the number of years you have accumulated interest in doubling your initial investment. It increases by one for each trip through the while-loop.
参考
カテゴリ
Help Center および File Exchange で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!