Solution (source code)

= Solution

Let $K(X)=|k(X)|>0$ on a smooth interval, with $X=\epsilon x$. For the <WKB approximation for a slowly varying oscillator>, write
$$
y=\exp\left[\epsilon^{-1}S_0(X)+S_1(X)+\cdots\right].
$$
Substitution into $\epsilon^2y_{XX}+K(X)^2y=0$ gives, at the first two orders,
$$
(S_0')^2+K^2=0,\qquad 2S_0'S_1'+S_0''=0.
$$
Thus $S_0'=\pm iK$ and $S_1'=-K'/(2K)$. The leading <WKB approximation> is
$$
\boxed{y(x)\sim\frac1{\sqrt{K(\epsilon x)}}\left[C_+\exp\left(i\int_{x_*}^xK(\epsilon s)ds\right)+C_-\exp\left(-i\int_{x_*}^xK(\epsilon s)ds\right)\right]}.
$$
The $K^{-1/2}$ <amplitude> is the transport correction accompanying the rapid <wave phase>. Real solutions are equivalent real sine and cosine combinations.

For validity, $K$ must be smooth, nonzero and slowly varying compared with the local wavelength. In physical $x$ derivatives, sufficient local checks are $|K_x|/K^2\ll1$ and $|K_{xx}|/K^3\ll1$. Indeed each displayed branch has relative residual
$$
\frac{y''+K^2y}{K^2y}=\frac{3K_x^2}{4K^4}-\frac{K_{xx}}{2K^3}.
$$
Smooth positive $K(X)$ bounded away from zero has these properties on fixed slow intervals. At a zero of $k$ the <WKB approximation> fails and a <classical turning point> requires a different local analysis, often an <Airy turning-point connection formula>. Rapid variations, coefficient singularities and excessively long accumulation of <wave phase> error also lie outside this leading approximation.