How can I fix this Infinite While loop?
조회 수: 9 (최근 30일)
이전 댓글 표시
I've writtent the code below to calculate interest I know it's not going to calculate it the way that it's written. My question to the community is why this code creates an infinite loop???
I've ran the code in blocks and have noticed that for some reason the value of b becomes infity as the code continues to run past the line where b is recalculated (b = b-p % New Principele value ). Could someone please take a look at this and let me know what I may be doing wrong.
a = 12
b = 13000
x = .2004/12 % Interest
p = 700
count = 0
i = 1
while b >= 0
I = b*x % Interest Payment
p = p-I % Amount taken of principal
b = b-p % New Principele value
k = b/p % Increasing I while b>= is true
if b == 0
break
end
end
댓글 수: 0
답변 (1개)
David Hill
2021년 1월 18일
a = 12;
b = 13000;
x = .2004/a % Interest
payment = 700;%I assume you want a constant payment until the last payment
count=1;
while b > 0
I(count) = round(b*x,2) % Interest Payment
p = payment-I(count) % principal reduction
b(count+1) = b(count)-p % New Principele value
count=count+1;
end
댓글 수: 2
참고 항목
카테고리
Help Center 및 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!