Public
Edited
Apr 17, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
setup = ({
a: 0,
b: 2,
A: 0,
B: 1,
D: 1,
k: 0,
u0: (x) => x * (2 - x),
f: (x) => 0,
n: 40,
T: 3
})

// setup = ({
// a: 0,
// b: 2,
// A: -1,
// B: 1,
// D: 1,
// k: 0,
// u0: (x) => (x < 1 ? -1 : 1), // x * (2 - x),
// f: (x) => 0, //-1 / 2,
// n: 50,
// T: 1
// })
Insert cell
Insert cell
Insert cell
Insert cell
function MoL(description) {
let { a, b, A, B, D, k, T, f, u0, n } = description;

let dx = (b - a) / n;
let init = d3.range(n + 1).map((i) => u0(a + i * dx));

function F(v, t) {
return v.map(function (x, i) {
let xi = a + i * dx;
if (i == 0) {
return A; // A;
} else if (i == n) {
return B; //B;
} else if (i == 1) {
return (D * (v[2] - 2 * v[1] + A)) / dx ** 2 + k * v[1] + f(xi);
} else if (i == n - 1) {
return (
(D * (B - 2 * v[n - 1] + v[n - 2])) / dx ** 2 + k * v[n - 1] + f(xi)
);
} else {
return (
(D * (v[i + 1] - 2 * v[i] + v[i - 1])) / dx ** 2 + k * v[i] + f(xi)
);
}
});
}
let solutions = rk4(F, init, 0, 0, T, D * T * n ** 2);
let ymin = d3.min(solutions[0]);
let ymax = d3.max(solutions[0]);
// [ymin, ymax] = d3.extent(solutions.flat());

solutions = solutions.map((s, m) =>
s.map(function (y, i) {
if (i == 0 && m > 0) {
return [a, A];
} else if (i == n && m > 0) {
return [b, B];
} else {
return [a + i * dx, y];
}
})
);

let yrange = ymax - ymin;
solutions.ymin = ymin - 0.5 * yrange;
solutions.ymax = ymax + 0.5 * yrange;
return solutions;
}
Insert cell
Insert cell
solutions = MoL(setup)
Insert cell
Insert cell
d3 = require('d3@6')
Insert cell
format = d3.format('0.2f')
Insert cell
import { Range } from "@observablehq/inputs"
Insert cell
import { Scrubber } from "@mbostock/scrubber"
Insert cell
functionPlot = require("function-plot@1/dist/function-plot")
Insert cell
import { rk4 } from '@mcmcclur/runge-kutta-for-systems-of-odes'
Insert cell
graph_style = html`<style>
g.graph > path {
stroke-width: 3px;
stroke: path
}
</style>`
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