从错误的点开始迭代(List index out of Range)



我正在编写使用循环(目前是for循环)的代码,利用Heron的方法,找到平方根的估计值,同时还显示迭代和相对变化。

到目前为止我写的是:

# Problem 1.
def square_root_for(a, x0, max_iter = 10, tol=1e-14):  

""" (number, integer, number) -> float
Return an estimate of the square root of a number using the Heron's method.

>>> square_root_for(5, 5)
Iteration | Estimate         | Relative Change
-------------------------------------------------
1         | 3.00000000000000 | 0.4000000000000000
2         | 2.33333333333333 | 0.2222222222222222
3         | 2.23809523809524 | 0.0408163265306123
4         | 2.23606889564336 | 0.0009053870529653
5         | 2.23606797749998 | 0.0000004106060359
6         | 2.23606797749979 | 0.0000000000000842
7         | 2.23606797749979 | 0.0000000000000000
2.23606797749979
"""

x = [x0]
x.append(a/x0)

print('Iteration | Estimate         | Relative Change')
print('-------------------------------------------------')


for i in list(range(1,( max_iter + 1),1)):
#x[i+1] = (s1 + x[i]) / 2

change = (abs(x[i] - x[i-1]) / x[i-1])
if change > tol:
x.append(1/2 * (x[i] + (a / x[i])))
print('{}         | {:.14f} | {:.16f}'.format(i, x[i], change))
i = i + 1


# Don't change or delete the 5 lines of code below.
a = 5
max_iter = 100
tol = 1e-15
x_final = square_root_for(a, a, max_iter, tol)
print('Final estimate using square_root_for is {0}'.format(x_final))

它大部分工作,但我的例子显示为:

Iteration | Estimate         | Relative Change
-------------------------------------------------
1         | 1.00000000000000 | 0.8000000000000000
2         | 3.00000000000000 | 2.0000000000000000
3         | 2.33333333333333 | 0.2222222222222222
4         | 2.23809523809524 | 0.0408163265306123
5         | 2.23606889564336 | 0.0009053870529653
6         | 2.23606797749998 | 0.0000004106060359
7         | 2.23606797749979 | 0.0000000000000842
8         | 2.23606797749979 | 0.0000000000000000

这是不正确的。第一行的评估和相对变更不应该在那里,而应该是第二行的评估和相对变更,对应于迭代1。也应该只有7次迭代,但由于这个问题,有8次。

代码返回以下错误:

Traceback (most recent call last):
File "C:UsersagsmiDesktopst114homework6.py", line 61, in <module>
x_final = square_root_for(a, a, max_iter, tol)
File "C:UsersagsmiDesktopst114homework6.py", line 50, in square_root_for
change = (abs(x[i] - x[i-1]) / x[i-1])
IndexError: list index out of range

总的来说,我的问题是,我如何解决这个问题,让我的第一次迭代返回第二次返回的内容,这样就只有7次迭代而不是8次。

谢谢!

将list()从for循环中移除,然后进行测试。

for i in range(1,( max_iter + 1),1):

同时删除for循环中的i = i+1。

并修改if语句:

if change > tol:
x.append(1/2 * (x[i] + (a / x[i])))
print('{}         | {:.14f} | {:.16f}'.format(i, x[i], change))
else:
break

# Problem 1.
def square_root_for(a, x0, max_iter = 10, tol=1e-14):  

""" (number, integer, number) -> float
Return an estimate of the square root of a number using the Heron's method.

>>> square_root_for(5, 5)
Iteration | Estimate         | Relative Change
-------------------------------------------------
1         | 3.00000000000000 | 0.4000000000000000
2         | 2.33333333333333 | 0.2222222222222222
3         | 2.23809523809524 | 0.0408163265306123
4         | 2.23606889564336 | 0.0009053870529653
5         | 2.23606797749998 | 0.0000004106060359
6         | 2.23606797749979 | 0.0000000000000842
7         | 2.23606797749979 | 0.0000000000000000
2.23606797749979
"""

x = [x0]
x.append(a/x0)

print('Iteration | Estimate         | Relative Change')
print('-------------------------------------------------')


for i in range(1,max_iter):
#x[i+1] = (s1 + x[i]) / 2
change = (abs(x[i] - x[i-1]) / x[i-1])
if change > tol:
x.append(1/2 * (x[i] + (a / x[i])))
else:
break
print('{}         | {:.14f} | {:.16f}'.format(i, x[i], change))
return(x[i])
# Don't change or delete the 5 lines of code below.
a = 5
max_iter = 100
tol = 1e-15
x_final = square_root_for(a, a, max_iter, tol)
print('Final estimate using square_root_for is {0}'.format(x_final))

我取出了I = I +1行,简化了AAR提到的范围语句,但是您得到的错误是因为它正在循环并期望找到一个数组值,所以当达到所需的精度时,您需要一个中断来逃离for循环。编辑-您还需要从函数返回值x[i]。

输出
Iteration | Estimate         | Relative Change
-------------------------------------------------
1         | 1.00000000000000 | 0.8000000000000000
2         | 3.00000000000000 | 2.0000000000000000
3         | 2.33333333333333 | 0.2222222222222222
4         | 2.23809523809524 | 0.0408163265306123
5         | 2.23606889564336 | 0.0009053870529653
6         | 2.23606797749998 | 0.0000004106060359
7         | 2.23606797749979 | 0.0000000000000842
Final estimate using square_root_for is 2.23606797749979

相关内容

最新更新