Introducing students to nonlinear model fitting with Jupyter notebooks through a quantitative diffraction experiment

The sophomore-level laboratory at Wellesley College emphasizes the development of data analysis and visualization skills. We use the Jupyter notebook computational environment, which combines code, output, and commentary into a single document. Here, we describe the experiment that introduces our students to nonlinear model fitting. Students translate a photodiode across a diffraction pattern to measure the spatial dependence of the pattern intensity, fit values predicted by the Fraunhofer approximation to their measured data, and determine parameters like the width of a diffracting slit with ≈ 0.5μm precision. We discuss how Jupyter notebooks encourage data transparency and foster student sense-making.


I. INTRODUCTION
Developing students' abilities to implement mathematical models using computers, visualize experimental data, and fit models to data in order to learn something about a physical system are important goals for a laboratory curriculum.Indeed, Modeling and Analyzing and Visualizing Data are two of the six focus areas highlighted by the AAPT Recommendations for the Undergraduate Physics Laboratory Curriculum [1].At Wellesley College, we emphasize these skills in the laboratory associated with the sophomore-level course Physics 202, Introduction to Quantum Mechanics and Thermodynamics.Here, we describe the Physics 202 experiment that introduces students to nonlinear model fitting: a quantitative experiment on one-dimensional Fraunhofer diffraction by single and double slits that also introduces the students to modern optical equipment.
The Physics 202 laboratory, which meets once weekly for three hours, includes experiments on thermal physics and modern physics along with focused work on computational data analysis.Prior to taking Physics 202, our students have typically completed one semester each of calculus-based introductory mechanics and electromagnetism.While the introductory laboratories introduce topics like error propagation, the emphasis on data visualization, model fitting, and error analysis in the Physics 202 laboratory is a substantial jump in sophistication for most students.In recent years, the Physics 202 laboratory has introduced students to Python computational tools for data analysis [2].While students are sometimes introduced to computation (with Glowscript VPython) in the introductory courses, we presume no experience at the beginning of Physics 202.We rely on the Jupyter notebook environment [3] for computation, data analysis, and visualization.As we will discuss, the ability to combine code (for analysis or visualization), the output of that code, and commentary in a single Jupyter notebook fosters scientific transparency and experimental sense-making.
In this manuscript, we describe how our students scan a photodetector across the far-field diffraction pattern of a single slit and determine the slit width.While there are many examples of laboratory experiments that examine diffraction and interference phenomena using similar techniques [4][5][6], we focus here on the data analysis skills that our students develop and how Jupyter notebooks facilitate student learning.For our students, however, the physics in this experiment is new.While some students have had a brief introduction to wave optics in their introductory electromagnetism course and may have measured the locations of single-and doubleslit intensity maxima, they have not previously modeled the intensity of the diffraction patterns quantitatively.

II. FRAUNHOFER DIFFRACTION BY A SINGLE SLIT
Consider an aperture of width a that is illuminated by a normally incident plane wave of wavelength λ.Suppose that a detector is located in a plane a perpendicular distance L away from the aperture.If the aperture is sufficiently tall, the diffraction of the plane wave can be modeled onedimensionally, and the intensity I at the detector depends on the angle θ between the detector and the optical axis.In the far field, when L a 2 /λ, the Fraunhofer approximation applies, and I(θ) is given by [7] Here, I 0 is the intensity in the forward direction.Let y denote the position of the detector along the detector plane.If the central maximum of the diffraction pattern is located at y 0 , then in the small angle approximation sin θ ≈ (y − y 0 )/L.Rewriting Eq. ( 1) in terms of y, we obtain In our experiment, the laser wavelength λ is given and L can be measured.We fit Eq. (2) to measurements of I at different values of y with a, I 0 , and y 0 as parameters to be determined by the fit.This enables us to measure the aperture width a.
Notice that I(y) depends in a nonlinear way on a and y 0 .Unlike most of the mathematical models our students have previously encountered, it is not possible to mathematically manipulate Eq. ( 2) to obtain a function that depends linearly on a (e.g., by taking a logarithm).

