Public
Edited
Mar 30, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
f = x => x ** 3 + 2 * x + 1
Insert cell
fp = x => 3 * x ** 2 + 2
Insert cell
afp = (x, h) => (h >= 0 ? (f(x + h) - f(x)) / h : (f(x - h) - f(x)) / h)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
afp_centered = (x, h) =>
h >= 0 ? (f(x + h) - f(x - h)) / (2 * h) : (f(x - h) - f(x + h)) / (2 * h)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
U = 0.25
Insert cell
xDomain = [0, 1]
Insert cell
numOfSteps = 10
Insert cell
dx = (xDomain[1] - xDomain[0]) / numOfSteps
Insert cell
nodes = Array.from(
{ length: numOfSteps + 1 },
(d, i) => xDomain[0] + (i * (xDomain[1] - xDomain[0])) / numOfSteps
)
Insert cell
function waveEquationApp(us, dt) {
return us.map((u, i) => {
if (i === 0) {
return u - ((U * dt) / dx) * (us[i + 1] - us[i]);
} else if (i === us.length - 1) {
return u - ((U * dt) / dx) * (us[i] - us[i - 1]);
}
return u - ((U * dt) / (2 * dx)) * (us[i + 1] - us[i - 1]);
});
}
Insert cell
ut0 = Array.from({ length: numOfSteps + 1 }, (d, i) => 0)
Insert cell
Insert cell
(ut0[0] = 0.1)
Insert cell
Insert cell
ut1 = waveEquationApp(ut0, 1)
Insert cell
ut2 = waveEquationApp(ut1, 1)
Insert cell
ut3 = waveEquationApp(ut2, 1)
Insert cell
Insert cell
Insert cell
Insert cell
{
let div = d3
.create('div')
.style('width', `${width}px`)
.style('height', `${200}px`);

let running = true;
let interval_id;
let controls = div
.append('div')
.style('width', `${300}px`)
.style('height', `${30}px`);
let start_stop_button = controls
.append('div')
.style('display', 'inline-block')
.append('button')
.on('click', function() {
if (running) {
running = false;
clearInterval(interval_id);
d3.select(this).html('start');
} else {
running = true;
start();
d3.select(this).html('stop');
}
})
.html('stop');
controls
.append('div')
.style('display', 'inline-block')
.append('button')
.on('click', setup)
.html('reset');
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", 200);

function setup() {
if (running) {
running = false;
clearInterval(interval_id);
start_stop_button.html('start');
}
}

function start() {
running = true;
interval_id = setInterval(function() {
console.log(1000);
}, 1000);
}

setup();
// start();
// start_stop_button.text('stop');

return div.node();
}
Insert cell
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
import { lineChartMaker } from "@syaleni/line-chart-maker"
Insert cell
import { table } from "@tmcw/tables@513"
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