-1

I have posted other thread but it did not receive answers thus i'm trying to provide some of my work to make more clear.

I need to use bisection method to determine monthly payment in order to pay off debt in one year exactly.

Here's some code:

originalBalance = 320000
annualInterestRate = 0.2
monthly_interest = annualInterestRate / 12
low = originalBalance/12
high = (originalBalance*(1 + monthly_interest)**12)/12
epsilon = 0.01
min_payment = (high + low)/2.0

while min_payment*12 - originalBalance >= epsilon:
    for month in range(0, 12):
        balance = (originalBalance - min_payment) * (1+monthly_interest)

    if balance < 0:
        low = min_payment
    elif balance > 0:
        high = min_payment
        min_payment = (high + low)/2.0
print "lowest payment: " + str(balance)

However, I receive very way off answer: 298222.173851

My friend told me that correct answer is : 29157.09

Which is a lot lower than my...I guess the problem is in rounding(which I did not do yet) and preserving the balance after every looping and resetting it if balance is over 0. I cannot figure out how to attempt this problem and please, help someone :)

Contempt
  • 189
  • 2
  • 5
  • 14
  • Are you allowed to use the [bisect](http://docs.python.org/library/bisect.html) package? – David Robinson Oct 09 '12 at 16:50
  • Nope, I have do this hard way...my code should be written using new lower and upper bounds and determine payment faster then previous code i have posted above. It must work fast with big numbers too...my code does not do that. It's from MIT website and I'm self-studying so... – Contempt Oct 09 '12 at 16:52

2 Answers2

0

This is the key:

while abs(x) > epsilon:
    x = balance
    for month in range(0, 12):
        x = (x - ans) * (1+monthly_interest)
rastagram
  • 31
  • 1
0

remember to round to 2 decimal places otherwise MITx will not accept the answer

rastagram
  • 31
  • 1
  • You might want to include this into your other answer. There is no need for two separate answers. – Bart Nov 19 '12 at 12:00
  • Hello. Now after having completed MITx and made an effort to do everything myself, I cannot believe how easy bisection search is :D – Contempt Feb 26 '13 at 11:11