-1

可能的重复:
使用二等分搜索来确定

我已经发布了其他线程,但它没有收到答案,因此我试图提供我的一些工作以更清楚地说明。

我需要使用二分法来确定每月还款额,以便准确地在一年内还清债务。

这是一些代码:

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 时重置它。我不知道如何尝试这个问题请帮助某人:)

4

2 回答 2

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)
于 2012-10-13T22:27:28.130 回答
3

这就是你需要的

while abs(x) > epsilon:
    x = balance
    for month in range(0, 12):
        x = (x - ans) * (1+monthly_interest)
于 2012-10-11T15:25:17.510 回答