I'm taking the course 6.00.1x Introduction to Computer Science and Programming. I have been asked to come up with a program that calculates the minimum repayments needed to pay off the credit card balance within a year. For this, I need to use bisection methods.
I have come up with this piece of code (please see below). When I run the code, I get 29591.55 as the answer; however, I'm meant to get 29157.09.
Can anyone help me with where I've gone wrong?
balance = 320000
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate/12.0
lowerRepayment = balance/12
upperRepayment = (balance * (1 + monthlyInterestRate)**12)/12
repayment = (lowerRepayment + upperRepayment)/2
unpaidBalance = balance
while unpaidBalance >= 0:
repayment = round(((lowerRepayment + upperRepayment)/2), 2)
unpaidBalance = balance
numberOfRepayments = 0
while numberOfRepayments < 12 and unpaidBalance >= 0:
numberOfRepayments += 1
monthlyUnpaidBalance = unpaidBalance - repayment
increment = monthlyUnpaidBalance * monthlyInterestRate
unpaidBalance = monthlyUnpaidBalance + increment
if unpaidBalance < -0.01:
lowerRepayment = repayment
elif unpaidBalance > 0.01:
upperRepayment = repayment
print 'Lowest Payment: ' + str(repayment)