Value assignment isn't working as expected

56 views Asked by At

The assignment isn't working as I want. I'm trying to get the convergence of my calculations, so I'm iterating the function through the for loop, and I want break the loop when I get the tolerance. The function gives me an array D[nr, nz], I iterate him in the for loop, and each time I want to compare the last iteration with the new iteration, to know if that difference is lower than the tolerance. But the difference between the arrays, before and after calling the function Ddif, is returning a vector of zeros.
I have seen other similar questions, but I still didn't find why isn't working.

import numpy
import math

nr = 4 #number of rows of D
nz = 4 #number of columns of D
D = numpy.array([[50,52,54,56],[60,62,64,66],[70,72,74,76],[80,82,84,86]])
def calculo(E):
    for i in range(0, nr, 1):
        for j in range(0, nz, 1):
            E[i, j] = E[i, j] * 0.9
    print(E)
    return E

#convergence
ncolmns3 = (nr * nz)
cont_i = 0
for a in range(0, 10000, 1):
    Ddif = []
    #before calling the function
    DOld2 = D.copy()
    #turning the array in an array of one row, and (nr * nz) columns
    DOld3 = numpy.reshape(DOld2, ncolmns3)
    DOld4 = DOld3.copy()

    D = calculo(D)

    #after calling the function
    DNew2 = D.copy()
    #turning the array in an array of one row, and (nr * nz) columns
    DNew3 = numpy.reshape(DNew2, ncolmns3)
    DNew4 = DNew3.copy()

    # 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.1
    cont_i += 1
    if (MaxDif <= Tol) is True:
        break

print(cont_i)
print(Ddif)
1

There are 1 answers

1
André Emiliano On

DOld2 = D.copy() D isn't defined and calculo(E) does not return anything