Public
Edited
Apr 22, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Boundary condition
function uBoundary(x, y) {
if (x == 0) {
return 4 * y ** 3 * (1 - y);
} else if (y == 0) {
return 0;
} else if (x == 1) {
return 4 * y * (1 - y) ** 3;
} else if (y == 1) {
return 0;
}
}
Insert cell
// Initial condition
u0 = (x, y) => 0
Insert cell
Insert cell
U = {
// Initial grid with values determined solely by u0
// let U0 = [];
// for (let i = 0; i <= N; i++) {
// let y = i / N;
// U0.push(
// d3.range(N + 1).map(function (j) {
// let x = j / N;
// return u0(x, y);
// })
// );
// }

// At the first step, we simply set the value on the boundary
let U1 = [];
for (let i = 0; i <= N; i++) {
let y = i / N;
U1.push(
d3.range(N + 1).map(function (j) {
let x = j / N;
if (i == 0 || i == N || j == 0 || j == N) {
return uBoundary(x, y);
} else {
return u0(x, y);
}
})
);
}

// We now take T more steps defining the value at each interior point
// to be the average of the values of its neighbors.
// Every 50 steps, we push the result to the list of grids.
let U = [U1];
for (let k = 1; k < T + 1; k++) {
let UU = [];
for (let i = 0; i <= N; i++) {
let y = i / N;
UU.push(
d3.range(0, N + 1).map(function (j) {
if (i == 0 || i == N || j == 0 || j == N) {
let x = j / N;
return uBoundary(x, y);
} else {
return (
(U[k - 1][i - 1][j] +
U[k - 1][i + 1][j] +
U[k - 1][i][j - 1] +
U[k - 1][i][j + 1]) /
4
);
}
})
);
}
U.push(UU);
}
let UThin = [U1];
for (let i = 50; i < U.length; i = i + 50) {
UThin.push(U[i]);
}
return UThin;
}
Insert cell
Insert cell
Insert cell
grids = {
return d3.range(0, U.length).map(function (t) {
let grid = [];
let k = 0;
for (let i = 0; i <= N; i++) {
for (let j = 0; j <= N; j++) {
let y = i/ N;
let x = j / N;
grid.push([x, y, U[t][i][j]]);
k++;
}
}
return pt_list_to_coordString(grid);
});
}
Insert cell
Insert cell
d3.select(pic).selectAll("coordinate").attr("point", grids[t])
Insert cell
Insert cell
T = 5000
Insert cell
N = 50
Insert cell
Insert cell
import { pt_list_to_coordString, show_x3d } from "@mcmcclur/parametric-surfaces"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more