JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!--
In the real world, you'd maybe bundle only the code you actually need,
but for sketches like this, the flat bundle is great! (225kb).
See: https://github.com/stdlib-js/stdlib/tree/develop/dist
-->
<script src="https://unpkg.com/@stdlib/stdlib@0.0.25/dist/stdlib-flat.min.js"></script>
<!--
plotly.js with 2D gl plotting code: (370 kb)
-->
<script src="https://cdn.plot.ly/plotly-gl2d-latest.min.js"></script>
<div id="graph"></div>
html, body {
margin: 0;
padding: 0;
}
// Source:
// Feynman, Richard Phillips, Albert R. Hibbs, and Daniel F. Styer.
// "Quantum Mechanics and Path Integrals." Mineola, NY: Dover Publications, 2005.
//
// Chapter 3.2, "Diffraction through a Slit".
//
// Plotted here is Eq. 3.40 for a particle passing through a known region of width b
// at some time T. Note that as ħ goes to zero, it approaches a classical slit which
// casts a sharp shadow (well, in one dimension, that is). The color of the field at
// horizontal slice represents the probability of finding the particle at that point
// and time given it passed through the slit at time T.
//
// TODO: Re-derive by hand to confirm whether the Fresnel integral listed is
// scaled or unscaled definition.
//
// Libraries:
// - https://github.com/stdlib-js/stdlib
// - https://github.com/plotly/plotly.js
//
//const hbar = 1.0 // planck's constant (varied in slider so a parameter, not const)
const m = 1.0 // mass
const T = 0.5 // time before scattering
const V = 1.0 // velocity
const b = 1.0 // slit width
const base = stdlib.base;
const X = V * T
const tmin = 0.0 + T
const tmax = 2.0 + T
const xmin = base.min(0, -4.0 + (V * tmax) * 0.5)
const xmax = base.max(0, 4.0 + (V * tmax) * 0.5)
const nx = 201
const nt = 201
var values = stdlib.logspace(-3, 0, 41)
var valueName = 'ħ'
function computeField (hbar) {
function u (x, t, sgn) {
var k = 1.0 + t / T;
return ((x - V * t) + sgn * b * k) / base.sqrt(stdlib.PI * hbar * t / m * k)
}
function psi(x, t) {
let SCp = base.fresnel(u(x, t, 1))
let SCm = base.fresnel(u(x, t, -1))
return m / (2.0 * stdlib.PI * hbar * (T + t)) * (
0.5 * base.pow(SCp[1] - SCm[1], 2) +
0.5 * base.pow(SCp[0] - SCm[0], 2)
)
}
const t = stdlib.linspace(tmin, tmax, nt)
const x = stdlib.linspace(xmin, xmax, nx)
const y = t.map(t => x.map(x => psi(x - X, t - T)))
return {x: x, y: t, z: y}
}
Plotly.plot('graph', {
data: [stdlib.merge({
type: 'heatmapgl',
colorscale: 'Viridis',
hoverinfo: 'skip',
showscale: false,
}, computeField(values[22])), {
type: 'scattergl',
mode: 'marker+text',
x: [0],
y: [0],
text: ['Initial position'],
marker: {
size: 10,
color: 'blue',
}
}],
layout: {
width: window.innerWidth,
height: window.innerHeight,
xaxis: {
title: 'Position, x'
},
yaxis: {
title: 'Time, t',
range: [-0.1, tmax]
},
title: '1-D diffraction through a slit',
updatemenus: [{
showactive: false,
x: 0,
y: 0,
yanchor: 'top',
xanchor: 'left',
pad: {t: 80},
type: 'buttons',
buttons: [{
label: 'Play',
method: 'animate',
args: [null, {
mode: 'immediate',
fromcurrent: true,
frame: {duration: 30}
}]
}]
}],
sliders: [{
active: 22,
pad: {l: 70, t: 50},
currentvalue: {xanchor: 'right', prefix: valueName + ' = '},
transition: {duration: 0},
steps: values.map(values => ({
label: values.toFixed(3),
method: 'animate',
args: [[values.toFixed(5)], {
mode: 'immediate',
transition: {duration: 0},
frame: {duration: 30}
}]
}))
}]
},
frames: values.map(values => ({
name: values.toFixed(5),
data: [computeField(values)],
traceIndices: [0],
}))
}).then(function () {
return Plotly.animate('graph', null, {
mode: 'immediate',
fromcurrent: true,
frame: {duration: 30}
});
});
Also see: Tab Triggers