# E09 · Maxwell on a grid

Solve Maxwell’s coupled field equations with staggered samples, then find out why time steps, pixels, and computational boundaries are part of the physics calculation.

<!-- course-motivation:start -->
## Why learn this?

**When can you trust a simulated electromagnetic field?**

A device with reflections, interfaces, or wavelength-scale features may need Maxwell's coupled fields rather than a prescribed phase mask. Computing those fields still does not make every plotted ripple physical: the grid, time step, and artificial boundaries can change the result.

**What you gain:** Build a time-domain Maxwell solver and test its stability, propagation error, and boundary reflections before interpreting a simulated device response.
<!-- course-motivation:end -->

<!-- course-preparation:start -->
## Before this lesson

Use these focused refreshers if an idea is unfamiliar; you do not need to complete each linked course.

- **Coupled electric and magnetic fields:** Recognize the coupling between changing electric and magnetic fields. Used here: The solver advances both fields in alternating updates. [Review: OpenStax · University Physics Volume 2, §16.1](https://openstax.org/books/university-physics-volume-2/pages/16-1-maxwells-equations-and-electromagnetic-waves). Read Maxwell's Equations and the discussion connecting changing electric and magnetic fields. Concentrate on Faraday's and Ampère–Maxwell's laws.
- **Derivatives on a sampled grid:** Approximate a derivative with neighboring samples and read partial derivatives as local rates of change. Used here: The Yee grid replaces spatial and temporal derivatives by staggered differences. [Review: MIT OCW · Lecture 10: Finite Differences in Time](https://ocw.mit.edu/courses/18-085-computational-science-and-engineering-i-fall-2008/resources/lecture-10-finite-differences-in-time/). Focus on replacing a derivative by a difference quotient and taking successive Euler steps. The stability discussion gives useful context for E09.

**Python preparation:** Use neighboring array slices and a time loop; distinguish field samples, probes, and recorded traces.
- [NumPy · The absolute basics for beginners](https://numpy.org/doc/stable/user/absolute_beginners.html) — Use the sections on array shape, indexing and slicing, basic operations, and broadcasting. Practice changing one parameter and rerunning a cell.

**Developed here:** Construct a Yee update, test its time-step limit, and measure boundary reflections and PML performance.

[All prerequisites and learning resources](https://vandermode.github.io/optics-in-5-minutes-course/preparation/)
<!-- course-preparation:end -->

## Learning outcomes

- Explain the spatial and temporal staggering of a Yee update.
- Distinguish instability, numerical dispersion, and boundary reflection.
- Measure reflection with a reference run and verify an absorber by refinement.

## Use the lesson

Watch the [E09 film](https://www.youtube.com/watch?v=2cyHmzPtHA0) (5:16), embedded near the top of the lesson.

Serve the complete teaching/ directory over HTTP. The lesson uses shared local assets and native JavaScript modules. Open this episode's folder in the browser. Its standalone notebook also works directly in Colab or Jupyter; it does not import repository code or require a GPU.

The notebook's core takes about 25–30 minutes to work through, with a separate optional extension. The first code cell installs numerical packages only when missing. Saved outputs, figures/, and figures.zip provide static figure access if a notebook frontend blocks rich output.

## Model and normalization

1D normalized Yee fields with real, nondispersive ε = n² and μ = 1. The PML specialization uses cubic grading with κ = 1 and α = 0. The electric source is bidirectional. The field display clips amplitudes outside ±1; the growth plot reports actual maxima and stops at 10⁶.

Computed quantities are generated by model.js in the browser and the explicitly written NumPy/SciPy models in lab.py. Model-specific assertions and independent references are in the lab and the course checks. The browser and notebook use documented but not necessarily identical grid sizes. Synthetic randomness is seeded.

## Rebuild

From the repository root, run: python teaching/tools/build_course.py, then python teaching/tools/validate_notebooks.py. The authored page content and controls are in teaching/tools/curriculum.py; opening motivations are in teaching/tools/motivation.py; preparation is in teaching/tools/preparation.py; notebook source is this folder's percent-format lab.py. Building preserves saved outputs when code cells are unchanged. Run npm test --prefix teaching for numerical browser-model tests.

## References

- [Meep · Introduction and Courant stability](https://meep.readthedocs.io/en/stable/Introduction/)
- [Meep · Perfectly matched layers and convergence](https://meep.readthedocs.io/en/latest/Perfectly_Matched_Layer/)
- [Roden & Gedney · Convolution PML](https://doi.org/10.1002/1098-2760(20001205)27:5%3C334::AID-MOP14%3E3.0.CO;2-A)
