Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

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.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--
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>
              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}
              
            
!

JS

              
                // 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}
  });
});
              
            
!
999px

Console