So currently this code is returning an infinite loop. The assignment is to calculate a fixed monthly payment. My biggest pain is with epsilon and trying to get my newly calculated balance within it's range. The code takes the balance and calculates the theoretical most and least amount we can pay monthly and be at 0 new_balance. MonthlyIR is monthly interest rate
def bisection(balance,annualinterestRate):
monthlyIR = annualinterestRate/12.0
new_balance = balance
monthly_lower = balance/12
monthly_upper = (balance * (1 + monthlyIR)**12)/12.0
epsilon = 0.01
print(monthly_lower,monthly_upper)
while abs(new_balance) >= epsilon:
new_balance = balance
print(monthly_lower,monthly_upper)
payment = (monthly_upper + monthly_lower)/2
for i in range(12):
new_balance -= payment
new_balance *= monthlyIR
if new_balance > 0:
monthly_lower = payment
else:
monthly_upper = payment
return round(payment,2)
So pretty much when I go through the monthly payments, and the new balance is still bigger than epsilon then set either the max or the min = to payments. However when It runs the max or min don't update and I can't figure out why. I would like someone to solve that particular issue and I would like insight on a cleaner way to do this. Whether it's more specialization of functions or a different approach other than iterative.
balanceandannualinterestRatewith the desired result.