To initialize the visualization, you may need to click "Run all initialization cells" above (see button location in figure).
#This code was authored by Alex Shvonski, Copyright 2020 MIT All Rights Reserved.
%matplotlib notebook
import ipywidgets as widgets
from IPython.display import display, clear_output
from ipywidgets import interact, interactive, interactive_output, fixed, FloatRangeSlider, IntSlider, HBox, Layout, Output, VBox
import numpy as np
import matplotlib.pyplot as plt
from math import pi
import cmath
#import warnings
#warnings.filterwarnings('ignore')
#Define functions
###############################################
###############################################
def intensity(y,d,lam,L):
#let c*epsilon_0*E_0^2 = 1
#result = np.cos(pi*d*10**(-3)*y/(lam*10**(-6)*L*10**(3.)))**2.
result = np.sin(pi*d*10**(-3)*y/(lam*10**(-6)*L*10**(3.)))**2./(pi*d*10**(-3)*y/(lam*10**(-6)*L*10**(3.)))**2.
return result
#Define plot
###############################################
###############################################
fig, ax = plt.subplots(1, 1, figsize=(9.5, 5))
plt.subplots_adjust(left=0.05, bottom=None, right=1, top=None, wspace=None, hspace=1.)
y_array = np.linspace(-50., 50., 1000)
d = 10. #um = 10^-3 mm
lam = 500 #nm = 10^-6 mm
L = 0.5 #m = 10^3 mm
#############################
#plot1
line_1, = ax.plot(y_array, intensity(y_array,d,lam,L),'r-', lw=2)
#plot params
ax.set_title('Single Slit Diffraction Intensity', fontsize=16)
ax.set_xlabel('$y\,[\mathrm{mm}]$', fontsize=16)
ax.set_xlim(min(y_array),max(y_array))
#ax.axis('off')
ax.set_ylim(0,1)
ax.grid(True)
plt.setp(ax.get_xticklabels(), fontsize=14)
plt.setp(ax.get_yticklabels(), fontsize=14)
#Define plot updater
###############################################
###############################################
def update(d,lam,L):
line_1.set_ydata(intensity(y_array,d,lam,L))
fig.canvas.draw_idle()
return
#Define control elements
###############################################
###############################################
s1=widgets.FloatSlider(
min=0.,
max=100.,
step=1.,
value=10.,
layout=Layout(width='500px'),
description='$D\,[\mathrm{\mu m}]$',
style = {'description_width': 'initial'})
s2=widgets.FloatSlider(
min=100.,
max=1000.,
step=10,
value=500,
layout=Layout(width='500px'),
description='$\lambda\,[\mathrm{nm}]$',
style = {'description_width': 'initial'})
s3=widgets.FloatSlider(
min=0.5,
max=2.,
step=0.01,
value=0.5,
layout=Layout(width='500px'),
description='$L\,[\mathrm{m}]$',
style = {'description_width': 'initial'})
#Connect controls to plot
###############################################
###############################################
out = interactive_output(update, {'d': s1, 'lam': s2, 'L': s3})
#Set layout
###############################################
###############################################
Vbox_layout = Layout(display='flex', flex_flow='column', justify_content='space-between', align_items='center')
#Display output
###############################################
###############################################
display(VBox([s1, s2, s3], layout=Vbox_layout))
VBox(children=(FloatSlider(value=10.0, description='$D\\,[\\mathrm{\\mu m}]$', layout=Layout(width='500px'), s…
Plot: The time-averaged intensity of light of wavelength $\lambda$ transmitted through a single slit of width $D$, projected on a screen a distance $L$ away. The intensity is shown as a function of $y\,[\mathrm{mm}]$. The small angle approximation is used for the spatial window that is displayed.
Consider the following questions and possible actions:
#The code in this block includes content from StackOverFlow User: harshil (CC BY-SA 4.0)
#and can be found here: https://stackoverflow.com/questions/27934885/how-to-hide-code-from-cells-in-ipython-notebook-visualized-with-nbviewer
###############################################
###############################################
#Enable hidden code
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')