I'm having some trouble with Problem Set 1, Part 3. The first two parts were easy, but I'm a little confused on how to implement bisection searching. I understand the concept well, but not sure what conditional to use to know when I've found the minimum monthly payment (mmp). I have looked at a few different examples and tested them, but I am getting incorrect answers. I thought perhaps the problem was that the floats were not limited to two decimal places, but adding round() to the upper and lower bounds seems to result in an infinite loop. Using an example from http://stackoverflow.com/questions/12820003/bisection-search?rq=1
This is my output: Enter the outstanding balance on your credit card: 320000 Enter the annual interest rate as a decimal: .2 RESULT Monthly payment to pay off debt in 1 year: $26666.6673641 Number of months needed: 11.9999996862
I used: if balance <= 5 and balance > 0: print "**if" print "Your minimum payment is:", guess print balance break elif balance > 0: print "**elif, balance is to high, guess being tested was to low" print "current lowerGuess", lowerGuess print "current guess", guess lowerGuess = guess print "new lowerGuess", lowerGuess guess = (higherGuess + lowerGuess)/2 print "new guess", guess else: print "**else, balance was to low, guess being tested was to high" print "current higherGuess", higherGuess print "current guess", guess higherGuess = guess print "new higherGuess", higherGuess guess = (higherGuess + lowerGuess)/2.0 print "new guess", guess and it seems to be working. the print statements are just in there to show me what's happening. it basically says that getting the balance down to $5 or less is good enough.
is it in Python? @kpennington89
Yes. I am getting closer. I did realize an error I didn't notice before: I was using the wrong equation to calculate remaining balance. The assignment is to find the minimum monthly payment to the nearest cent, so I would prefer to do that than to the nearest $5. Here is my code at the moment: while mmp*12 - bal >= 0.01: remain = bal for month in range(0,12): remain = round(remain * (1+monthly) - mmp,2) #print "\nmonth: " + str(month+1) #print "remaining balance: " + str(remain) if remain < -0.2: #guess too high upper = mmp mmp = round((lower+upper)/2,2) print "\nremaining balance: " + str(remain) print "mmp = " + str(mmp) print "lower: " + str(lower) print "upper: " + str(upper) elif remain > 0.01: #guess too low lower = mmp mmp = round((lower+upper)/2.0,2) print "\nremaining balance: " + str(remain) print "mmp = " + str(mmp) print "lower: " + str(lower) print "upper: " + str(upper) else: break print "\nRESULT\nMonthly payment to pay off debt in 1 year: $" + str(mmp) print "Number of months needed: " + str(round(bal/mmp,2)) Which results in an accurate answer for the first test case: Enter the outstanding balance on your credit card: 320000 Enter the annual interest rate as a decimal: .2 RESULT Monthly payment to pay off debt in 1 year: $29643.05 Number of months needed: 10.8 BUT the result for the second test case is off by $0.01. Caused by using round()? : Enter the outstanding balance on your credit card: 999999 Enter the annual interest rate as a decimal: .18 RESULT Monthly payment to pay off debt in 1 year: $91679.9 Number of months needed: 10.91 I also noticed that in his handout the # of months required is not accurate for the minimum monthly payment in his results. It says 12 for both when really it rounds up to 11 months. I suppose these results are good enough! Thanks all for your help!
Join our real-time social learning platform and learn together with your friends!