def fx(x):
v = float(x*x*x - 5*x - 7)
return v
err = int(input("Enter zeroes in approxiamtion in solution: "))
b = float(input("Enter upper limit for root: "))
a = float(input("Enter lower limit for root: "))
c = 0.0
while fx(c) > (1 * 10 ** -err):
c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
print(a, b, c, sep=', ')
a = b
b = c
# c = 0
print()
print("The root is approximately = ", b)
即使循环给出了正确的值,为什么在执行此代码时仍会出现零除错误?
编辑:我将^运算符更改为**,并删除了c = 0,但现在答案甚至与实际答案并不接近。 ^接线员奇怪地给了我正确的答案。
带有**运算符的输出:
Enter zeroes in approxiamtion in solution: 3
Enter upper limit for root: 2
Enter lower limit for root: 1
The root is approximately = 2.0
带有^运算符的输出:
Enter zeroes in approxiamtion in solution: 3
Enter upper limit for root: 2
Enter lower limit for root: 1
1.0, 2.0, 6.5
Traceback (most recent call last):
2.0, 6.5, 2.1658986175115205
File "C:/Users/anves/PycharmProjects/nt/nt.py", line 46, in <module>
c = b - (((b - a)/(fx(b) - fx(a))) * fx(b))
6.5, 2.1658986175115205, 2.302797651275858
ZeroDivisionError: float division by zero
2.1658986175115205, 2.302797651275858, 2.934211734811194
2.302797651275858, 2.934211734811194, 2.705017710718186
2.934211734811194, 2.705017710718186, 2.7438299655247143
2.705017710718186, 2.7438299655247143, 2.7474171932954787
2.7438299655247143, 2.7474171932954787, 2.7473464241055843
2.7474171932954787, 2.7473464241055843, 2.7473465403033757
2.7473464241055843, 2.7473465403033757, 2.747346540307211
2.7473465403033757, 2.747346540307211, 2.747346540307211
最后一个值是未打印的正确值吗?
答案 0 :(得分:0)
我刚刚运行了您的代码,发现fx(b)
和fx(a)
是相同的值。因此,当您计算出fx(b)
减去fx(a)
时,您会得到0
希望这会有所帮助。
Terminal -----------------------------------
Enter zeroes in approxiamtion in solution: 5
Enter upper limit for root: 5
Enter lower limit for root: 5
93.0
93.0
Traceback (most recent call last):
File "d:/PythonScripts/first_python_project/curso_python.py", line 13, in <module>
c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
ZeroDivisionError: float division by zero
添加更多内容:
当您达到较低的值时,fx(b)
和fx(a)
会变得非常相似。
像这样:
Enter zeroes in approxiamtion in solution: 3
Enter upper limit for root: 5
Enter lower limit for root: 3
93.0
5.0
3.0, 5.0, 2.8863636363636362
2.614751596543952
93.0
5.0, 2.8863636363636362, 2.825218326106125
1.424401534793665
2.614751596543952
2.8863636363636362, 2.825218326106125, 2.7520503761266304
0.08317571970829363
1.424401534793665
2.825218326106125, 2.7520503761266304, 2.747512887487003
0.002935214303143141
0.08317571970829363
2.7520503761266304, 2.747512887487003, 2.747346905212986
6.43830337132556e-06
0.002935214303143141
2.747512887487003, 2.747346905212986, 2.747346540335565
5.002736003234531e-10
6.43830337132556e-06
2.747346905212986, 2.747346540335565, 2.747346540307211
1.7763568394002505e-15
5.002736003234531e-10
2.747346540335565, 2.747346540307211, 2.747346540307211
1.7763568394002505e-15
1.7763568394002505e-15
Traceback (most recent call last):
File "d:/PythonScripts/first_python_project/curso_python.py", line 13, in <module>
c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
ZeroDivisionError: float division by zero
答案 1 :(得分:0)
我看到两个问题。 一种是表达式(1 * 10 ^ -err)。 Python中的运算符 ^ 表示按位XOR,10 ^ -err是负整数。因此,在找到根之后,循环不会结束。 它应该是(10 ** -err)。另外,您必须将abs(f(c))与它进行比较。
第二个问题是c = 0作为根的初始值。它应该在(a,b)间隔之内,而不是零。
答案 2 :(得分:0)
当我添加使用abs()
和**
的建议,然后捕获异常时,我得到如下代码:
def fx(x):
v = float(x*x*x - 5*x - 7)
return v
err = int(input("Enter zeroes in approxiamtion in solution: "))
b = float(input("Enter upper limit for root: "))
a = float(input("Enter lower limit for root: "))
c = 0.0
while abs(fx(c)) > ( 10 ** -err):
try:
c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
except ZeroDivisionError:
break
print(a, b, c, sep=', ')
a = b
b = c
c = 0
print()
print("The root is approximately = ", b)
当我运行它时,我得到了:
Enter zeroes in approxiamtion in solution: 3
Enter upper limit for root: 2
Enter lower limit for root: 1
1.0, 2.0, 6.5
2.0, 6.5, 2.1658986175115205
6.5, 2.1658986175115205, 2.302797651275858
( steps omitted )
2.7473464241055843, 2.7473465403033757, 2.747346540307211
2.7473465403033757, 2.747346540307211, 2.747346540307211
The root is approximately = 2.747346540307211
这会产生正确的答案吗?
从输出的倒数第二行可以看到,最后两个数字相同(b和c),下一次循环a和b将具有这些值,因此fx(b) - fx(a)
将得出{{ 1}}和一个ZeroDivisionError。
答案 3 :(得分:0)
您需要重新设计逻辑,但是我可以告诉您(1 * 10 ** -err)
并没有您认为的那样。 ^
是按位互斥或运算符。对于指数,您需要**
。因此您的代码应为
(1 * 10 ** -err)
我尝试运行它,由于fx(0)为负数,它立即停止运行,该值小于上面括号中生成的非常小的正值。
这是一个工作版本,尽管我不确定代码实际上完成了什么...
def fx(x):
v = float(x*x*x - 5*x - 7)
return v
err = int(input("Enter zeroes in approximation in solution: "))
b = float(input("Enter upper limit for root: "))
a = float(input("Enter lower limit for root: "))
c = 0.0
ctr = 0 # ^^^^^^^^^^^^^^^^^^
while abs(fx(c)) > (1 * 10 ** -err) and fx(b) != fx(a): # ^ is binary xor!
ctr += 1
c = b - ((b - a)/(fx(b) - fx(a))) * fx(b)
print(ctr, a, b, c, sep=', ')
a = b
b = c
c = 0
print()
print("The root is approximately = ", b, ' after ', ctr, ' iterations')
input()
我尝试运行了3次,范围为1到10和1/3/8零。所有三倍的结果以b完全相同。到达那里只是花了不同的迭代次数。
答案 4 :(得分:-1)
出现此错误是因为在代码中的某个时候
让我们说fx(a)=1.0000000000000001
和fy(b)=1.0
而不是fx(a)-fy(b)=0.0000000000000001
,您将获得0.0。
因此零除法错误即将来临