我已发布其他帖子,但没有收到答案,因此我试图提供一些工作以使其更清晰。
我需要用二分法确定每月付款,以便在一年内偿还债务。
这里有一些代码:
originalBalance = 320000
annualInterestRate = 0.2
monthly_interest = annualInterestRate / 12
low = originalBalance/12
high = (originalBalance*(1 + monthly_interest)**12)/12
epsilon = 0.01
min_payment = (high + low)/2.0
while min_payment*12 - originalBalance >= epsilon:
for month in range(0, 12):
balance = (originalBalance - min_payment) * (1+monthly_interest)
if balance < 0:
low = min_payment
elif balance > 0:
high = min_payment
min_payment = (high + low)/2.0
print "lowest payment: " + str(balance)
然而,我收到的答案很简单:298222.173851
我的朋友告诉我,正确答案是:29157.09
这比我的要低很多......我想问题是四舍五入(我还没有做)并在每次循环后保持平衡,如果余额超过0则重置它。我无法弄清楚如何尝试这个问题,请帮助别人:)
答案 0 :(得分:4)
这是正确的。
originalBalance = 320000
annualInterestRate = 0.2
monthly_interest = annualInterestRate / 12
low = originalBalance/12
high = (originalBalance*(1 + monthly_interest)**12)/12
epsilon = 0.01
min_payment = (high + low)/2.0
while min_payment*12 - originalBalance >= epsilon:
for month in range(0, 12):
balance = (originalBalance - min_payment)/10 * (1+monthly_interest)
if balance < 0:
low = min_payment
elif balance > 0:
high = min_payment
min_payment = (high + low)/2.0
print "lowest payment: " + str(balance)
答案 1 :(得分:3)
这就是你需要的
while abs(x) > epsilon:
x = balance
for month in range(0, 12):
x = (x - ans) * (1+monthly_interest)