Python Bisection search overshoots target -
i have create code find exact payment, cent, needed pay off loan in 12 months using bisection. code created works overshoots it's target. loan payed off within 12 months after making 12 payments final balance should around 0. way bigger negative number.
the code using is:
startbalance = float(raw_input('credit balance in $: ')) annualrate = float(raw_input('annual interest rate in decimals: ')) monthlyrate = annualrate / 12.0 minpaymentlow = startbalance / 12.0 minpaymenthigh = (startbalance*(1+monthlyrate)**12.0)/12.0 cent = 0.01 payment = (minpaymenthigh+minpaymentlow)/2.0 while (payment*12-startbalance) >= cent: month in range(0, 12): balance = (startbalance-payment)/10*(1+monthlyrate) if balance < 0: minpaymentlow = payment elif balance > 0: minpaymenthigh = payment payment = (minpaymenthigh + minpaymentlow)/ 2.0 print 'result' print 'number of months needed: 12' print 'montly pay: $', round(balance,2)
it looks these lines:
for month in range(0, 12): balance = (startbalance-payment)/10*(1+monthlyrate)
should be:
balance = startbalance month in range(0, 12): balance = (balance-payment) * (1 + monthlyrate)
or similar, depending on how want calculate interest, , whether consider payments occurring @ start or end of month.
Comments
Post a Comment