counting number of times commadn executed in while loop

조회 수: 3 (최근 30일)
Rick
Rick 2014년 6월 21일
댓글: Roger Stafford 2014년 6월 21일
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.

답변 (1개)

Roger Stafford
Roger Stafford 2014년 6월 21일
FinalValue = Investment;
Count = 0;
while FinalValue < 2*Investment;
FinalValue = FinalValue + Rate*FinalValue;
Count = Count+1;
end
  댓글 수: 2
Rick
Rick 2014년 6월 21일
could you explain how that works? What is Count+1??
Roger Stafford
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 CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by