How can I fix this Infinite While loop?

11 次查看(过去 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

回答(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