III. EXPERIMENTAL SETUP
The optical setup we use to scan diffraction patterns is shown in Fig. 1.We arrange all the optical components on a standard optical table or breadboard.The beam from a 0.95-mW-power, 632.8-nm-wavelength helium-neon (HeNe) laser (Uniphase 1108) is first expanded to a diameter of ≈ 2 mm by a pair of convex lenses arranged as a Keplerian telescope.The beam is then directed with a pair of steering mirrors onto a mounted slide containing slits of several widths or pairs of slits with different widths and separations (Pasco OS-9179).Our photodetector, a silicon photodiode (Thorlabs SM1PD1A), is mounted on a motorized, computer-controlled linear translation stage (Zaber T-LLS260) that can be scanned perpendicular to the optical axis.Because of the photodiode's large photosensitive area (≈ 1 cm 2 ), we mount a 100-µmwide slit (Thorlabs S100R) immediately in front of the photodiode to improve the detector's spatial resolution.
In our setup, the maximum photocurrent is approximately 10 −7 A. A current-to-voltage converter constructed from a LF 411 operational amplifier [8] maintains the photodiode at zero bias and converts the photocurrent into a measurable voltage (Fig. 2).We use a 10 MΩ gain resistor to produce an output voltage around 1 V.We assemble the current-to-voltage circuit on a solderless breadboard and measure the output voltage using a digital multimeter (BK Precision 2704C).

IV. PEDAGOGICAL CONTEXT AND COMPUTATIONAL ENVIRONMENT
In the laboratory, students work in groups of two or three and begin by aligning the beam expander telescope, the diffracting aperture, and the photodetector.Usually, this experiment is our students' first experience with researchgrade optical components, and we have found this relatively straightforward alignment to be an excellent introduction to working with optics.After completing the alignment, students measure the output from the current-to-voltage converter while moving the photodiode across a diffraction pattern, and typically measure both the diffraction pattern from a single slit as well as from a pair of slits.We provide the students with a working current-to-voltage converter and a simple computer program to control the motorized stage.We do so because our students complete the experiment in a single meeting and have not yet studied electronics.
Our students use Jupyter notebooks for modeling the Fraunhofer diffraction patterns of single and double slits, visualizing their own data, and fitting Fraunhofer diffraction patterns to their data.Jupyter notebooks are full-fledged computational environments that run locally within a web browser.While we formerly introduced students to MAT-LAB in Physics 202, there are several reasons why we now use Jupyter notebooks.First, they allow the execution of code written using standard open-source Python scientific libraries such as numpy, scipy, and matplotlib.Second, in a fashion similar to Mathematica, they integrate Python code cells, the output of those cells (which can include plots), and commentary cells (known as Markdown cells) into a single userfriendly document.The commentary cells allow the writing of mathematical symbols and equations using L A T E X. Figure 3 is a screenshot of a Jupyter notebook submitted by a student showing these features.Critically, the combination of code, output, and commentary allows a Jupyter notebook to capture the entire scientific narrative of a data analysis.Rather than just displaying a plot, a Jupyter notebook shows how that plot was generated.Third, these features have led to the growing adoption of Jupyter notebooks by Wellesley faculty for research as well as in the data science industry.Finally, Jupyter notebooks can easily be shared (particularly using websites such as github.com),which can facilitate the communication

Markdown cell
LaTeX formatting FIG. 3. Screenshot from a student-submitted Jupyter notebook.The screenshot shows code written by a student to generate a plot of her own experimental double-slit diffraction data along with a best-fit model, the plot resulting from that code, as well as some commentary in a Markdown cell. of scientific results.
Prior to beginning their experimental work, we ask our students to plot the expected diffraction patterns from single and double slits, and to explore how the diffraction patterns depend on parameters like the slit width.These computational activities help the students develop a stronger intuition for what they will measure.The computational activities also help scaffold the data analyses our students will later do, since they will eventually need to implement a model like Eq. (2) in software.Following the end of the laboratory period, students analyze their data and present their results in a Jupyter notebook.Several days later, the students electronically submit their Jupyter notebooks for assessment.
It takes our students some time to learn to use Jupyter notebooks effectively and to become comfortable fixing common bugs in their own code.Crucially, our students do this experiment roughly midway through the semester, after having had several weeks' worth of experience in using Jupyter notebooks and in performing linear least squares fits.

