Skip to content

Instantly share code, notes, and snippets.

@nkint
Last active May 15, 2019 22:32
Show Gist options
  • Save nkint/0436719e2c6cae01e9cd27009cf5e884 to your computer and use it in GitHub Desktop.
Save nkint/0436719e2c6cae01e9cd27009cf5e884 to your computer and use it in GitHub Desktop.
Quantum Harmonic Oscillator Animation
// use typescript
// use math.js
import math from "mathjs";
// a
// a1 a2 b c d
// (2^n*n!)^(-1/2) * Pi^(-1/4) * Exp[-x^2/2] * HermiteH[n, x];
function psi(n: number, x: number) {
const nf = math.factorial(n) as number;
const a1 = Math.pow(2, n * nf);
const a2 = -1 / 2;
const a = Math.pow(a1, a2);
const b = Math.pow(Math.PI, -1 / 4);
const c = Math.exp(Math.pow(-x, 2) / 2);
const d = computeHermite(n, x);
return a * b * c * d;
}
function energy(n: number) {
return n + 1 / 2;
}
// psit: psi[n, x] Exp[-I*energy[n]*t]
function psit(n: number, x: number, t: number) {
const n1 = psi(n, x);
const i = math.multiply(-1, math.sqrt(-1));
const e = energy(n);
const n2 = math.exp(math.multiply(math.multiply(i, e), t));
const ret = math.multiply(n1, n2);
return ret;
}
// Im[psit[1, x, t]]
export function graphD(x: number, t: number, amplitude: number) {
const _x = fit(x, 0, 1, -5, 5);
const y = (math.multiply(psit(1, _x, t), 1) as any).im;
return y;
}
console.log("x:0, t: 0, f(x, t): ", graphD(0, 0, 10));
console.log("x:-2, t: 0, f(x, t): ", graphD(-2, 0, 10));
console.log("x:2, t: 0, f(x, t): ", graphD(2, 0, 10));
console.log("x:0, t: 0.5, f(x, t): ", graphD(0, 0.5, 10));
console.log("x:-2, t: 0.5, f(x, t): ", graphD(-2, 0.5, 10));
console.log("x:2, t: 0.5, f(x, t): ", graphD(2, 0.5, 10));
console.log("x:0, t: 1, f(x, t): ", graphD(0, 1, 10));
console.log("x:-2, t: 1, f(x, t): ", graphD(-2, 1, 10));
console.log("x:2, t: 1, f(x, t): ", graphD(2, 1, 10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment