michaelw90
michaelw90

Reputation: 533

Bisection Search in Python - Find lowest payment over one year

I've been going nuts about this problem for hours, and I've been redoing it over and over! At this point I think I'm actually seeing numbers flying around me.

Anyway, I'm supposed to write a program that finds the correct amount of money to pay each month over one year, to pay off debt on a credit card. So with this program, there's a few conditions that must be met.

Here's my code at the moment, and all I'm getting with this is infinite loops. What am I doing wrong here?

balance = 320000
annualInterestRate = 0.2

monthly_interest = float(annualInterestRate) / 12.0
lower_bound = float(balance / 12)
upper_bound = (balance * (1 + monthly_interest)**12) / 12.0
epsilon = 0.01
ans = float(lower_bound + upper_bound) / 2

while abs((balance * (1 + monthly_interest)**12) / 12.0) >= epsilon:

    ans = float(lower_bound + upper_bound) / 2
    total = float(ans * 12)
    new_balance = 0
    interest = 0

    for month in range(0, 12):
        interest += ans + (1 + monthly_interest)
        new_balance += ans + interest

    if new_balance > balance:
        upper_bound = ans
        print "low" + str(new_balance)
    elif new_balance < balance:
        lower_bound = ans
        print "high" + str(new_balance)
    else:
        print "What's going on here?"

print "Lowest payment: %r" % ans

Upvotes: 0

Views: 518

Answers (1)

Sergio Ayestar&#225;n
Sergio Ayestar&#225;n

Reputation: 5920

I believe there are a couple things wrong here, so first things first, your while is an infinite loop because the condition you are using will never converge to a solution (the variable values never change inside the loop). On top of that the condition (of the while) seems wrong for this kind of problem.

This is what I think you are trying to do, you are trying to find the upper and lower bounds for "the monthly payment" and the convergence condition for that is that the difference between those bounds should be less to a constant epsilon (in other words the error should be less than epsilon).

inside your loop you are calculating the midpoint correctly, this midpoint already is taking into account the interest but are calculating it again. The conditions to change the upper and lower bound are not taking into account the interest so this part of the code is a little messy.

So, modifying those conditions your program actually converges to a solution:

balance = 320000
annualInterestRate = 0.2

monthly_interest = float(annualInterestRate) / 12.0
lower_bound = float(balance / 12)
upper_bound = (balance * (2 + monthly_interest)**12) / 12.0
epsilon = 0.001
ans = float(lower_bound + upper_bound) / 2
total_debt=balance * (1 + annualInterestRate)
print total_debt
while (upper_bound - lower_bound) >= epsilon:

    ans = float(lower_bound + upper_bound) / 2
    total = float(ans * 12)

    if total > total_debt:
        upper_bound = ans
        print "low " + str(total)
    elif total < total_debt:
        lower_bound = ans
        print "high " + str(total)
    else:
        print "Solution found"
    break


print "Lowest payment: %r" % ans

Hope it helps!

Upvotes: 1

Related Questions