Python 而嵌套在for循环中的循环只在for循环的第一次迭代中执行
我对嵌套在for循环中的while循环有问题。它在for循环的第一次迭代中完美地执行,但是for循环在所有其他迭代中跳过while循环 我试图用for循环的每次迭代while循环的执行次数nsteps_list来填充一个列表nsteps_list。预期的答案类似于List=[17,16,16,14,15,13,12,15…],但现在发生的一切都是List=[17,0,0,0,0,0…] 循环代码如下:Python 而嵌套在for循环中的循环只在for循环的第一次迭代中执行,python,loops,for-loop,while-loop,Python,Loops,For Loop,While Loop,我对嵌套在for循环中的while循环有问题。它在for循环的第一次迭代中完美地执行,但是for循环在所有其他迭代中跳过while循环 我试图用for循环的每次迭代while循环的执行次数nsteps_list来填充一个列表nsteps_list。预期的答案类似于List=[17,16,16,14,15,13,12,15…],但现在发生的一切都是List=[17,0,0,0,0,0…] 循环代码如下: # Bisection Method minn = np.arange(.00001, .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)
#二分法
minn=np.arange(.00001.001,0.00005)
nsteps_list=[0.0]*(len(minn))#用nsteps重写每个索引,作为遍历
nsteps=0
对于范围(0,len(minn)-1)内的i:
而math.fabs(fx_2)>minn[i]:
如果fx_2>0:
x_3=x_2
打印“更新:x_3=“+str(x_2)”
elif fx_2<0:
x_1=x_2
打印“更新:x_1=“+str(x_2)”
x_2=0.5*(x_1+x_3)
fx_2=func(x_2)
nsteps+=1
打印nsteps
nsteps_列表[i]=nsteps
nsteps=0
打印“列表:”+str(nsteps\U列表)
从实验中我知道,它可以很好地迭代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()
#/usr/bin/python
将matplotlib.pylab作为plt导入
将numpy作为np导入
输入数学
#抛物线=3x^2-9x+2==>有最小根和2个实根
def func(n):#定义函数以获得抛物线的每个x的f(x)
a=3
b=-9
c=2
fx=a*n*n+b*n+c
外汇收益
#对x中的值调用抛物线函数
x=np.arange(-2.0,4.0,0.2)
y=func(x)
plt.图(1)
平面图(x,y)
plt.绘图(x,x*0)
#为二分法声明变量
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)
如果fx_1>=0:
打印“警告:变量x_1未正确初始化。”
如果fx_3 minn[i]:
如果fx_2>0:
x_3=x_2
打印“更新:x_3=“+str(x_2)”
elif fx_2<0:
x_1=x_2
打印“更新:x_1=“+str(x_2)”
x_2=0.5*(x_1+x_3)
fx_2=func(x_2)
nsteps+=1
打印nsteps
nsteps_列表[i]=nsteps
nsteps=0
打印“列表:”+str(nsteps\U列表)
打印“x_2=“+str(x_2)+”和f(x_2)=“+str(fx_2)+”
plt.图(2)
plt图(np.log10(明尼苏达州),nsteps\U列表)
plt.图(1)
plt.绘图(x_2,fx_2,“mo”)
plt.show()
因此,我需要这个数组根据对应的minn值的日志在图形上绘制。有什么问题吗?我调试了你的程序。
第一次for
循环迭代后,while循环运行17次。
i
从0递增到1,并尝试再次计算while循环
此时,fx_2
的值为-8.63145396579057e-06
和minn[1]
is6.0000000000000002e-05
因此,fx_2
的绝对值小于min[i]
,其中i==1
,因此不允许再次输入内部循环,而是为nsteps
吐出零
因为我不是一个数学天才,我建议你再看看你试图计算的东西。简短回答:因为
math.fabs(fx_2)>minn[I]
在第一次迭代后是不正确的。我认为很多初始化(例如x_3=3
等)必须在for循环中进行,就在while循环之前,因此,在for循环的每次迭代中,起始情况都会重新恢复。但也许我误解了这个项目的概念。如果没有详细的解释,这将完成什么,很难猜测。非常感谢你,这是我错过的。现在工作很好。嗨,非常感谢你的回复。我最终自己弄明白了,这是一个如此明显的细节!我没有同时重置x_1和x_3的值。在for循环中添加它修复了我的错误。感谢您花时间回复