diff --git a/src/javascript/sim5.js b/src/javascript/sim5.js index 3bfdc95..8dd3571 100644 --- a/src/javascript/sim5.js +++ b/src/javascript/sim5.js @@ -7,6 +7,7 @@ const c = cvs.getContext('2d'); const wavelengthInput = document.getElementById("wavelengthInput_DS"); const slitWidthInput = document.getElementById("slitWidthInput_DS"); const slitSeparationInput = document.getElementById("slitSeparationInput_DS"); +const envelopeInput = document.getElementById("envelopeInput_DS"); const simulation = new DoubleSlitSimulation(cvs, c) const animate = () => { @@ -32,5 +33,9 @@ slitSeparationInput.oninput = () => { simulation.setSlitSeparation(slitSeparationInput.value / 1_000_000); } +envelopeInput.oninput = () => { + simulation.setEnvelope(envelopeInput.checked); +} + animate() \ No newline at end of file diff --git a/src/javascript/sim6.js b/src/javascript/sim6.js index 318e4c5..b9127f0 100644 --- a/src/javascript/sim6.js +++ b/src/javascript/sim6.js @@ -8,6 +8,7 @@ const slitsInput = document.getElementById("slitsInput_nSlit"); const wavelengthInput = document.getElementById("wavelengthInput_nSlit"); const slitWidthInput = document.getElementById("slitWidthInput_nSlit"); const slitSeparationInput = document.getElementById("slitSeparationInput_nSlit"); +const envelopeInput = document.getElementById("envelopeInput_nSlit"); const simulation = new NSlitSimulation(cvs, c) const animate = () => { @@ -38,5 +39,9 @@ slitsInput.oninput = () => { simulation.setSlits(slitsInput.value); } +envelopeInput.oninput = () => { + simulation.setEnvelope(envelopeInput.checked); +} + animate() \ No newline at end of file diff --git a/src/javascript/simulations/doubleSlit.js b/src/javascript/simulations/doubleSlit.js index 901c158..62cae4e 100644 --- a/src/javascript/simulations/doubleSlit.js +++ b/src/javascript/simulations/doubleSlit.js @@ -10,6 +10,7 @@ class DoubleSlitSimulation extends Simulation { this.wavelength = wavelength; this.slitWidth = slitWidth; this.slitSeparation = slitSeparation; + this.envelope = 1; this.color = w2h(this.wavelength); this.t = 0; @@ -25,19 +26,30 @@ class DoubleSlitSimulation extends Simulation { this.slitWidth / this.ypx2m, this.slitSeparation / this.ypx2m); this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; } evaluate = (theta) => { theta = Math.round(theta * 1_000_000) / 1_000_000; if (theta in this.cache) return this.cache[theta]; const sine = Math.sin(theta); - const cs = Math.cos(Math.PI * this.slit.separation * this.ypx2m * sine / this.wavelength); + const cs = Math.cos(Math.PI * (this.slit.separation + this.slit.width) * this.ypx2m * sine / this.wavelength); const a = Math.PI * this.slit.width * this.ypx2m * sine / this.wavelength const tmp = Math.sin(a) / a; this.cache[theta] = cs * cs * tmp * tmp; return this.cache[theta]; } + evaluateEnvelope = (theta) => { + theta = Math.round(theta * 1_000_00) / 1_000_00; + if (theta in this.cacheEnvelope) return this.cacheEnvelope[theta]; + let sine = Math.sin(theta); + let a = Math.PI * this.slit.width * this.ypx2m * sine / this.wavelength; + let tmp = Math.sin(a) / a; + this.cacheEnvelope[theta] = tmp * tmp; + return this.cacheEnvelope[theta]; + } + update = () => { this.t += this.dt; @@ -46,6 +58,7 @@ class DoubleSlitSimulation extends Simulation { this.screen.draw(); this.slit.draw(); this.plotIntensity(); + if (this.envelope) this.plotEnvelope(); this.redraw = false; } else this.c.clearRect(this.slit.x + 2.5, 0, this.screen.x - this.slit.x - 5, this.cvs.height); @@ -75,11 +88,25 @@ class DoubleSlitSimulation extends Simulation { this.c.stroke(); } + plotEnvelope = () => { + this.c.beginPath(); + this.c.lineWidth = 1; + this.c.strokeStyle = "#FFFFFF"; + for (let y = 0; y <= this.cvs.height; y++) { + const theta = Math.atan2((y - this.slit.y) * this.ypx2m, (this.screen.x - this.slit.x) * this.xpx2m); + const intensity = this.evaluateEnvelope(theta) * this.cvs.width / 10; + if (y === 0) this.c.moveTo(this.screen.x + 5 + intensity, y); + else this.c.lineTo(this.screen.x + 5 + intensity, y); + } + this.c.stroke(); + } + displayMeasurements = () => { this.c.save(); this.c.beginPath(); this.c.moveTo(this.slit.x, this.cvs.height * 0.9); this.c.lineTo(this.screen.x, this.cvs.height * 0.9); + this.c.lineWidth = 3; this.c.strokeStyle = "#179e7e"; this.c.stroke(); this.c.translate((this.slit.x + this.screen.x) / 2, this.cvs.height * 0.9 - 18); @@ -96,6 +123,7 @@ class DoubleSlitSimulation extends Simulation { this.color = w2h(wavelength); this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; } setSlitWidth = (slitWidth) => { @@ -103,6 +131,7 @@ class DoubleSlitSimulation extends Simulation { this.slit.width = slitWidth / this.ypx2m; this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; } setSlitSeparation = (slitSeparation) => { @@ -110,6 +139,12 @@ class DoubleSlitSimulation extends Simulation { this.slit.separation = slitSeparation / this.ypx2m; this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; + } + + setEnvelope = (envelope) => { + this.envelope = envelope; + this.redraw = true; } intensityAt = (x, y) => { diff --git a/src/javascript/simulations/nSlit.js b/src/javascript/simulations/nSlit.js index 400c5da..7a31ccc 100644 --- a/src/javascript/simulations/nSlit.js +++ b/src/javascript/simulations/nSlit.js @@ -11,6 +11,7 @@ class NSlitSimulation extends Simulation { this.slitWidth = slitWidth; this.slitSeparation = slitSeparation; this.slits = slits; + this.envelope = 1; this.color = w2h(this.wavelength); this.t = 0; @@ -26,6 +27,7 @@ class NSlitSimulation extends Simulation { this.slitWidth / this.xpx2m, this.slitSeparation / this.xpx2m, this.slits); this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; } evaluate = (theta) => { @@ -40,6 +42,16 @@ class NSlitSimulation extends Simulation { return this.cache[theta]; } + evaluateEnvelope = (theta) => { + theta = Math.round(theta * 1_000_00) / 1_000_00; + if (theta in this.cacheEnvelope) return this.cacheEnvelope[theta]; + let sine = Math.sin(theta); + let a = Math.PI * this.slit.width * this.xpx2m * sine / this.wavelength; + let tmp = Math.sin(a) / a; + this.cacheEnvelope[theta] = tmp * tmp; + return this.cacheEnvelope[theta]; + } + update = () => { this.t += this.dt; @@ -48,6 +60,7 @@ class NSlitSimulation extends Simulation { this.screen.draw(); this.slit.draw(); this.plotIntensity(); + if (this.envelope) this.plotEnvelope(); this.redraw = false; } else this.c.clearRect(0, this.screen.y + 2.5, this.cvs.width, this.slit.y - this.screen.y - 5); @@ -77,11 +90,25 @@ class NSlitSimulation extends Simulation { this.c.stroke(); } + plotEnvelope = () => { + this.c.beginPath(); + this.c.lineWidth = 1; + this.c.strokeStyle = "#FFFFFF"; + for (let x = 0; x <= this.cvs.width; x++) { + const theta = Math.atan2((x - this.slit.x) * this.xpx2m, (this.slit.y - this.screen.y) * this.ypx2m); + const intensity = this.evaluateEnvelope(theta) * this.cvs.height / 5; + if (x === 0) this.c.moveTo(x, this.screen.y - 5 - intensity); + else this.c.lineTo(x, this.screen.y - 5 - intensity); + } + this.c.stroke(); + } + displayMeasurements = () => { this.c.save(); this.c.beginPath(); this.c.moveTo(this.cvs.width * 0.9, this.slit.y); this.c.lineTo(this.cvs.width * 0.9, this.screen.y); + this.c.lineWidth = 3; this.c.strokeStyle = "#179e7e"; this.c.stroke(); this.c.translate(this.cvs.width * 0.9 + 40, (this.slit.y + this.screen.y) / 2); @@ -98,6 +125,7 @@ class NSlitSimulation extends Simulation { this.color = w2h(wavelength); this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; } setSlitWidth = (slitWidth) => { @@ -105,6 +133,7 @@ class NSlitSimulation extends Simulation { this.slit.width = slitWidth / this.xpx2m; this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; } setSlitSeparation = (slitSeparation) => { @@ -112,6 +141,7 @@ class NSlitSimulation extends Simulation { this.slit.separation = slitSeparation / this.xpx2m; this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; } setSlits = (slits) => { @@ -119,6 +149,12 @@ class NSlitSimulation extends Simulation { this.slit.slits = slits; this.redraw = true; this.cache = {}; + this.cacheEnvelope = {}; + } + + setEnvelope = (envelope) => { + this.envelope = envelope; + this.redraw = true; } intensityAt = (x, y) => { diff --git a/src/simulations/double_slit_diffraction.md b/src/simulations/double_slit_diffraction.md index 359c017..0ce0b2f 100644 --- a/src/simulations/double_slit_diffraction.md +++ b/src/simulations/double_slit_diffraction.md @@ -12,5 +12,9 @@ Wavelength: 500 nm +
+ + +
\ No newline at end of file diff --git a/src/simulations/n_slit_diffraction.md b/src/simulations/n_slit_diffraction.md index 1419163..39c21ed 100644 --- a/src/simulations/n_slit_diffraction.md +++ b/src/simulations/n_slit_diffraction.md @@ -16,5 +16,9 @@ Wavelength: 500 nm +
+ + +
\ No newline at end of file