我的数组中的元素未定义,但在循环外它们很好
elements in my array is undefined, but outside cycle they are good
为什么我的数组 P0 和 P1 的元素未定义?它们在循环“while (运行 === true)”中未定义。在这个循环之外它们等于 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
}
}
我尝试更改创建二维数组的方法,我也尝试移动循环并且一切正常,但是,在这样的构造中,数组 P0 和 P1 的元素等于未定义
在javascript中“,”运算符return正确的值。
所以当你写 PO[z, r] 时,就像你写 PO[r] 一样。
我想你想改为写 PO[z][r] = P1[z][r]。
为什么我的数组 P0 和 P1 的元素未定义?它们在循环“while (运行 === true)”中未定义。在这个循环之外它们等于 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
}
}
我尝试更改创建二维数组的方法,我也尝试移动循环并且一切正常,但是,在这样的构造中,数组 P0 和 P1 的元素等于未定义
在javascript中“,”运算符return正确的值。 所以当你写 PO[z, r] 时,就像你写 PO[r] 一样。 我想你想改为写 PO[z][r] = P1[z][r]。