【问题标题】:While loop nested inside for loop only executes for first iteration of for loop嵌套在 for 循环中的循环仅在 for 循环的第一次迭代时执行
【发布时间】:2015-10-14 23:59:25
【问题描述】:

我遇到了嵌套在 for 循环中的 while 循环的问题。它在 for 循环的第一次迭代中完美执行,但 for 循环随后在所有其他迭代中跳过 while 循环。

我正在尝试为 for 循环的每次迭代填充一个列表 nsteps_list,其中包含 while 循环执行的次数 nsteps。预期的答案将类似于 List = [17, 16, 16, 14, 15, 13, 12, 15...],但所发生的只是 List = [17, 0, 0, 0, 0, 0 ...]

此处循环代码:

# Bisection Method

minn = np.arange(.00001, .001, 0.00005)
nsteps_list = [0.0] * (len(minn))  # Rewrite each index with nsteps as iterating through
nsteps = 0

for i in range(0, len(minn) - 1):

    while math.fabs(fx_2) > minn[i]:

        if fx_2 > 0:
            x_3 = x_2
            print "UPDATE: x_3 = " + str(x_2)

        elif fx_2 < 0:
            x_1 = x_2
            print "UPDATE: x_1 = " + str(x_2)

        x_2 = 0.5 * (x_1 + x_3)
        fx_2 = func(x_2)
        nsteps += 1

    print nsteps
    nsteps_list[i] = nsteps
    nsteps = 0

print "List: " + str(nsteps_list)

我从实验中知道,它可以很好地遍历 for 循环,但它无法返回到 while 循环,因此 nsteps 重置为 0 永远不会改变,我的列表充满了 0。

这是完整的代码,在上下文中:

#!/usr/bin/python

import matplotlib.pylab as plt
import numpy as np
import math


# Parabola = 3x^2-9x+2 ==> Has minimum and 2 real roots

def func(n):  # Defining function to get f(x) for each x for parabola

    a = 3
    b = -9
    c = 2

    fx = a * n * n + b * n + c
    return fx


# Calling parabola function on values in x
x = np.arange(-2.0, 4.0, 0.2)
y = func(x)

plt.figure(1)
plt.plot(x, y)
plt.plot(x, x * 0)

# Declare Variables for bisection method

x_1 = 2.0
x_3 = 3.0
x_2 = 0.5 * (x_1 + x_3)

fx_1 = func(x_1)
fx_2 = func(x_2)
fx_3 = func(x_3)

if fx_1 >= 0:
    print "Warning: Variable x_1 not initialised correctly."
if fx_3 <= 0:
    print "Warning: Variable x_3 not initialised correctly."

# Bisection Method

minn = np.arange(.00001, .001, 0.00005)
nsteps_list = [0.0] * (len(minn))  # Rewrite each index with nsteps as iterating through
nsteps = 0

for i in range(0, len(minn) - 1):

    while math.fabs(fx_2) > minn[i]:

        if fx_2 > 0:
            x_3 = x_2
            print "UPDATE: x_3 = " + str(x_2)

        elif fx_2 < 0:
            x_1 = x_2
            print "UPDATE: x_1 = " + str(x_2)

        x_2 = 0.5 * (x_1 + x_3)
        fx_2 = func(x_2)
        nsteps += 1

    print nsteps
    nsteps_list[i] = nsteps
    nsteps = 0

print "List: " + str(nsteps_list)

print "x_2 = " + str(x_2) + " and f(x_2) = " + str(fx_2) + "."

plt.figure(2)
plt.plot(np.log10(minn), nsteps_list)
plt.figure(1)
plt.plot(x_2, fx_2, "mo")

plt.show()

所以我需要这个数组根据对应的 minn 值的对数绘制图表。有什么想法吗?

【问题讨论】:

  • 简短回答:因为math.fabs(fx_2) &gt; minn[i] 在第一次迭代后不成立。
  • 我认为很多初始化(例如x_3 = 3 等)必须发生在 in for 循环中,就在 while 循环之前,以便在每次迭代for循环开始的情况被重新设置。但也许我误解了程序的概念。如果没有详细解释这将完成什么,很难猜测。
  • 非常感谢,这是我错过的。现在完美运行。

标签: python loops for-loop while-loop


【解决方案1】:

我调试了你的程序。 在第一次for 循环交互之后,while 循环运行了 17 次。 i 从 0 增加到 1,并尝试再次评估 while 循环。

此时fx_2 的值是-8.63145396579057e-06minn[1]6.0000000000000002e-05

所以fx_2 的绝对值小于 min[i] 其中i==1,因此不允许再次进入内部循环,而是为nsteps 吐出零。

由于我不是数学专家,我建议您再看看您要计算的内容。

【讨论】:

  • 您好,非常感谢您的回复。我最终自己弄清楚了,这是一个如此明显的细节!我没有重置我的 x_1 和 x_3 值。在 for 循环中添加它已经修复了我的错误。感谢您抽出宝贵时间回复
最近更新 更多