How can I fix this Infinite While loop?
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
David Hill
on 18 Jan 2021
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!