关于Python二分法求值的一个问题!求哥哥们快来帮忙!小白比较弱。。。。。

#下面的代码是二分法求信用卡每月最低还款的题,这是正确代码
def pay(balance,annualInterestRate):
    if balance<=0:
        return None
    low=balance/12.0
    high=(balance*(1+annualInterestRate/12.0)**12)/12.0
    while True:
        b=balance
        pay2=(low+high)/2
        for i in range(12):
            b=(b-pay2)*(1+annualInterestRate/12.0)
        if abs(b)<=0.01:
            return pay2
        elif b>0.01:
            low=pay2
        else:
            high=pay2
print pay(320000,0.2)

#这是错误代码!请问这下面应该怎么改,应该是错在while的代码块!求问用while写的话怎么改??
def pay(balance,annualInterestRate):
    if balance<=0:
        return None
    b=balance
    low=b/12.0
    high=(b*(1+annualInterestRate/12.0)**12)/12.0
    pay2=(low+high)/2
    while b>0.01:
        if pay2>b*(1+annualInterestRate/12):
            high=pay2
        else:
            low=pay2
        pay2=(low+high)/2.0
        b=(b-pay2)*(1+annualInterestRate/12.0)
    return pay2
print pay(320000,0.2)

还想弱弱的问一下?这个能不能通过牛顿拉夫逊算法计算?

asdhjhg
浏览 1233回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python