How can I fix this Infinite While loop?

조회 수: 9 (최근 30일)
Daniel Casas
Daniel Casas 2021년 1월 18일
편집: per isakson 2021년 1월 20일
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

답변 (1개)

David Hill
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
Daniel Casas
Daniel Casas 2021년 1월 20일
I appreciate the help, and effort but I'd like to know if you ran the code and ran into the same problem I had?
Daniel Casas
Daniel Casas 2021년 1월 20일
편집: per isakson 2021년 1월 20일
I got the code running the way that I wanted thank to your code, it helped me realize what I was doing wrong thanks. It's still not finished but here is the working While loop.
clear all
% Interest Payment
a = 12;
b = 13000;
x = .1304/a % Interest
payment = 700;%I assume you want a constant payment until the last payment
count=1;
I = []
c = []
while b > 0
c(1) = b
I(count) = b*x % Interest Payment
p = payment-I(count) % principal reduction
count=count+1;
c(count) = c(count-1)-p % New Principele value
b =c(count)
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by