# Second Order Runge-Kutta Integration

## The Midpoint Method

We will now take the lessons from the derivation of Euler's method to develop a higher-order integrator. Consider a first order ordinary differential equation in the form

$$\frac{df}{dx} = g(x). \label{ode3}$$

To advance the solution using Euler's method, take the derivative at the beginning of the interval:

$$f(x + \Delta x) = f(x) + g(x) \Delta x. \label{euler_gx}$$

To discover a second order integration scheme, consider what happens when the derivative is taken as the middle of the interval instead:

$$f(x + \Delta x) = f(x) + \Delta x \frac{df}{dx}\Biggr\rvert_{x + {\Delta x}/2.} \label{midpoint}$$

Once again take the Taylor expansion of $\frac{df}{dx}$:

\eqalign{ f(x + \Delta x) &= f(x) + \Delta x \frac{df}{dx}\Biggr\rvert_{x + {\Delta x}/2} \cr &= f(x) + \Delta x (\frac{df}{dx}\Biggr\rvert_x + \frac{\Delta x}{2}\frac{d^2f}{dx^2}\Biggr\rvert_x + \frac{\Delta x^2}{8}\frac{d^3f}{dx^3}\Biggr\rvert_x) \cr &= f(x) + \Delta x \frac{df}{dx}\Biggr\rvert_x + \frac{\Delta x^2}{2}\frac{d^2f}{dx^2}\Biggr\rvert_x + \frac{\Delta x^3}{8}\frac{d^3f}{dx^3}\Biggr\rvert_x \cr &= f(x) + \Delta x \frac{df}{dx}\Biggr\rvert_x +\frac{\Delta x^2}{2}\frac{d^2f}{dx^2}\Biggr\rvert_x + O(x^3). } \label{midpoint_expansion}

By taking the derivative at the middle of the interval instead of the beginning, a scheme of second order accuracy is achieved.

Equation \eqref{rungekutta} shows the steps involved in the integration scheme known as the Midpoint method or the second order Runge-Kutta method:

\eqalign{ f(x_{i+1/2}) & = f(x_i) + \frac{\Delta x}{2} \cdot g(x_{i}, f(x_{i})) \cr f(x_{i+1}) & = f(x_{i}) + \Delta x \cdot g(x_{i+1/2}, f(x_{i+1/2})). } \label{rungekutta}

If $g$ depends only on $x$ and not $f$, the scheme can be simplified to one step:

$$f(x_{i+1}) = f(x_{i}) + \Delta x \cdot g(x_{i+1/2})). \label{rungekutta2}$$