Solution (source code)

= Solution

<Fermat principle> states that the <optical path length> $\mathcal S=\int n\,ds$, or equivalently travel time $\mathcal S/c_0$, is stationary under small variations of the ray with fixed endpoints. Stationarity need not mean a global minimum.

For a plane ray written as $y(x)$, use $\mathcal S[y]=\int F(x,y,y')dx$, with $F=n(x,y)\sqrt{1+y'^2}$. Its stationary paths obey the <Euler-Lagrange equation>
$$
\boxed{\frac{\partial F}{\partial y}-\frac d{dx}\left(\frac{\partial F}{\partial y'}\right)=0.}
$$
It follows by taking the <first variation>, integrating the term in $\delta y'$ by parts, and using zero endpoint variations.