Using Bisection Search on Lowest Payments on Credit Card debt and
Asked Answered
M

2

0

My code:

monthlyInterestRate = annualInterestRate/12.0 
low = balance/12 
high = (balance*(1+monthlyInterestRate)**12)/12 
guess = (low+high)/2 
unpaidBalance = balance 
month = 1

while True:
    unpaidBalance= unpaidBalance-guess 
        while month < 13:
            if unpaidBalance <= -0.1:
                low = guess
                month += 1
            elif unpaidBalance >= 0.1:
                high = guess
                month += 1
            else:
                break
            guess = (low + high)/2 
print "Lowest Payment: " + str(round(guess, 2))

When I test it it gets stuck at the line "while month < 13:"

Why does it do this and how do I fix it?

Menagerie answered 25/2, 2013 at 20:22 Comment(3)
what does it means for you that it gets stuck? ...Competent
It loops at that line.Menagerie
Search 'MITx' on stackoverflow to find other people with similar homework problem :)Senega
C
1

If you break at each loop of inner while, you remains less than 13.

And this goes on and on since you proceed While True and do not update your guess.

I fear you are facing infinite looping there.

Your break statement breaks the closest loop, that is the While month < 13 loop. The next line is not read. guessis not updated. The while True is not broken.

Maybe you wanted to say

while month < 13:
   unpaidBalance= unpaidBalance-guess 
   if unpaidBalance <= -0.1:
         low = guess
   elif unpaidBalance >= 0.1:
         high = guess
   month += 1
   guess = (low + high)/2 
Competent answered 25/2, 2013 at 20:30 Comment(7)
I just copied over the code (^^) to try it out, but that is still looping at the same line...???Menagerie
because you always fall in your else case. Month is not incremented, you remain below 13. Then, you should rework algorithm.Competent
I'm still so confused! What am I supposed to do exactly?Menagerie
try with my last update, then month is ALWAYS incremented and it should work. BUT it all depends of what you want in the end.Competent
Here's a test case: annualInterestRate = 0.18 and balance = 999999, result should be 90325.07.Menagerie
is that yours https://mcmap.net/q/1329775/-bisection-search-duplicate?rq=1 ?Competent
No, it's not mine but it's the same problem. Thanks for all your help though! ~{^U^}~Menagerie
S
1

here you are

No is the best solution, but it works

monthlyPaymentRate = (balance*annualInterestRate/12)/((1-(1+annualInterestRate/12)**-12))

interest = monthlyPaymentRate * (annualInterestRate/12)

#print (monthlyPaymentRate)
#print (interest)
monthlyPaymentRate = (monthlyPaymentRate - interest) +1
#print (monthlyPaymentRate)
balanceInit = balance

epsilon = 0.01
low = monthlyPaymentRate
while low*12 - balance > epsilon:
    balances = balanceInit
    for i in range(12):
                 minpay =  monthlyPaymentRate
                 unpaybal = balances - minpay
                 interest = (annualInterestRate /12) * unpaybal
                  smontfinal =  unpaybal + interest
                  balances = smontfinal
                 #print('Remaining balance: ' ,round(balances,2) )
                 if balances <0:
                     low = -1
                     break
    if balances < 0 :
        low = -1
    else:
        monthlyPaymentRate =monthlyPaymentRate + 0.001 

print('Lowest Payment:' ,round(monthlyPaymentRate,2) )
Serve answered 27/9, 2016 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.