First of all, if you are doing MITx exercise and completed the previous test (just to increment 10 in guess) you're a little step to get it. Just need some adjustments on while condition and check about the annual results.
About bisection search I'll try to clarify the concept. You'll always have two extremities, the minimum and the maximum. And will always start guessing by the middle of the extremities.
After the first guess, you'll need to adjust the extremities due the annual results. If after one year paying the minimum for drinks, girls, program books and other things you didn't pay the total balance, you certainty need to increase the minimum. Otherwise If, for example, you payed the total balance in the 10th month, you need to drink more and meet new girls next year!!! just kidding... you need do decrease the minimum. This is the check you need to do after completed one year of hard payments
In the exercise, we have:
- balance and annualInterestRate = given (we don't need to care)
- minimum (lower bound) = Balance / 12
- maximum (upper bound) = (Balance x (1 + Monthly interest rate)**12) / 12.0
The first guess will be (minimum + maximum) /2 I called guessMinimum so:
guessMinimum = (minimum + maximum)/2
So you'll start using the first guess (guessMinimum). After one year you'll check the remain.
If the remain is negative, means that you have paid too much. you need to decrease the monthly payment. Otherside, if after one month the remain is positive (for example, more than your precision (ex. 0.10)) you need to decrease the monthly payment, okay?!
Trying to design the thinking.....
+------------------------------------------------+
| /\ /\ /\ |
| \/------------------\/-------------------\/ |
|MINIMUM guess MAXIMUM|
| Minimum |
+------------------------------------------------+
If after one year, the "remain" is negative (for example). Means that the 'guessMinimum' is to much!!! You'll need... not you, the PROGRAM!! The program need to adjust it, lower the minimum so......
+---------------------------------------------------+
| Got negative 'remain' |
| ++ |
| /\ || /\ /\ |
| \/-------------||---\/-------------------\/ |
| MINIMUM || guess MAXIMUM |
| ++ Minimum-, |
| ', |
| `. |
| `., |
| ', |
| ', |
| `. |
| ` |
| /\ /\ /\ |
| \/------------------\/-------------------\/ |
| MINIMUM guess MAXIMUM |
+---------------------------------------------------+
Sorry guys. I tried to insert an image but as a new member. I couldn't. need at least 10 reputation.... help me!!!! too much work to use characters!!!!
And the CODE need to do this hard work to adjust the minimum until the 'remain' is acceptable (within your precision, or epsilon, or any letter or variable or.. okay. :)
After understanding the concept and drawings.. let's check the CODE.
balance = 999999;
annualInterestRate = 0.18
monthlyInterestRate = annualInterestRate / 12
minimum = balance / 12
maximum = (balance * (1 + monthlyInterestRate)**12) / 12.0
guessMinimum = (minimum + maximum)/2
remain = balance #if you payed nothin, the remain is the balance!!!!
precision = 0.10 #you choose....
while (remain >= precision):
guessMinimum = (minimum + maximum)/2
for i in range (1,13):
newBalance = remain - guessMinimum
monthInterest = annualInterestRate/12*newBalance
remain = newBalance+monthInterest
# after one month, the CODE need to check about the remain
if (remain < 0): #paying too much.... need to decrease the value
maximum = guessMinimum #remember my beautiful draw above!!
remain = balance # reset the remain to start again!!
elif (remain > precision): #paying less .... need to increase the value
minimum = guessMinimum
remain = balance # reset the remain to start again!!
print "Lowest Payment: %.2f" %(guessMinimum)
That's it.