My homework problem: Find the smallest monthly payment required pay off a given loan principal within a year. One-twelfth of the original balance is a good lower bound; a good upper bound is one-twelfth of the balance, after having its interest compounded monthly for an entire year.
In short:
Monthly interest rate = (Annual interest rate) / 12.0
Monthly payment lower bound = Balance / 12
Monthly payment upper bound = (Balance * (1 + Monthly interest rate)**12) / 12.0
I have to write a bisection search to find that smallest monthly payment to the cent.
Every time I run this code I get the lowest payment to be a value a couple hundred off from the correct solution.
balance = 414866
annualInterestRate = 0.22
month = 0
monthlyinterest = (annualInterestRate) / 12.0
updatedbalance = balance
monlowbound = balance / 12
monupbound = (balance * (1 + monthlyinterest)**12) / 12.0
mid = (monlowbound + monupbound) /2
minpay = 0
while balance > 0 and month <= 12:
balance = updatedbalance
updatedbalance = ((monthlyinterest * balance) + balance) - minpay
month += 1
if updatedbalance > 0:
minpay = (mid + monupbound)/2
mid = monlowbound
if updatedbalance < 0:
minpay = (monlowbound + mid)/2
monupbound = mid
else:
print("Lowest payment:" + " " + str(round(minpay,2)))
This is what I get as the output:
Lowest payment: 40888.41
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
Lowest payment: 38783.0
balance*(1+annualInterestRate) == balance*(1+monthlyInterestRate)^12to calculate monthly interest rate.