Published
Edited
Nov 27, 2018
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function phi(x,t) {
let l = 2*Math.sqrt(t);
let phi = 1 - erf(x/l);
return phi;
}
Insert cell
dt=0.01
Insert cell
time = {
let i = 1;
yield i*dt;
while (true && running) {
++i;
yield i*dt;
}
}
Insert cell
data = {
let points = [];
let xmin = x.domain()[0];
let xmax = x.domain()[1];
let dx = (xmax-xmin)/(nx-1);
for (let n=0; n<nx; n++) {
let xnow=xmin + n*dx
let point={x:xnow,y:phi(xnow,time)};
points.push(point);
}
return points;
}
Insert cell
nx=500
Insert cell
line = d3.line()
.defined(d => !isNaN(d.x))
.x(d => x(d.x))
.y(d => y(d.y))
Insert cell
x = d3.scaleLinear()
.domain([0,10])
.range([30, width-10])
Insert cell
y = d3.scaleLinear()
.domain([0, 1.2])
.range([height-30,10])

Insert cell
yAxis = g => g
.attr("transform", "translate(30,0)")
.call(d3.axisLeft(y))
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height-30})`)
.call(d3.axisBottom(x))
Insert cell
function erf(x) {
//
// approximation to error function, from https://hewgill.com/picomath/javascript/erf.js.html
//
// constants
var a1 = 0.254829592;
var a2 = -0.284496736;
var a3 = 1.421413741;
var a4 = -1.453152027;
var a5 = 1.061405429;
var p = 0.3275911;

// Save the sign of x
var sign = 1;
if (x < 0) {
sign = -1;
}
x = Math.abs(x);

// A&S formula 7.1.26
var t = 1.0/(1.0 + p*x);
var y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*Math.exp(-x*x);

return sign*y;
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

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