Interactive Diffraction Grating Models for Matlab

A collection of m-files useful for learning and teaching about the principles of diffraction gratings.
Click here to download the ZIP file "DiffractionGratingNew.zip" containing all the programs shown here.

Diffraction grating geometry with a monochromatic incident light (monograting.m)

Click to view enlarged figure
Click to view enlarged figure
Diffraction grating geometry with a monochromatic incident light beam, showing only diffracted orders +4 to -4. The grating surface is at the bottom of the diagram, along the x axis. The line labeled "Incident beam" is the direction of the incoming light beam. The line labeled "0" is the direction of the zeroth-order diffraction. The left and right cursor arrow keys control the angle of incidence, the up and down cursor arrow keys increase and decrease the wavelength of the incident beam, and the A and Z keys control the grating ruling density (lines/mm). Click monograting.m to view code or right-click and select "Save link as..." to download the m-file.

Keyboard commands:
Angle of incidence, degrees...left and right cursor arrows
Wavelength, nm................up and down cursor arrows
Ruling density, lines/mm......A/Z

Monochromatic incident light beam, with only a single selected order shown (ordergrating.m)


Click to view enlarged figure
Diffraction grating with a monochromatic incident light beam, with a single selected order shown. The grating surface is at the bottom of the diagram, along the x axis. The line labeled "Incident beam" is the direction of the incoming light beam. The dotted line is the diffracted (outgoing) beam. The line labeled "Order 0" is the direction of the zeroth-order diffraction. The left and right cursor arrow keys control the angle of incidence, the up and down cursor arrow keys increase and decrease the wavelength of the incident beam, the A and Z keys control the grating ruling density (lines/mm), and the S and X keys increase and decrease the diffraction order shown.  Click ordergrating.m to view code or right-click and select "Save link as..." to download the m-file.

The figure on the left illustrates the operation of an echelle grating, a coarsely-ruled grating that acheives high dispersion by being operated at high diffraction orders and large angles of incidence and diffraction. 

Keyboard commands:
Angle of incidence, degrees...left and right cursor arrows
Wavelength, nm................up and down cursor arrows
Ruling density, lines/mm......A/Z
Diffraction order.............S/X

Diffraction grating illuminated with white light (whitegrating.m)


Click to view enlarged figure
Diffraction grating illuminated with white light, with several pre-selected diffracted wavelengths shown in orders +1 and +2 only. The grating surface is at the bottom of the diagram, along the x axis. The vertical dotted white line in the center is the perpendicular to the grating, from which all angles are measured. The solid white line labeled "Incident beam" is the incoming white light beam. The colored lines are diffracted beams of the selected wavelengths are defined in lines 30-36. Each is labeled with its wavelength and diffraction order (m=1 or 2). (The second-order diffractions are drawn as shorter lines to make them distinct from the first-order diffractions. Dotted lines represent wavelengths that are not normally visible to the human eye). The line labeled "Zero order" is the direction of the zeroth-order diffraction, which is angle of specular reflection from the grating surface. 

The left and right cursor arrow keys control the angle of incidence, the up and down cursor arrow keys (or the A and Z keys)  control the grating ruling density (lines/mm).  The diffracted beam labels will be clearer if the figure window is enlarged or zoomed to full-screen. Click whitegrating.m to view code or right-click and select "Save link as..." to download the m-file.

Keyboard commands:
Angle of incidence, degrees...left and right cursor arrows
Ruling density, lines/mm......A/Z or up and down cursor arrows



Interactive simulation of interference and diffraction (interference.m)


Click to view enlarged figure
Interactive simulation of interference between the reflections from adjacent grooves in a diffraction grating, showing how the operation of a diffraction grating emerges naturally just by adding up a bunch of sine waves. A grating with N grooves is illuminated by pure monochromatic light. When viewed at an angle, the reflections from each groove travel over slightly different path lengths and therefore are slightly phase-shifted with respect to each other. In this simulation, the light beams from each reflection is represented by the sine wave of its electromagnetic vector. Each reflection is slightly phase-shifted compared to adjacent reflections by an amount that depends on the wavelength and the path length differences between adjacent reflections. When all the the sinewaves are added up, this phase shift results in a partial cancellation of some of the waves, called interference. As the path length differences are changed (by pressing the left and right cursor arrow keys), a diffraction pattern emerges that shows intensity maxima whenever the path length difference is an integral number of wavelengths (called the "order"), thereby resulting in constructive interference. As the number of grooves N increases (by pressing the up and down cursor arrow keys), these maxima become very sharp and the intensity between the maxima becomes lower and lower. Real gratings have thousands of grooves and thus have extremely sharp maxima with almost complete darkness between the maxima.  The maxima are the different orders" of diffraction.  Watch the "Total Intensity..." reported below the sine wave plot as you approach path length differences that are an integral number of wavelengths. Start with just 2 grooves, then increase gradually to see how the diffraction pattern evolves. Click interference.m to view code or right-click and select "Save link as..." to download the m-file.

Keyboard commands:
Path Length Difference..............left and right cursor arrows
Number of grooves in the grating....up and down cursor arrows

Diffraction Grating Spectrum (DiffractionGratingSpectrum.m)


Click to view enlarged figure
Self-running simulation of a diffraction grating spectrum illuminated by monochromatic light. Shows a plot of the total intensity at the detector vs the wavelength of the incident light (expressed at the path length difference between adjacent reflections, pld). It's instructive to run this simulation with the number of grooves N=2, then N=3, N=4, and so on. Maxima in the intensity occur whenever the path length difference (pld) is an integral number of wavelengths (called the "order"). As the number of grooves gets bigger and bigger, the maxima become very sharp and the intensity is nearly zero between the maxima. In this figure, the number of grooves N is 100. Click DiffractionGratingSpectrum.m to view code or right-click and select "Save link as..." to download the m-file.

Expanded view of first order diffraction (DiffractionGratingFirstOrder.m)


Click to view enlarged figure
Close-up of first-order diffraction pattern of grating illuminated by monochromatic light. Shows the "diffraction limit' caused by the finite number of grooves, N. The larger N, the narrower this pattern, and the higher the diffraction-limited resolution of the grating. The width of the maximum is inversly proportional to the number of grooves N. In this figure, the number of grooves N is 300. A real, practical spectrometer can not achieve this so-called diffraction-limited resolution because it must utilize a finite slit width in order to allow sufficient light to enter the detector. As the slit with approaches zero, the effective resolution approaches the diffraction limit illustrated here, but the intensity of the light reaching the detector approaches zero. Click DiffractionGratingFirstOrder.m to view code or right-click and select "Save link as..." to download the m-file.

ZIP file containing all of the above functions and demos.  Old versions with sliders, for Matlab 6.5.

Tom O'Haver
Professor Emeritus
Department of Chemistry and Biochemistry
The University of Maryland at College Park
toh@umd.edu
http://www.wam.umd.edu/~toh