A diffraction grating is an optical element with many equally spaced slits or grooves.
When monochromatic light falls on it, the waves coming from neighboring slits interfere with one another.
At most directions the interference is partly destructive, but at a small set of special angles it becomes strongly constructive.
Those bright directions are called the principal maxima, and they are the basis of wavelength measurement and spectral resolution in grating spectrometers.
The central relation for an ideal transmission grating is
\[
d\sin\theta_m = m\lambda,
\qquad
m = 0,\pm 1,\pm 2,\dots
\]
Here \(d\) is the grating spacing, \(\lambda\) is the wavelength, and \(\theta_m\) is the angle of the
\(m\)-th diffraction order measured from the straight-through direction.
The integer \(m\) labels the order. The central beam corresponds to \(m=0\), the first pair of side maxima corresponds to \(m=\pm1\), and so on.
A given order exists only if \(|m\lambda/d|\le 1\), because the sine of a real angle cannot exceed 1 in magnitude.
This immediately explains why a grating does not produce infinitely many orders for a fixed wavelength.
For the sample input
\(d = 2\ \mu\text{m}\),
\(\lambda = 500\ \text{nm}\),
\(N = 1000\),
and
\(m = 1\),
first convert the spacing and wavelength to SI units:
\[
\begin{aligned}
d &= 2\times 10^{-6}\ \text{m}, \\
\lambda &= 500\times 10^{-9}\ \text{m}
= 5.00\times 10^{-7}\ \text{m}.
\end{aligned}
\]
Then apply the order equation:
\[
\begin{aligned}
\sin\theta_1
&= \frac{1\cdot \lambda}{d} \\
&= \frac{5.00\times 10^{-7}}{2.00\times 10^{-6}} \\
&= 0.250.
\end{aligned}
\]
Therefore
\[
\theta_1 = \sin^{-1}(0.250) \approx 14.5^\circ,
\]
which matches the sample output. This angle tells you where the first principal maximum appears on either side of the central beam.
A grating is especially useful because it can separate close wavelengths.
The standard ideal resolving-power formula is
\[
R = \frac{\lambda}{\Delta\lambda_{\min}} = Nm,
\]
where \(N\) is the number of illuminated slits and \(m\) is the order magnitude.
Rearranging gives the smallest wavelength difference that can be resolved:
\[
\Delta\lambda_{\min} = \frac{\lambda}{Nm}.
\]
For the sample values,
\[
R = Nm = 1000\cdot 1 = 1000,
\]
so the grating can ideally distinguish wavelengths differing by about
\[
\Delta\lambda_{\min}
= \frac{500\ \text{nm}}{1000}
= 0.500\ \text{nm}.
\]
This shows why more illuminated slits lead to sharper principal maxima and better resolution.
It also shows why higher orders are attractive in spectroscopy: as \(m\) increases, the resolving power increases as well, provided that the selected order still exists.
Another important idea is angular dispersion, which measures how quickly the diffraction angle changes when the wavelength changes.
Starting from
\(d\sin\theta_m = m\lambda\),
differentiate with respect to \(\lambda\):
\[
\begin{aligned}
d\cos\theta_m\frac{d\theta}{d\lambda} &= m, \\
\frac{d\theta}{d\lambda} &= \frac{m}{d\cos\theta_m}.
\end{aligned}
\]
This formula says that the angular spread between nearby wavelengths increases for smaller grating spacing, larger order, and larger diffraction angle.
In practice, this is why dense gratings and higher orders produce more widely separated spectral lines.
However, very high angles can also reduce usable intensity, and real gratings are not perfectly ideal.
The animation in this calculator is schematic. It shows incoming plane waves, a visible subset of slits, and the selected principal maximum on a screen.
The numerical results still use the real value of \(N\), but the plot may cap the displayed slit count so that the interference structure remains readable on screen.
That makes the visualization practical while keeping the algebra exact for the chosen inputs.
At a more advanced university level, one studies finite slit width, blaze angle, efficiency curves, overlapping orders, and the Rayleigh criterion in greater detail.
But for many introductory and intermediate problems, the key relations to remember are
\[
d\sin\theta_m = m\lambda,
\qquad
R = Nm,
\qquad
\Delta\lambda_{\min} = \frac{\lambda}{R},
\qquad
\frac{d\theta}{d\lambda} = \frac{m}{d\cos\theta_m}.
\]
Together, these formulas tell you where each order appears, whether a chosen order is allowed, how sharply the grating can separate nearby wavelengths, and how strongly the angle changes as the wavelength varies.
They are the core of diffraction-grating analysis in optics and spectroscopy.