Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用近似解和对分搜索_Python_Python 3.x_Algorithm_Bisection - Fatal编程技术网

Python 使用近似解和对分搜索

Python 使用近似解和对分搜索,python,python-3.x,algorithm,bisection,Python,Python 3.x,Algorithm,Bisection,我想澄清的是,这个问题来自于EDX python课程的Comp Sci简介(免费在线课程),我在网上看到了很多答案。我想弄清楚为什么这个特定的代码会进入无限循环,而不是完全更改代码的方法(除非它是根本错误的…) 主要是,这一失败的原因: while abs(balance - fixedP*12) >= epsilon: 该计划的目标是找到最小的每月付款到美分,这样我们可以在一年内付清余额 下面的代码到达了这个数量(如果你在一个while循环中插入一个打印(FixEDP)语句,但是它会

我想澄清的是,这个问题来自于EDX python课程的Comp Sci简介(免费在线课程),我在网上看到了很多答案。我想弄清楚为什么这个特定的代码会进入无限循环,而不是完全更改代码的方法(除非它是根本错误的…)

主要是,这一失败的原因:

while abs(balance - fixedP*12)  >= epsilon:
该计划的目标是找到最小的每月付款到美分,这样我们可以在一年内付清余额

下面的代码到达了这个数量(如果你在一个while循环中插入一个打印(FixEDP)语句,但是它会被困在一个无限循环中。我相信我的情况正在衰退,我不知道为什么。当余额变成“29157.09”时,我的(FixEdP*12)检查也变为29157.09,我以为while循环会停止。但是,它一直被卡住

balance = 320000
annualInterestRate = .2 
#above is given, below is my code

epsilon = 0.01  #precision check
balanceD = balance  #copy of balance
month = 0  #counter
monthlyRate = annualInterestRate/12  #given formula for problem

#bisection search parameters using given forumulas
low = balance / 12
high = (balance * (1+monthlyRate)**12)/12

fixedP = (low + high) / 2.0  #midpoint


while abs(balance - fixedP*12)  >= epsilon:
  unpaid = balance - fixedP
  newB = unpaid + monthlyRate*unpaid
  balance = newB
  month += 1

  if(month == 12 and balance > 0): #set new lower bound since original guess was too low
    low = fixedP
    fixedP = (high + low)/2
    balance = balanceD
    month = 0
  elif(month == 12 and balance < 0): #set new higher bound since original guess was too high
    high = fixedP
    fixedP = (low + high)/2
    balance = balanceD
    month = 0

print('Lowest Payment: ', round(fixedP, 2)) 
余额=320000
年利率=.2
#上面是给出的,下面是我的代码
ε=0.01#精度检查
平衡=平衡#平衡副本
月份=0#计数器
月利率=年利率/12#问题的给定公式
#使用给定公式的对分搜索参数
低=平衡/12
高=(余额*(1+月龄)**12)/12
固定值=(低+高)/2.0#中点
而abs(平衡固定p*12)>=ε:
未付=余额-固定值
新员工=未付+月工资*未付
余额=新的
月份+=1
如果(月==12,余额>0):#设置新的下限,因为原始猜测太低
低=固定值
固定值=(高+低)/2
平衡
月份=0
elif(月=12,余额<0):#由于最初的猜测太高,所以设置了新的上限
高=固定值
固定值=(低+高)/2
平衡
月份=0
打印('最低付款:',圆形(固定付款,2))
上面的代码到达固定的每月金额,但它在金额上陷入无限循环。它从不退出while循环。您能帮我找出while循环条件失败的原因吗


谢谢。

您的逻辑和其他一切都是正确的。但是您的while函数逻辑不知何故不正确,您不应该比较abs(balance-fixedP*12)>=epsilon。它应该是:

while balance >= epsilon:
或:

此外,在后面的比较中,您需要将balance与epsilon进行比较,而不是0

因为余额应该是从剩余月份中扣除的值,所以这里不应该使用12

还有你的代码

balance = 320000
annualInterestRate = .2 
#above is given, below is my code

epsilon = 0.01  #precision check
balanceD = balance  #copy of balance
month = 0  #counter
monthlyRate = annualInterestRate/12  #given formula for problem

#bisection search parameters using given forumulas
low = balance / 12
high = (balance * (1+monthlyRate)**12)/12

fixedP = (low + high) / 2.0  #midpoint


while balance >= epsilon:
    unpaid = balance - fixedP
    newB = unpaid + monthlyRate*unpaid
    balance = newB
    month += 1
    if(month == 12 and balance > epsilon): #set new lower bound since original guess was too low
        low = fixedP
        fixedP = (high + low)/2
        balance = balanceD
        month = 0
    elif(month == 12 and balance < -epsilon): #set new higher bound since original guess was too high
        high = fixedP
        fixedP = (low + high)/2
        balance = balanceD
        month = 0


print('Lowest Payment: ', round(fixedP, 2)) 
余额=320000
年利率=.2
#上面是给出的,下面是我的代码
ε=0.01#精度检查
平衡=平衡#平衡副本
月份=0#计数器
月利率=年利率/12#问题的给定公式
#使用给定公式的对分搜索参数
低=平衡/12
高=(余额*(1+月龄)**12)/12
固定值=(低+高)/2.0#中点
当平衡>=ε时:
未付=余额-固定值
新员工=未付+月工资*未付
余额=新的
月份+=1
如果(月==12,余额>ε):#设置新的下限,因为原始猜测太低
低=固定值
固定值=(高+低)/2
平衡
月份=0
elif(月=12,余额<-epsilon):#设置新的上限,因为最初的猜测太高
高=固定值
固定值=(低+高)/2
平衡
月份=0
打印('最低付款:',圆形(固定付款,2))
或者,当获得所需值时,可以使用break函数退出while循环:

balance = 320000
annualInterestRate = .2 
#above is given, below is my code

epsilon = 0.01  #precision check
balanceD = balance  #copy of balance
month = 0  #counter
monthlyRate = annualInterestRate/12  #given formula for problem

#bisection search parameters using given forumulas
low = balance / 12
high = (balance * (1+monthlyRate)**12)/12

fixedP = (low + high) / 2.0  #midpoint


while True:
    unpaid = balance - fixedP
    newB = unpaid + monthlyRate*unpaid
    balance = newB
    month += 1
    if(month == 12 and balance > epsilon): #set new lower bound since original guess was too low
        low = fixedP
        fixedP = (high + low)/2
        balance = balanceD
        month = 0
    elif(month == 12 and balance < -epsilon): #set new higher bound since original guess was too high
        high = fixedP
        fixedP = (low + high)/2
        balance = balanceD
        month = 0
    else:
        break

print('Lowest Payment: ', round(fixedP, 2)) 
余额=320000
年利率=.2
#上面是给出的,下面是我的代码
ε=0.01#精度检查
平衡=平衡#平衡副本
月份=0#计数器
月利率=年利率/12#问题的给定公式
#使用给定公式的对分搜索参数
低=平衡/12
高=(余额*(1+月龄)**12)/12
固定值=(低+高)/2.0#中点
尽管如此:
未付=余额-固定值
新员工=未付+月工资*未付
余额=新的
月份+=1
如果(月==12,余额>ε):#设置新的下限,因为原始猜测太低
低=固定值
固定值=(高+低)/2
平衡
月份=0
elif(月=12,余额<-epsilon):#设置新的上限,因为最初的猜测太高
高=固定值
固定值=(低+高)/2
平衡
月份=0
其他:
打破
打印('最低付款:',圆形(固定付款,2))

请随时发布您不清楚的内容。谢谢。

我在这里看到的问题与其说是Python问题,不如说是经济学问题

也就是说,为了使代码正常工作,我们首先在while块之前添加以下行:

series_pw_factor = ((1+monthlyRate)**11 - 1) / (monthlyRate*(1+monthlyRate)**11)
然后我们可以修改您的条件,如下所示:

while abs(balanceD - fixedP*series_pw_factor - fixedP)  >= epsilon:
现在来解释经济学是如何与这个问题联系在一起的:

你的while条件的问题是,它没有考虑到货币的时间价值(即利息)。如果你今天以1000美元贷款,并在5个月内每月支付200美元(在每个月底),如果利息不为零,你在5个月结束时仍将欠贷款的钱

虽然问题中没有明确说明,但要得到答案“29157.09”,您必须假设第一笔付款在第一个月初支付,最后一笔付款在第12个月初支付

付款结构如下:

while abs(balanceD - fixedP*series_pw_factor - fixedP)  >= epsilon:
初始余额:320 000美元

t=0时的付款1(第1个月开始):29157.09

t=0时的值为29157.09/(1+月龄)^0=29157.09

t=1时的付款2(第2个月初):29157.09

t=0时的值为29157.09/(1+月龄)^1=28679.10

t=2时的付款3(第3个月初):29157.09

t=0时的值为29157.09/(1+月龄)^2=28208.96

t=3时的付款4(第4个月初):29157.09

t=0时的值为29157
fixedP = balance / (1 + ((1+annualInterestRate/12)**11 - 1) / (annualInterestRate/12*(1+annualInterestRate/12)**11))