How can I fix this Infinite While loop?

9 visualizzazioni (ultimi 30 giorni)
Daniel Casas
Daniel Casas il 18 Gen 2021
Modificato: per isakson il 20 Gen 2021
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

Risposte (1)

David Hill
David Hill il 18 Gen 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 Commenti
Daniel Casas
Daniel Casas il 20 Gen 2021
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 il 20 Gen 2021
Modificato: per isakson il 20 Gen 2021
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

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by