Ermakov equation arising from electromagnetic fields propagating in 1D inhomogeneous media