A. Typical results and analysis
Figure 4 shows a typical single-slit diffraction pattern recorded with our setup.There is excellent agreement between our data and a best fit to Eq. (2).In order to perform the fit, we use the Levenberg-Marquardt nonlinear least squares algorithm implemented in scipy.optimize.curve_fit.To use this algorithm, we first need to implement (2) as a callable software function.We then need to provide a well-chosen initial guess for the model parameters -a step that is not needed for linear least squares problems.As our students quickly discover, the Levenberg-Marquardt algorithm may not converge sensibly for poorly chosen initial parameter values.Our students learn to see if the initial parameter values they choose are reasonable by plotting the model calculated using those values.They also learn the importance of visualizing the best-fit model and examining the residuals after the fit has run.
In the measurements shown in Fig. 4, the distance L between the diffracting aperture and the photodetector is (795± 2) mm.The maximum angle θ between the detector and the optical axis is approximately 0.03 rad, justifying the smallangle approximation used in deriving Eq. (2).From the fit, we find a = (80.0± 0.3) µm, which is consistent with the nominal 80 µm width of the slit.

B. Sample student work and feedback
The Jupyter notebook shown in Fig. 3 illustrates some of the capabilities they provide for computation and presentation.The software implementation of Eq. (2) (or the more complex model describing diffraction by a pair of slits) is not always straightforward for our students.However, the combination of code and output in a single document allows students to easily show other students or their instructors exactly how they have processed their data.We encourage this transparency with experimental data and their analysis as good scientific practice.This transparency is also invaluable when students encounter software bugs or something unexpected in their data.
More importantly, from a pedagogical standpoint, we encourage students to use Markdown commentary cells to capture their thoughts, questions, and observations while analyzing data -much as they might write observations in a laboratory notebook.These commentary cells can provide valuable insight into student reasoning and sense-making.We present one example.Prior to taking data, a student calculated and plotted the single-slit diffraction pattern [Eq.( 1)] symmetrically about θ = 0; her plots showed equal numbers of secondary maxima on either side of the central maximum.However, in the laboratory, she and her partners did not scan the photodiode over equal distances on both sides of the central maximum.Therefore, when she plotted her data, she noticed that she had more secondary maxima on one side than the other.A quotation from the commentary cell immediately following this plot captures her reasoning: We have one more bump on the left than we do on the right but that may be [be]cause we took more data on the left side than on the right side.
Here, the student noticed something she was not expecting but was able to rationalize her observations by connecting her plot to what she had done in the laboratory.
As demonstrated through responses to anonymous questionnaires, our students value both the experimental skills they develop in this exercise as well as our emphasis throughout the Physics 202 laboratory on developing data analysis skills using Jupyter notebooks.One student noted that Jupyter notebooks were "a very clean way to present all our graphs, calculations, and answers," and another remarked that "I . . .learned that I really like coding . . . the feeling of pride I got after completing a notebook was one of the highlights of this semester for me."Regarding this experiment in particular, one student explained that it was one of her favorite experiments of the semester because "we . . .learned a lot about the set-up of [an] optics table and learned how to use a lot of new equipment.It was a frustrating lab, but, more so in hindsight, a fun one."This comment highlights the substantial intellectual and technical challenges -in both data analysis and optical alignment -that this experiment presents to our students while demonstrating its pedagogical value.

VI. CONCLUSIONS
We have described an experiment that uses a scanned photodiode to quantitatively measure Fraunhofer diffraction patterns and introduces nonlinear model fitting using the Jupyter notebook computational environment.We have discussed how Jupyter notebooks encourage transparency with data, foster the communication of scientific narratives, and provide insight into student reasoning.This experiment can straightforwardly be extended to include additional slits and apertures of different shapes, or can also explore Fresnel diffraction [5].In the future, we plan to assess what our students learn from this experiment more systematically.We also plan to examine how our students' expectations and attitudes towards experimental physics evolve during the Physics 202 laboratory as a whole using surveys like the E-CLASS [9].

FIG. 1 .
Photodiode FIG. 2. Schematic diagram of photodiode current-to-voltage converter.The photocurrent IP D flows from the zero-biased photodiode (PD) through the gain resistor R. The output voltage Vout from the operational amplifier is given by Vout = −IP D R. For clarity, 0.1 µF filter capacitors on the ±12 V power supplies are not shown.

FIG. 4 .
FIG.4.Diffraction pattern from an 80-µm-wide slit recorded by a photodiode scanned perpendicular to the optical axis.Open symbols: measured points; solid line: best fit to Eq. (2).