评估收敛性



我试图使计算收敛,但没有成功。我正在通过for循环迭代函数,当我得到容差时,我想打破循环。因此,该函数给了我一个数组D[nr, nz],我在for循环中对其进行迭代,每次我想将上一次迭代与新的迭代进行比较,以知道差值是否低于公差。但是,在调用函数Ddif之前和之后,数组之间的区别在于返回一个零向量。我认为我没有正确地传递值,也没有在相同的值之间产生差异,但我不知道为什么。

nr = 8
nz = 10
def calculo(E):
# my calcs
#return an array D[nr,nz]
return
ncolmns3 = (nr * nz)
cont_i = 0
Ddif = []
for a in range(0, 10000, 1):
#before calling the function
DOld2 = D
#turning the array in an array of one row, and (nr * nz) columns
DOld3 = numpy.reshape(DOld2, ncolmns3)
D = calculo(D)
#after calling the function
DNew2 = D
#turning the array in an array of one row, and (nr * nz) columns
DNew3 = numpy.reshape(DNew2, ncolmns3)
# Difference between before and after calling the function
for i in range(0, ncolmns3, 1):
Ddif.append(math.fabs(DOld3[i] - DNew3[i]))
MaxDif = numpy.max(Ddif)
#tolerance
Tol = 0.01
cont_i += 1
if (MaxDif <= Tol) is True:
break
print(cont_i)

当使用DOld2 = D时,您不创建新数组,它是对D的引用。如果D更改,DOld2也会更改。(与DOld3相同(。您最终会计算:DOld2(=D(-DOld3(=D

您应该运行的一个简化示例:

a=[1,2]
b=a
a[1]=7
print("CHANGE A")
print("a = ",a)
print("b = ",b)
b[0]=4
print("CHANGE B")
print("a = ",a)
print("b = ",b)

一个简单的解决方案:

DOld2 = numpy.array(D)
DNew2 = numpy.array(D)

PS:不要忘记在每次迭代开始时重置Ddif

最新更新