Home > Net >  elements in my array is undefined, but outside cycle they are good
elements in my array is undefined, but outside cycle they are good

Time:11-30

Why my elements of arrays P0 and P1 is undefined? They are undefined in cycle "while (run === true)". Outside this cycle they equals 0

function Q(x, y) {
    return 10000;
}

function Create2DArray(rows) {
    var mas = [];
    for (var i = 0; i < rows; i  ) {
        mas[i] = [];
        for (var j = 0; j < rows   1; j  ) {
            mas[i][j] = 0;
        }
    }
    return mas
}

let t = 0, tmax = 50, RightX = 250, h = 10, d = 19, K = 0.4, wind = 0;

let N = RightX / h
console.log(N);
C = 5

let alpha = 10 * Math.PI / 180

let U = C * Math.cos(alpha)
let V = C * Math.sin(alpha)
let M1, M2, M3, M4
let D = d * C;

let P0 = Create2DArray(N)
let P1 = Create2DArray(N)
console.log(P1);

let MinVal = h / U

if (MinVal > h / V) {
    MinVal = h / V
}

if (MinVal > ((h * h) / (2 * D))) {
    MinVal = (h * h) / (2 * D)
}

let tau = K * MinVal

let i = 0, j = 0;

t = t   tau
let run = true
while (run === true) {
    for (i = 0; i <= N - 1; i  ) {
        for (j = 0; j <= N - 1; j  ) {
            if (U > 0) {
                M1 = P0[i][j] * U * h * tau
                M2 = P0[i][j - 1] * U * h * tau
            }

            else if (U < 0) {
                M1 = P0[i   1][j] * U * h * tau
                M2 = P0[i][j] * U * h * tau
            }

            else if (V > 0) {
                M3 = P0[i][j] * V * h * tau
                M4 = P0[i][j - 1] * V * h * tau
            }

            else if (V < 0) {
                M3 = P0[i][j   1] * V * h * tau;
                M4 = P0[i][j] * V * h * tau;
            }
            else {
                console.log("test");
            }
            S1 = P0[i][j]
            S2 = (tau / (h * h)) * D * (P0[i   1][j] - 4 * P0[i][j]   P0[i][j]   P0[i][j   1]   P0[i][j])
            S3 = 1 / (h * h) * (M1 - M2   M3 - M4)
            S4 = Q(i, j) * tau
            P1[i, j] = S1 - S3   S2   S4
        }

        for (let z = 0; z <= N   1; z  ) {
            P1[0, z] = 0;
            P1[N   1, z] = 0;
        }

        for (let z = 0; z <= N   1; z  ) {
            P1[z, 0] = 0;
            P1[z, N   1] = 0;
        }

        for (let z = 0; z <= N   1; z  ) {
            for (let r = 0; r <= N   1; r  ) {
                P0[z, r] = P1[z, r]
            }
        }
    }
    t = tau   t
    if (t > tmax) {
        run = false
    }
}

I tried to change the methods for creating two-dimensional arrays, I also tried to move the loop and everything worked, however, in such a construction, the elements of the array P0 and P1 are equal to undefined

CodePudding user response:

In javascript the "," operator return the right value. So when you write PO[z, r], it's like you write PO[r]. I guess you want to write PO[z][r] = P1[z][r] instead.

  • Related