Light ray equation: derivation?

For a course in Experimental Aerdynamics I have to study a section on Optical Measurement Techniques. My oral exam is coming up and there is this little thing I don't understand. It annoys me expremely. It is the ray equation. It is about light ray path in a medium with changing refractive index. I will copy what's in the lecture notes, and maybe someone will know the derivation of this formulae.

You can easily obtain this equation from the standard variational principle for light. This principle, called Fermat's principle, states that light will take the path which extremizes the time of flight.

Ok, so how does it work out? The time of flight is given by the integral [tex] T = \int \frac{ds}{v} ,[/tex] where [tex] ds [/tex] is the infinitesimal arc length and [tex] v [/tex] is the speed of light. You know that the speed of light in a medium of refractive index [tex] n [/tex] is just [tex] v = c/n [/tex], so you rewrite the time of flight as [tex] T = \frac{1}{c} \int n(\vec{x}(s)) ds, [/tex] where I have indicated the dependence on arc length [tex] s [/tex]. Following the usual variational approach, we try to find which path extremizes this quantity. Consider varying the path of the light ray from [tex] \vec{x}(s) [/tex] to [tex] \vec{x}(s) + \delta \vec{x}(s) [/tex] with end points fixed. The first order variation in the time of flight is given by [tex] \delta T = \frac{1}{c} \int \[ \delta n ds + n \delta ds \], [/tex] and note that it is important to remember that the infinitesimal arc length changes too! We can easily calculate the first term as [tex] \delta n = \nabla n \cdot \delta \vec{x}. [/tex] The variation of arc length is also easy to calculate: [tex] \delta ds = \sqrt{(d\vec{x} + d \delta \vec{x})^2} - \sqrt{(d\vec{x})^2} = ds \frac{d\vec{x}}{ds} \cdot \frac{d \delta \vec{x}}{ds}, [/tex] where I have kept only first order terms in [tex] \delta \vec{x} [/tex]. We can now write the variation of T as [tex] \delta T = \frac{1}{c} \int [ \nabla n \cdot \delta \vec{x} + n \frac{d\vec{x}}{ds} \cdot \frac{d \delta \vec{x}}{ds} ] ds = \frac{1}{c} \int [ \nabla n - \frac{d}{ds}\left(n \frac{d\vec{x}}{ds}\right) ]\cdot \delta \vec{x} ds. [/tex] The last equality there follows from an integration by parts and the fact that [tex] \delta \vec{x} [/tex] vanishes at the end points. The extremal path has [tex] \delta T = 0 [/tex] for arbitrary variations [tex] \delta \vec{x} [/tex]. This implies that the extremal path satisfies [tex] \nabla n - \frac{d}{ds}\left(n \frac{d\vec{x}}{ds}\right) = 0, [/tex] which is the desired equation.

If you haven't seen it before, the variational approach is extremely powerful and elegant. I like it a lot. Hope this helps.