Public
Edited
Oct 16, 2024
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
u = uu(
c,
d,
(x) => u0_compiled.evaluate({ x: x }),
(x) => v0_compiled.evaluate({ x: x })
)
Insert cell
// The general solution, in terms of the inputs
function uu(c, k, u0, v0) {
let N = 60;
let pi = Math.PI;
let exp = Math.exp;
let sin = Math.sin;
let cos = Math.cos;

let u0_coeffs = d3
.range(1, N + 1)
.map(n => int(x => u0(x) * sin(n * pi * x), 0, 1, 1e-10, 10, 5));
let u0v0_coeffs = d3
.range(1, N + 1)
.map(n =>
int(x => (k * u0(x) + 2 * v0(x)) * sin(n * pi * x), 0, 1, 1e-10, 10, 5)
);
function u(x, t) {
return (
2 *
d3.sum(
d3.range(1, N + 1).map(function(n) {
let sqrnpcmk = Math.sqrt(4 * n ** 2 * c ** 2 * pi ** 2 - k ** 2);
return (
exp((-k * t) / 2) *
(u0_coeffs[n - 1] * cos((sqrnpcmk * t) / 2) +
(u0v0_coeffs[n - 1] * sin((sqrnpcmk * t) / 2)) / sqrnpcmk) *
sin(n * pi * x)
);
})
)
);
}
return u;
}
Insert cell
// Parse the inputs
u0_compiled = math.compile(u0_text)
Insert cell
v0_compiled = math.compile(v0_text)
Insert cell
Insert cell
example_init_displacement = ({
"displace middle": "x*(1-x)",
"displace left": "x*(1-x)^8",
"pluck middle": "x<0.5 ? 2x: 2-2x",
"pluck left": "x<0.2 ? x/0.2 : (1-x)/0.8",
"strike middle": "0",
"strike left": "0",
"mode 1": "sin(pi x)",
"mode 4": "sin(4pi x)"
})
Insert cell
example_init_velocity = ({
"displace middle": "0",
"displace left": "0",
"pluck middle": "0",
"pluck left": "0",
"strike middle": "exp(-(x-0.5)^2)",
"strike left": "exp(-(x-0.1)^2)",
"mode 1": "0",
"mode 4": "0"
})
Insert cell
Insert cell
scales = {
let w = 0.8 * width;
let h = w * 0.4;

let pad = 15;
let xmin = 0;
let xmax = 1;
let M = d3.max([Math.abs(wmin), Math.abs(wpmin), wpmax, wmax]);
let ymin = -M;
let ymax = M;

let xScale = d3
.scaleLinear()
.domain([xmin, xmax])
.range([pad, w - pad]);
let yScale = d3
.scaleLinear()
.domain([ymin, ymax])
.range([h - pad, pad]);
let pts_to_path = d3
.line()
.x(function (d) {
return xScale(d[0]);
})
.y(function (d) {
return yScale(d[1]);
});

return { xScale: xScale, yScale: yScale, pts_to_path: pts_to_path };
}
Insert cell
// Aproximate max for plotting purposes
wmax = d3.max(
d3
.range(0, 1, 1 / 5)
.map(function(x) {
return minimize(x => -u0_compiled.evaluate({ x: x }), {
lowerBound: x,
upperBound: x + 1 / 5
});
})
.map(x => u0_compiled.evaluate({ x: x }))
)
Insert cell
// Aproximate max speed for plotting purposes
wpmax = d3.max(
d3
.range(0, 1, 1 / 5)
.map(function(x) {
return minimize(x => -v0_compiled.evaluate({ x: x }), {
lowerBound: x,
upperBound: x + 1 / 5
});
})
.map(x => v0_compiled.evaluate({ x: x }))
) / 2
Insert cell
// Aproximate min for plotting purposes
wmin = d3.min(
d3
.range(0, 1, 1 / 5)
.map(function(x) {
return minimize(x => u0_compiled.evaluate({ x: x }), {
lowerBound: x,
upperBound: x + 1 / 5
});
})
.map(x => u0_compiled.evaluate({ x: x }))
)
Insert cell
// Aproximate half min speed for plotting purposes
wpmin = d3.min(
d3
.range(0, 1, 1 / 5)
.map(function(x) {
return minimize(x => v0_compiled.evaluate({ x: x }), {
lowerBound: x,
upperBound: x + 1 / 5
});
})
.map(x => v0_compiled.evaluate({ x: x }))
)/2
Insert cell
Insert cell
d3 = require("d3-selection@3", "d3-scale@3", "d3-shape@3", "d3-array@3", "d3-format@3")
Insert cell
minimize = require('https://bundle.run/minimize-golden-section-1d@3.0.0')
Insert cell
import { adaptiveSimpson as int } from '@rreusser/integration'
Insert cell
import { build_samples } from '@mcmcclur/adaptive-plotter'
Insert cell
math = require('mathjs@7')
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