二等分搜索 [英] Bisection search
问题描述
可能重复:
使用对分搜索来确定
Possible Duplicate:
Using bisection search to determine
我发布了其他主题,但未收到答案,因此,我试图提供一些工作以使其更加清晰.
I have posted other thread but it did not receive answers thus i'm trying to provide some of my work to make more clear.
我需要使用二等分法来确定每月还款额,以便准确地在一年内还清债务.
I need to use bisection method to determine monthly payment in order to pay off debt in one year exactly.
以下是一些代码:
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
However, I receive very way off answer: 298222.173851
我的朋友告诉我正确答案是:29157.09
My friend told me that correct answer is : 29157.09
哪个比我的低很多...我想问题出在四舍五入(我还没有这样做),并且在每次循环后保留余额并在余额超过0时将其重置.我不知道该怎么办尝试此问题,请帮助某人:)
Which is a lot lower than my...I guess the problem is in rounding(which I did not do yet) and preserving the balance after every looping and resetting it if balance is over 0. I cannot figure out how to attempt this problem and please, help someone :)
推荐答案
这是正确的.
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)
这篇关于二等分搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!