SinghG
SinghG

Reputation: 11

Bisection Search for Minimum Payment (Python) - answer is too large

Doing a bisection search for determining the minimum monthly payment to pay off a credit card balance in 1 year. My result is always a few hundred too large, and it fails to print in the MITx: 6.00.1x online submission module (Always prints 0.00). Can someone please offer some suggestions?

Code:

balance = 999999
annualInterestRate = 0.18
interest = annualInterestRate / 12.0
lower = balance / 12.0
upper = (balance * (1+ interest)**12) / 12.0
guess = (upper + lower) / 2

remainder = balance
newBalance = 0

epsilon = 0.00000001

while (newBalance > epsilon):
    guess = (upper + lower) / 2
    for i in range (0,12):
        newBalance = remainder - guess
        remainder -= guess
        month_interest = newBalance * interest
        remainder = newBalance + month_interest

        if newBalance < 0:
            upper = guess
            newBalance = balance
        elif newBalance > epsilon:
            lower = guess
            newBalance = balance 
print "Lowest Payment: %.2f" %guess 

Upvotes: 1

Views: 193

Answers (2)

M4rtini
M4rtini

Reputation: 13539

There are numerous problems here. First, as noted in the other answer, the while loop never runs. If it did, you'll never simulated anymore than 1 month of payments with the if\elif inside the loop. And you also never reset the remainder variable when starting with a new guess.

remainder -= guess seems useless when you set it to something else two lines down before using it.

newBalance -= guess and then newBalance *= (1+interest) would be enough for the monthly payment loop

balance = 999999
annualInterestRate = 0.18
interest = annualInterestRate / 12.0
lower = balance / 12.0
upper = (balance * (1+ interest)**12) / 12.0
guess = (upper + lower) / 2

newBalance = balance

epsilon = 0.00000001
while (newBalance > epsilon):
    guess = (upper + lower) / 2
    for i in range (12):
        newBalance -= guess
        newBalance *= (1+interest)

    if newBalance < 0:
        #print("Guess: {0}. {1}. Payed too much, start agian with new guess".format(guess, newBalance))
        upper = guess
        newBalance = balance
    elif abs(newBalance) > epsilon:
        #print("Guess: {0}. {1}. PAyed too little , start again with new guess".format(guess, newBalance))
        lower = guess
        newBalance = balance
print ("Lowest Payment: %.2f" %guess )


#Checking result
for i in range(12):
    balance -= guess
    balance *= (1+interest)
print(balance)

>>Lowest Payment: 90325.03
>>6.380723789334297e-09

Upvotes: 1

Abufari
Abufari

Reputation: 78

The problem is, that you set newBalance = 0. So the while-condition newBalance > epsilon is never true and you jump right into the print statement. Set newBalance to 1 or whatever.

Upvotes: 1

Related Questions